Home G15662. 톱니바퀴(2)
Post
Cancel

G15662. 톱니바퀴(2)

문제

image


제출 코드

image


같은 문제가 있어 이걸 풀고 입출력 값만 바꿔서 다시 제출함


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
package gold;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class G15662_gear2 {
	static int gn;
	static class Turn{
		int g, d;
		Turn(int g, int d){
			this.g = g;
			this.d = d;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		gn = Integer.parseInt(br.readLine());
		char[][] gear = new char[gn][8];
		int[][] p = new int[gn][2];
		for(int i=0; i<gn; i++) {
			gear[i] = br.readLine().toCharArray();
			p[i][0] = 6;	// 왼쪽 맞닿는 부분 (pointer)
			p[i][1] = 2;	// 오른쪽 맞닿는 부분 (pointer)
		}
		int K = Integer.parseInt(br.readLine());
		for(int k=0; k<K; k++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int stGear = Integer.parseInt(st.nextToken())-1;
			int dir = Integer.parseInt(st.nextToken());

			Queue<Turn> queue = new LinkedList<>();
			queue.add(new Turn(stGear, dir));

			boolean[] visit = new boolean[gn];
			while(!queue.isEmpty()) {
				Turn n = queue.poll();
				visit[n.g] = true;

				int[] sides = {-1, 1}; //left & right
				for(int s : sides) {
					int side = n.g + s;
					if(side>=0 && side<gn && !visit[side] && gear[side][p[side][(s==-1)?1:0]]!=gear[n.g][p[n.g][(s==-1)?0:1]])
						queue.add(new Turn(side, n.d*(-1)));
				}
				p[n.g][0] = (p[n.g][0]-n.d+8)%8;
				p[n.g][1] = (p[n.g][1]-n.d+8)%8;
			}
		}
		int result = 0;
		for(int i=0; i<gn; i++) {
			int top = (p[i][1]-2+8)%8;
			if(gear[i][top]=='1') result ++;
		}
		System.out.println(result);
	}

}
This post is licensed under CC BY 4.0 by the author.