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
| #include <bits/stdc++.h>
using namespace std;
struct Location {
int row;
int col;
int way; // 1~4 위 오 아 왼
};
char eumap[25][25], answer;
int R, C;
Location M, Z, blank;
list<Location> q;
bool findBlank(int row, int col); //찾으면 true
bool goNext(Location cur); // 빈칸 발견시 false. 나머지 true
Location nextCell(Location c);
void fillBlank();
int main() {
cin >> R >> C;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) {
cin >> eumap[i][j];
if (eumap[i][j] == 'M')
M = { i, j, 0 };
else if (eumap[i][j] == 'Z')
Z = { i, j, 0 };
}
if (!findBlank(M.row, M.col))
findBlank(Z.row, Z.col);
fillBlank();
cout << blank.row + 1 << " " << blank.col + 1 << " " << answer << endl;
}
bool findBlank(int r, int c) {
if (r > 0 && eumap[r - 1][c] != '.')
q.push_back({ r, c, 1 });
if (c < C - 1 && eumap[r][c + 1] != '.')
q.push_back({ r, c, 2 });
if (r < R -1 && eumap[r + 1][c] != '.')
q.push_back({ r, c, 3 });
if (c > 0 && eumap[r][c - 1] != '.')
q.push_back({ r, c, 4 });
while (!q.empty()) {
if (goNext(q.front()))
q.pop_front();
else {
q.clear();
return true;
}
}
q.clear();
return false;
}
bool goNext(Location cur) {
cur = nextCell(cur);
int r = cur.row, c = cur.col, w = cur.way;
char t = eumap[r][c];
if (t == '.') {
blank = { r, c, w };
return false;
}
if (w == 1 || w == 3) {
if (t == '|' || t == '+')
q.push_back({ r , c, w });
if (t == '1' || t == '2' || t == '+')
q.push_back({ r , c, 2 });
if (t == '4' || t == '3' || t == '+')
q.push_back({ r , c, 4 });
}
else {
if (t == '-' || t == '+')
q.push_back({ r, c , w });
if (t == '3' || t == '2' || t == '+')
q.push_back({ r, c , 1 });
if (t == '4' || t == '1' || t == '+')
q.push_back({ r, c , 3 });
}
return true;
}
Location nextCell(Location cur) {
int r = cur.row, c = cur.col, w = cur.way;
if (w == 1 && r > 0)
return { r - 1, c, w };
if (w == 2 && c < C - 1)
return { r, c + 1, w };
if (w == 3 && r < R - 1)
return { r + 1, c, w };
if (w == 4 && c > 0)
return { r, c - 1,w};
}
void fillBlank() {
int r = blank.row, c = blank.col, w = blank.way;
bool check[4] = { 0,0,0,0 };
if (r > 0) {
char t = eumap[r - 1][c];
check[0] = t != '|' ? t != '1' ? t != '4' ? t != '+' ? false : true : true : true : true;
}
if (c < C - 1) {
char t = eumap[r][c + 1];
check[1] = t != '-' ? t != '4' ? t != '3' ? t != '+' ? false : true : true : true : true;
}
if (r < R - 1) {
char t = eumap[r + 1][c];
check[2] = t != '|' ? t != '2' ? t != '3' ? t != '+' ? false : true : true : true : true;
}
if (c > 0) {
char t = eumap[r][c-1];
check[3] = t != '-' ? t != '2' ? t != '1' ? t != '+' ? false : true : true : true : true;
}
if (check[0] && check[1] && check[2] && check[3])
answer = '+';
else if (check[0] && check[2])
answer = '|';
else if (check[1] && check[3])
answer = '-';
else if (check[1] && check[2])
answer = '1';
else if (check[0] && check[1])
answer = '2';
else if (check[0] && check[3])
answer = '3';
else if (check[2] && check[3])
answer = '4';
}
|