-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_1824_혁진이의프로그램검증_유승아.java
171 lines (142 loc) · 3.19 KB
/
Solution_1824_혁진이의프로그램검증_유승아.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Solution_1824_혁진이의프로그램검증_유승아 {
static int R, C;
static char[][] arr;
static String[][] visit;
static int[] dy = { -1, 1, 0, 0 };
static int[] dx = { 0, 0, -1, 1 }; // 상하좌우
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
arr = new char[R][C];
visit = new String[R][C];
for (int i = 0; i < R; i++) {
String str = br.readLine();
for (int j = 0; j < C; j++) {
arr[i][j] = str.charAt(j);
visit[i][j] = "";
}
} // input
// 0 상 / 1 하 / 2 좌 / 3 우
boolean isSuccess = solve();
System.out.println("#" + tc + " " + (isSuccess ? "YES" : "NO"));
} // end of tc
}// end of main
// 이게 되겟니?
static public boolean solve() {
// 0 상 / 1 하 / 2 좌 / 3 우
Queue<pair> q = new LinkedList<>();
// visit[y][x] += "*"+M+dir+"*";
q.add(new pair(0, 0, 3, 0));
while (!q.isEmpty()) {
pair p = q.poll();
int dir = p.dir;
int tmpmem = p.mem;
char now = arr[p.y][p.x];
if (now != '?') {
switch (now) {
case '<':
dir = 2;
break;
case '>':
dir = 3;
break;
case '^':
dir = 0;
break;
case 'v':
dir = 1;
break;
case '_':
if (tmpmem == 0)
dir = 3;
else
dir = 2;
break;
case '|':
if (tmpmem == 0)
dir = 1;
else
dir = 0;
break;
case '@': // 프로그램 끝!!
return true;
case '+':
if (tmpmem == 15)
tmpmem = 0;
else
tmpmem++;
break;
case '-':
if (tmpmem == 0)
tmpmem = 15;
else
tmpmem--;
break;
default:
if ('0' <= now && now <= '9') {
tmpmem = now - '0';
break;
}
}
String log = "*" + tmpmem + dir + "*";
if (visit[p.y][p.x].contains(log)) {
} else {
visit[p.y][p.x] += log;
int ny = p.y + dy[dir];
int nx = p.x + dx[dir];
if (ny > R - 1)
ny = 0;
else if (ny < 0)
ny = R - 1;
if (nx > C - 1)
nx = 0;
else if (nx < 0)
nx = C - 1;
q.add(new pair(ny, nx, dir, tmpmem));
}
} else { // '?'인 경우
for (int d = 0; d < 4; d++) {
String log = "*" + tmpmem + d + "*";
if (visit[p.y][p.x].contains(log)) {
} else {
visit[p.y][p.x] += log;
int ny = p.y + dy[d];
int nx = p.x + dx[d];
if (ny > R - 1)
ny = 0;
else if (ny < 0)
ny = R - 1;
if (nx > C - 1)
nx = 0;
else if (nx < 0)
nx = C - 1;
q.add(new pair(ny, nx, d, tmpmem));
}
}
}
} // end of while
return false;
} // end of solve
public static class pair {
int y;
int x;
int dir;
int mem;
public pair(int y, int x, int dir, int mem) {
super();
this.y = y;
this.x = x;
this.dir = dir;
this.mem = mem;
}
}
}