-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoj0210.cpp
More file actions
93 lines (86 loc) · 1.81 KB
/
aoj0210.cpp
File metadata and controls
93 lines (86 loc) · 1.81 KB
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;
int W, H, ppl;
char m[30][30];
string d = "WSEN";
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
bool canMove(int h, int w) {
if(0 <= h && 0 <= w && h < H && w < W)
if(m[h][w] == '.' || m[h][w] == 'X')
return true;
return false;
}
char decideDirection(int h, int w, char d2) {
REP(i, 4) {
int nd = (d.find(d2) + 3 + i) % 4;
int nh = h + dy[nd], nw = w + dx[nd];
if(canMove(nh, nw)) return d[nd];
}
return d2;
}
int main() {
while(cin >> W >> H, W|H) {
ppl = 0;
REP(i, H) REP(j, W) {
cin >> m[i][j];
if(d.find(m[i][j]) != string::npos)
ppl++;
}
int cnt = 0;
while(ppl) {
REP(i, H) REP(j, W) {
if(d.find(m[i][j]) != string::npos) {
m[i][j] = decideDirection(i, j ,m[i][j]);
}
}
bool chk[30][30];
memset(chk, true, sizeof(chk));
REP(i, 4) REP(j, H) REP(k, W) {
if(m[j][k] == d[i] && chk[j][k]) {
int nh = j + dy[i], nw = k + dx[i];
if(m[nh][nw] == '.' && chk[nh][nw]) {
swap(m[j][k], m[nh][nw]);
chk[nh][nw] = false;
}
else if(m[nh][nw] == 'X' && chk[nh][nw]) {
m[j][k] = '.';
chk[nh][nw] = false;
ppl--;
}
chk[j][k] = false;
}
}
cnt++;
if(cnt > 180) break;
//debug
// REP(i, H) {
// REP(j, W)
// cout << m[i][j];
// cout << endl;
// }
}
if(cnt > 180) cout << "NA" << endl;
else cout << cnt << endl;
}
return 0;
}