728x90
배열돌리기 1
https://www.acmicpc.net/problem/16926
package 배열돌리기시리즈;
import java.util.*;
import java.io.*;
public class 배열돌리기1 {
static int n,m,r;
static StringTokenizer st;
static int[][] arr;
static int[] dx = {1,0,-1,0};
static int[] dy = {0,1,0,-1};
public static boolean cango(int x, int y) {
return x>=0 && x<n && y>=0 &&y<m; //세로크기 m인거 주의!!
}
public static void circulate(int x, int y , int szi, int szj) {
int nx = x;
int ny = y;
/*시작점의 값은 prev에 목적지의 값은 tmp에 저장해둔다.*/
int prev = arr[x][y];//현재좌표의 prev값 저장
for(int i=0; i<4; i++) {
nx+=dx[i];
ny+=dy[i];
while(cango(nx,ny) && nx>=x && ny>=y&& nx<(x+szi) && ny<(y+szj)) { //사각형의 범위내에 있는 좌표인지 확인해본다.
int tmp = arr[nx][ny]; //움직였을떄를 tmp에 일시적으로 저장
arr[nx][ny]=prev;
prev = tmp;
nx+=dx[i];
ny+=dy[i];
}
nx-=dx[i];
ny-=dy[i];
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st= new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
arr= new int[n][m];
for(int i=0; i<n; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<m; j++) {
arr[i][j]=Integer.parseInt(st.nextToken());
}
}
for(int i=0; i<r; i++) {
int x =0;
int y=0;
int ci = (n-1)/2; //맨왼쪽 위 꼭짓점 좌표의 범위
int cj = (m-1)/2;
int szi = n; //움직임을 적용할 가로 크기
int szj = m; //움직임을 적용할 세로 크기
while(x<=ci && y<=cj && szi>0 && szj>0 ) {
circulate(x,y,szi,szj);
x++;
y++;
szi-=2; //사각형의 크기가2씩줄어듬
szj-=2; //사각형의 크기가2씩줄어듬
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
배열돌리기 2 - 모듈러 연산을 통해 회전횟수를 줄인다
https://www.acmicpc.net/problem/16927
package 배열돌리기시리즈;
import java.util.*;
import java.io.*;
public class 배열돌리기2 {
static int n,m,r;
static int[][] arr;
static int[] dx = {1,0,-1,0};
static int[] dy = {0,1,0,-1};
public static boolean cango(int x, int y) {
return x>=0 && x<n && y>=0 && y<m;
}
public static void print() {
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
public static void circulate(int x, int y , int szx, int szy) {
int prev = arr[x][y];
int tx =x;
int ty = y;
int nx = x;
int ny = y;
for(int i=0; i<4; i++) {
nx = nx+dx[i];
ny = ny+dy[i];
while(cango(nx,ny) && nx>=tx && nx<(tx+szx)&&ny>=ty &&ny<(ty+szy)) {
int tmp = arr[nx][ny];
arr[nx][ny] = prev;
prev = tmp;
nx+=dx[i];
ny+=dy[i];
}
nx-=dx[i];
ny-=dy[i];
}
}
public static void main(String[] args) throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
arr = new int[n][m];
for(int i=0; i<n; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<m; j++) {
arr[i][j]= Integer.parseInt(st.nextToken());
}
}
/// for(int i=0; i<r; i++) {
int x =0;
int y =0;
int szx = n;
int szy = m;
while(x<=(n-1)/2 && y<=(m-1)/2 && szx>0 && szy>0){
int total = 2*szx+2*szy-4; //중요!! 돌리는 것도 중복이 되므로 칸의 개수 만큼 모듈러 연산을해서 돌리는것을 최소화한다.
int nr = r%total;
while(nr>0) {
circulate(x,y,szx,szy);
nr--;
}
szx-=2;
szy-=2;
x++;
y++;
}
// }
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
배열돌리기3
https://www.acmicpc.net/problem/16935
package 배열돌리기시리즈;
import java.util.*;
import java.io.*;
public class 배열돌리기3 {
static int n,m,r;
static int[][] arr;
public static void print() {
for(int i=0; i<arr.length;i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void one() {
int hn = (n-1)/2;
int[][]tmp = new int[n][m];
for(int i=0; i<=hn; i++) {
tmp[n-i-1]=arr[i];
tmp[i]=arr[n-i-1];
}
arr = tmp;
}
public static void two() {
int hm = (m-1)/2;
int[][]tmp = new int[n][m];
for(int j=0; j<=hm; j++) {
for(int i=0; i<n;i++) {
tmp[i][j] = arr[i][m-1-j];
}
for(int i=0; i<n;i++) {
tmp[i][m-1-j] = arr[i][j];
}
}
arr= tmp;
}
public static void three() { // 오른쪽으로 90도 중요!!-> 자주나오는 유형
int[][]tmp = new int[m][n];
for(int i=0; i<n; i++) {
int[]list= arr[i];
for(int ni=0; ni<m; ni++) {
tmp[ni][n-1-i]=list[ni];
}
}
int t = m;
m = n;
n =t;
arr = tmp;
}
public static void four() { // 왼쪽으로 90도 중요!!-> 자주나오는 유형
int[][]tmp = new int[m][n];
for(int i=0; i<n; i++) {
int[]list= arr[i];
for(int ni=0; ni<m; ni++) {
tmp[m-1-ni][i]=list[ni];
}
}
int t = m;
m = n;
n =t;
arr = tmp;
}
public static void five() {
int[][]tmp = new int[n][m]; //새로운 배열을 할당에서 다시 배치시키는게 훨 쉽다
int is = 0;
int js =0;
for(int i=is;i<is+n/2; i++) {
for(int j=js; j<js+m/2; j++) {
tmp[i][j]=arr[i+n/2][j];
}
}
is = 0;
js = m/2;
for(int i=is;i<is+n/2; i++) {
for(int j=js ; j<js +m/2; j++) {
tmp[i][j] =arr[i][j-js];
}
}
is = n/2;
js = m/2;
for(int i=is;i<is+n/2; i++) {
for(int j=js ; j<js +m/2; j++) {
tmp[i][j]= arr[i-is][j];
}
}
is = n/2;
js = 0;
for(int i=is;i<is+n/2; i++) {
for(int j=js ; j<js +m/2; j++) {
tmp[i][j]= arr[i][j+m/2];
}
}
arr=tmp;
}
public static void six() {
int[][]tmp = new int[n][m];
int is = n/2;
int js =0;
for(int i=is;i<is+n/2; i++) {
for(int j=js; j<js+m/2; j++) {
tmp[i][j]=arr[i-n/2][j];
}
}
is = n/2;
js =m/2;
for(int i=is;i<is+n/2; i++) {
for(int j=js; j<js+m/2; j++) {
tmp[i][j]=arr[i][j-m/2];
}
}
is =0;
js =m/2;
for(int i=is;i<is+n/2; i++) {
for(int j=js; j<js+m/2; j++) {
tmp[i][j]=arr[i+n/2][j];
}
}
is =0;
js =0;
for(int i=is;i<is+n/2; i++) {
for(int j=js; j<js+m/2; j++) {
tmp[i][j]=arr[i][j+m/2];
}
}
arr = tmp;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
arr = new int[n][m];
for(int i=0; i<n; i++) {
st = new StringTokenizer(br.readLine());
for(int j =0; j<m; j++) {
arr[i][j]= Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine());
for(int i=0; i<r; i++) {
int k = Integer.parseInt(st.nextToken());
if(k==1) {
one();
}
else if(k==2) {
two();
}
else if(k==3) {
three();
}
else if(k==4) {
four();
}
else if(k==5) {
five();
}
else if(k==6) {
six();
}
}
print();
}
}
'백준 > 구현' 카테고리의 다른 글
배열돌리기1-16926번(java) (0) | 2021.08.11 |
---|---|
배열돌리기3-16935(java) (0) | 2021.08.11 |
색종이- 2563번(java) (0) | 2021.08.11 |
요세푸스 문제 - 1158번 (java) (0) | 2021.08.11 |
스위치 켜고 끄기 (0) | 2021.08.02 |