[백준] 17406. 배열돌리기4 (JAVA)
728x90
반응형
거의 인내심 테스트 문제..
SW역량 어렵게 나오면 이렇게..? 시간 많이 잡아먹게 나올듯
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int n,m,k;
static int map[][];
static int n_map[][];
static int r,c,s;
static int ypos[]= {0,1,0,-1};
static int xpos[]= {1,0,-1,0};
static int sum=Integer.MAX_VALUE;
static int[][] command;
static boolean[] visited;
static int[] num;
static int[] target;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st=new StringTokenizer(br.readLine());
n=Integer.parseInt(st.nextToken());
m=Integer.parseInt(st.nextToken());
k=Integer.parseInt(st.nextToken());
map=new int[n][m];
command=new int[k][3];
target=new int[k];
for (int i = 0; i < k; i++) {
target[i]=i;
}
num=new int[k];
visited=new boolean[k];
for (int i = 0; i < n; i++) {
st=new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
map[i][j]=Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < k; i++) {
st=new StringTokenizer(br.readLine());
command[i][0]=Integer.parseInt(st.nextToken());
command[i][1]=Integer.parseInt(st.nextToken());
command[i][2]=Integer.parseInt(st.nextToken());
}
nPr(0);
System.out.println(sum);
}
private static void nPr(int cnt) {
if(cnt==k) {
n_map=deepCopy();
for(int x:num) {
r=command[x][0];
c=command[x][1];
s=command[x][2];
rotate(r-s-1,c-s-1,2*s);
}
for (int w = 0; w < n; w++) {
int temp=0;
for (int j = 0; j < m; j++) {
temp+=n_map[w][j];
}
if(temp<sum)sum=temp;
}
return;
}
for (int i = 0; i < k; i++) {
if(visited[i])continue;
visited[i]=true;
num[cnt]=target[i];
nPr(cnt+1);
visited[i]=false;
}
}
private static int[][] deepCopy(){
int[][] res=new int[n][m];
for (int i = 0; i < n; i++) {
System.arraycopy(map[i], 0, res[i], 0, m);
}
return res;
}
private static void rotate(int i, int j, int len) {
int x=j;
int y=i;
while(len>=2) {
int cnt=0;
int dir=0;
int temp=n_map[y][x];
while(cnt!=4*len) {
int yy=y+ypos[dir];
int xx=x+xpos[dir];
if(xx<j || yy<i || xx>j+len || yy>i+len) {
dir=(dir+1)%4;
continue;
}
int temp2=n_map[yy][xx];
n_map[yy][xx]=temp;
temp=temp2;
y=yy;
x=xx;
cnt+=1;
}
len-=2;
x=++j;
y=++i;
}
}
}
728x90
반응형
'백준' 카테고리의 다른 글
[백준] 3040번 백설 공주와 일곱 난쟁이 (0) | 2021.02.15 |
---|---|
[백준] 2961번 도영이가 만든 맛있는 음식 (java, 부분 집합 subset) (0) | 2021.02.15 |
[백준] 16935번 배열돌리기3 (JAVA, C++) (0) | 2021.02.10 |
[백준] 16926번 배열돌리기1 (java) (0) | 2021.02.10 |
[백준] 1592번 영식이와 친구들 (java) (0) | 2021.02.09 |
TAGS.