https://www.acmicpc.net/problem/14891
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class topni {
int topnum;
int dir;
topni(int topnum, int dir) {
this.topnum = topnum;
this.dir = dir;
}
}
public class Main {
static int[][] arr;
static Queue<topni> q;
static boolean[] visited;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
arr = new int[4][8];
q = new LinkedList<topni>();
for (int i = 0; i < arr.length; i++) {
String a = sc.nextLine();
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = a.charAt(j) - '0';
}
}
int casecnt = sc.nextInt();
for (int i = 0; i < casecnt; i++) {
visited = new boolean[4];
int topnum = sc.nextInt() - 1;
int dir = sc.nextInt();
q.add(new topni(topnum, dir));
visited[topnum] = true;
bfs();
}
int result = 0;
for (int i = 0; i < 4; i++) {
if (arr[i][0] == 1) {
if (i == 0) {
result = result + 1;
} else if (i == 1) {
result = result + 2;
} else if (i == 2) {
result = result + 4;
} else if (i == 3) {
result = result + 8;
}
}
}
System.out.println(result);
}
private static void bfs() {
while (!q.isEmpty()) {
topni qtemp = q.poll();
int topnum = qtemp.topnum;
int dir = qtemp.dir;
check(topnum, dir);
int[] temp = new int[8];
if (dir == 1) {
temp[0]=arr[topnum][7];
for (int j = 1; j <= 7; j++) {
temp[j] = arr[topnum][j - 1];
}
} else {
temp[7]=arr[topnum][0];
for (int j = 6; j >= 0; j--) {
temp[j]=arr[topnum][j+1];
}
}
for(int i=0;i<8;i++) {
arr[topnum][i]=temp[i];
}
/*
* System.out.print(topnum+ ": "); for(int i=0;i<8;i++) {
* System.out.print(arr[topnum][i]+" "); } System.out.println();
*/
}
}
private static void check(int topnum, int dir) {
if (topnum != 0 && topnum != 3) {
if ((arr[topnum][2] != arr[topnum + 1][6]) && visited[topnum + 1] == false) {
visited[topnum]=true;
q.add(new topni(topnum + 1, dir * -1));
}
if ((arr[topnum][6] != arr[topnum - 1][2]) && visited[topnum - 1] == false) {
visited[topnum]=true;
q.add(new topni(topnum - 1, dir * -1));
}
} else if (topnum == 0) {
if ((arr[topnum][2] != arr[topnum + 1][6]) && visited[topnum + 1] == false) {
visited[topnum]=true;
q.add(new topni(topnum + 1, dir * -1));
}
} else {
if ((arr[topnum][6] != arr[topnum - 1][2]) && visited[topnum - 1] == false) {
visited[topnum]=true;
q.add(new topni(topnum - 1, dir * -1));
}
}
}
}
'코딩테스트(백준)' 카테고리의 다른 글
[백준] 15686 : 치킨 배달 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
---|---|
[백준] 14502 : 연구소 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
[백준] 16236 : 아기 상어 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
[백준] 14889 : 스타트와 링크 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
[백준] 17144: 미세먼지 안녕! - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |