https://suhyeokeee.tistory.com/34
이전 포스팅과 똑같은 알고리즘이기 때문에 설명을 적진 않겠다.
설명이 필요하다면 위의 링크를 보길 바란다.
위 문제와 똑같다.
다만 다른것 이라면 톱니바퀴의 개수가 4개가 아니라 t개라는 것
이것만 바꿔주면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
public class Main {
static int t,k;
static int map[][];
static boolean[] visited ;
static int copy_map[][];
static int sum=0;
static Queue<Node> q = new LinkedList<>();
static ArrayList<Node>list = new ArrayList<>();
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
t = Integer.parseInt(br.readLine());
map = new int[t+1][9];
copy_map = new int[t+1][9];
for(int i=1; i<=t; i++) {
String str = br.readLine();
for(int j=1; j<=8; j++) {
map[i][j] = str.charAt(j-1)-'0';
}
}
k = Integer.parseInt(br.readLine());
for(int i=0; i<k; i++) {
String[] t = br.readLine().split(" ");
int num = Integer.parseInt(t[0]);
int d = Integer.parseInt(t[1]);
q.add(new Node(num,d));
}
solve();
}
public static void solve() {
while(!q.isEmpty()) {
visited= new boolean[t+1];
Node a= q.poll();
int num = a.num;
int d = a.d;
visited[num]=true;
can_turn(num,d);
}
}
for(int i=1; i<=t; i++) {
if(map[i][1]==1) {
sum++;
}
}
System.out.println(sum);
}
public static void turn(int num, int d) {
for(int i=1; i<=t; i++) {
for(int j=1; j<=8; j++) {
copy_map[i][j]=map[i][j];
}
}
if(d==1) {
for(int i=1; i<=8; i++) {
if(i==1) {
map[num][1] = copy_map[num][8];
}
else {
map[num][i] = copy_map[num][i-1];
}
}
}
else {
for(int i=8; i>=1; i--) {
if(i==8) {
map[num][8]=copy_map[num][1];
}
else {
map[num][i] = copy_map[num][i+1];
}
}
}
}
public static void can_turn(int num, int d) {
list.add(new Node(num,d));
if(num-1>=1 && !visited[num-1] && map[num][7] != map[num-1][3]) {
visited[num-1] = true;
can_turn(num-1,d*-1);
}
if(num+1<=t && !visited[num+1] && map[num][3] != map[num+1][7]) {
visited[num+1] = true;
can_turn(num+1,d*-1);
}
}
}
class Node{
int num,d;
Node(int num, int d){
this.num=num;
this.d=d;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'알고리즘' 카테고리의 다른 글
[백준 14890] 경사로 -JAVA // le_effort// (0) | 2020.03.12 |
---|---|
[백준 14503] 로봇 청소기 -JAVA //le_effort// (0) | 2020.03.12 |
[백준 1316] 그룹 단어 체커 -JAVA //le_effort// (0) | 2020.03.11 |
[백준 14891] 톱니바퀴 -JAVA //le_effort// (0) | 2020.03.11 |
[백준 14499] 주사위 굴리기 -JAVA //le_effort// (0) | 2020.03.11 |