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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; ++i)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;
const int maxn = 500010;
template <typename NBXIN>
NBXIN read() {
NBXIN s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * f;
}
int n, m, q;
vector<int> mat[maxn];
struct VAL {
int pos[4];
};
vector<VAL> val[maxn];
pair<int, int> dl[4] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
#define UP 2
#define DOWN 0
#define LEFT 1
#define RIGHT 3
#define nxtx x + dl[(dir + 1) % 4].first
#define nxty y + dl[(dir + 1) % 4].second
#define gox x + dl[dir].first
#define goy y + dl[dir].second
void go(int dir, int &x, int &y) {
x += dl[dir].first;
y += dl[dir].second;
}
int cnt = 1;
void getAns(int dir, int x, int y, int xx, int yy) {
// while (mat[nxtx][nxty] != 1) {
// val[gox][goy].pos[dir] = cnt;
// go(dir, x, y);
// cnt++;
// }
bool flg = 1;
while (1) {
// cout << x << ' ' << y << ' ' << cnt << endl;
flg = 0;
if (mat[gox][goy] == 1 || mat[nxtx][nxty] != 1) {
if (mat[nxtx][nxty] != 1) {
dir++;
dir = (dir + 4) % 4;
} else {
dir--;
dir = (dir + 4) % 4;
}
}
if (mat[gox][goy] != 1) {
if (val[gox][goy].pos[dir]) return;
val[gox][goy].pos[dir] = cnt;
go(dir, x, y);
cnt++;
}
}
}
int main() {
freopen("pokemon.in", "r", stdin);
freopen("pokemon.out", "w", stdout);
n = read<int>();
m = read<int>();
For(i, 0, m + 1) {
mat[0].push_back(1);
mat[n + 1].push_back(1);
}
int pp = 0, ppp = 0;
For(i, 1, n) {
mat[i].push_back(1);
val[i].resize(m + 1);
For(j, 1, m) {
char ch = getchar();
while (ch != '.' && ch != 'X') {
ch = getchar();
}
if (ch == '.') {
mat[i].push_back(0);
if (pp == 0) {
pp = i;
ppp = j;
}
} else {
mat[i].push_back(1);
}
}
mat[i].push_back(1);
}
q = read<int>();
getAns(RIGHT, pp, ppp, pp, ppp);
For(i, 1, n) {
For(j, 1, m) { cout << setw(4) << val[i][j].pos[0]; }
cout << endl;
}
cout << endl;
For(i, 1, n) {
For(j, 1, m) { cout << setw(4) << val[i][j].pos[1]; }
cout << endl;
}
cout << endl;
For(i, 1, n) {
For(j, 1, m) { cout << setw(4) << val[i][j].pos[2]; }
cout << endl;
}
cout << endl;
For(i, 1, n) {
For(j, 1, m) { cout << setw(4) << val[i][j].pos[3]; }
cout << endl;
}
cout << endl;
int ccnt = cnt - 1;
For(i, 1, q) {
int x, y, xx, yy;
x = read<int>();
y = read<int>();
xx = read<int>();
yy = read<int>();
char ch = getchar();
while (!isalpha(ch)) {
ch = getchar();
}
// cout <<ccnt << endl;
if (ch == 'U') {
go(UP, x, y);
int minn = 0x3f3f3f3f;
For(i, 0, 3) {
if (val[xx][yy].pos[i]) {
int tmp = val[xx][yy].pos[i];
if (tmp <
(val[x][y].pos[UP] - 1 == 0 ? ccnt : val[x][y].pos[UP] - 1)) {
tmp += ccnt;
}
minn = min(minn, tmp);
}
}
// minn++;
cout << minn - (val[x][y].pos[UP] - 1 == 0 ? ccnt : val[x][y].pos[UP] - 1)
<< endl;
}
if (ch == 'D') {
go(DOWN, x, y);
int minn = 0x3f3f3f3f;
For(i, 0, 3) {
if (val[xx][yy].pos[i]) {
int tmp = val[xx][yy].pos[i];
if (tmp <
(val[x][y].pos[DOWN] - 1 == 0 ? ccnt : val[x][y].pos[DOWN] - 1)) {
tmp += ccnt;
}
minn = min(minn, tmp);
}
}
// minn++;
cout << minn - (val[x][y].pos[DOWN] - 1 == 0 ? ccnt
: val[x][y].pos[DOWN] - 1)
<< endl;
}
if (ch == 'L') {
go(LEFT, x, y);
int minn = 0x3f3f3f3f;
For(i, 0, 3) {
if (val[xx][yy].pos[i]) {
int tmp = val[xx][yy].pos[i];
if (tmp <
(val[x][y].pos[LEFT] - 1 == 0 ? ccnt : val[x][y].pos[LEFT] - 1)) {
tmp += ccnt;
}
minn = min(minn, tmp);
}
}
// minn++;
// cout << val[x][y].pos[LEFT] << endl;
cout << minn - (val[x][y].pos[LEFT] - 1 == 0 ? ccnt
: val[x][y].pos[LEFT] - 1)
<< endl;
}
if (ch == 'R') {
go(RIGHT, x, y);
int minn = 0x3f3f3f3f;
For(i, 0, 3) {
if (val[xx][yy].pos[i]) {
int tmp = val[xx][yy].pos[i];
if (tmp < (val[x][y].pos[RIGHT] - 1 == 0
? ccnt
: val[x][y].pos[RIGHT] - 1)) {
tmp += ccnt;
}
minn = min(minn, tmp);
}
}
// minn++;
cout << minn - (val[x][y].pos[RIGHT] - 1 == 0 ? ccnt
: val[x][y].pos[RIGHT] - 1)
<< endl;
}
// cout << cnt << endl;
}
return 0;
}
|