Problem:
ザ・スクエアーズ
この度、有名なテーマパークに、巨大迷路ザ・スクエアーズが新しく完成しました。 消防署の指導により避難訓練をしなければなりませんが、巨大迷路なだけに訓練にかかる時間を予測することができません。そこで、あなたは以下の仕様をもとに避難訓練シミュレータを開発することになりました。
巨大迷路は図 1 に示すように、横
W
、縦
H
の
W × H
個のマス目で表わされます。各マス目は、通路(白いマス目)、壁(茶色いマス目)  、非常口(緑のマス目)のいずれかです。図中の○は人を表し、その中の英小文字(E、W、S、N)はその人が向いている方角(東西南北)を表しています。図は上方向が北になるように描かれています。
図1
巨大迷路内にいる人は最初、東西南北のいずれかの方向を向いて立っています。各人は 1 秒単位で同時に次に示す手順で移動を試みます。
現在向いている方向の、右、前、左、後のマス目を順番に調べ、最初に見つけた、空いている通路または非常口の方向に向きを変えます。そのようなマス目が無い場合は向きを変えません。
目の前のマス目が空いていて、他の人の目の前のマス目になっていない場合は移動します。同じマス目を目の前のマスとする人が複数いる場合は、そのマス目の、東、北、西、南のマス目にいる人の順で選択された 1 人が移動します。
移動後に非常口に到着した人は、無事避難し迷路内から消えます。
与えられた巨大迷路と人の位置情報を入力とし、全ての人が避難し終える時間を出力するプログラムを作成してください。   脱出に 180 秒より長い時間を要する場合は NA と出力して下さい。  迷路と人の位置情報は、
H
行
W
列の文字によって与えられます。各文字の意味は以下のとおりです。
#
: 壁
.
: 床
X
: 非常口
E
: 東を向いている人
N
: 北を向いている人
W
: 西を向いている人
S
: 南を向いている人
なお、迷路と外部との境界は壁
#
または非常口
X
のいずれかです。また、巨大迷路の中には、人が必ず１人以上います。
Input
複数のデータセットの並びが入力として与えられます。      入力の終わりはゼロふたつの行で示されます。
各データセットは以下の形式で与えられます。
W
H
str
1
str
2
:
str
H
1 行目に迷路の横方向の大きさ
W
、縦方向の大きさ
H
(1 ≤
W, H
≤ 30) が与えられます。続く
H
行に迷路の
i
行目を表す文字列
str
i
(長さ
W
) が与えられます。
データセットの数は 50 を超えません。
Output
入力データセットごとに、全ての人が避難し終える時間を１行に出力します。
Sample Input
10 3
##########
#E.......X
##########
4 4
####
#N.#
#..X
####
5 5
#####
#N..#
###.X
#S..#
#####
6 6
######
#..#X#
#.EE.#
####N#
#....#
######
8 8
##X#####
#....E.#
#####.##
#.#...##
#.W.#..#
#.#.N#.X
#X##.#.#
########
0 0
Output for the Sample Input
8
NA
9
16
10

Submitted solutions:

code_snippet: #include <string>
#include <vector>
#include <iostream>
using namespace std;
string ds = "ENWS";
int dx[4] = { 0, -1, 0, 1 };
int dy[4] = { 1, 0, -1, 0 };
int H, W; string s[33]; bool flag[33][33];
int main() {
	while (cin >> W >> H, H | W) {
		for (int i = 0; i < H; i++) cin >> s[i];
		int ret = -1;
		for (int i = 1; i <= 180; i++) {
			for (int j = 0; j < H; j++) {
				for (int k = 0; k < W; k++) {
					flag[j][k] = false;
					int pd = ds.find(s[j][k]);
					if (pd == string::npos) continue;
					for (int l = 3; l < 7; l++) {
						int tx = j + dx[(pd + l) % 4], ty = k + dy[(pd + l) % 4];
						if (s[tx][ty] == '.' || s[tx][ty] == 'X') {
							s[j][k] = ds[(pd + l) % 4];
							break;
						}
					}
				}
			}
			for (int j = 0; j < H; j++) {
				for (int k = 0; k < W; k++) {
					if (s[j][k] == '.' || s[j][k] == 'X') {
						for (int l = 0; l < 4; l++) {
							int tx = j + dx[l], ty = k + dy[l];
							if (0 <= tx && tx < H && 0 <= ty && ty < W) {
								int pd = ds.find(s[tx][ty]);
								if (pd == string::npos || pd != (l + 2) % 4) continue;
								flag[tx][ty] = true;
								break;
							}
						}
					}
				}
			}
			for (int j = 0; j < H; j++) {
				for (int k = 0; k < W; k++) {
					if (flag[j][k]) {
						int pd = ds.find(s[j][k]);
						int tx = j + dx[pd], ty = k + dy[pd];
						if (s[tx][ty] == '.') swap(s[j][k], s[tx][ty]);
						else s[j][k] = '.';
					}
				}
			}
			int cnt = 0;
			for (int j = 0; j < H; j++) {
				for (int k = 0; k < W; k++) {
					if (ds.find(s[j][k]) != string::npos) cnt++;
				}
			}
			if (cnt == 0) {
				ret = i;
				break;
			}
		}
		if (ret == -1) cout << "NA" << endl;
		else cout << ret << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3344
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int w, h;
    while(cin >> w >> h, w){
        int ans = 181;
        vector<string> A(h);
        for(auto &&s:A)cin >> s;
        auto fin = [&](){
            int cnt = 0;
            for(auto &&s:A){
                cnt += count(s.begin(), s.end(), 'E');
                cnt += count(s.begin(), s.end(), 'N');
                cnt += count(s.begin(), s.end(), 'W');
                cnt += count(s.begin(), s.end(), 'S');
            }
            return cnt == 0;
        };
        vector<char> C = {'E', 'N', 'W', 'S'};
        map<char,int> mp = {{'E', 0}, {'N', 1}, {'W', 2}, {'S', 3}};
        vector<pair<int,int>> dir = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};//進む方向
        vector<vector<vector<bool>>> mov;
        auto ope = [&](int y, int x){
            int dy, dx, s = mp[A[y][x]];
            for(int i = 0; i < 4; i++){
                tie(dy, dx) = dir[(i + s + 3) & 3];
                if(A[y + dy][x + dx] == '#')continue;
                if(A[y + dy][x + dx] == 'E')continue;//ここらへん微妙
                if(A[y + dy][x + dx] == 'N')continue;
                if(A[y + dy][x + dx] == 'S')continue;
                if(A[y + dy][x + dx] == 'W')continue;
                A[y][x] = C[(i + s + 3) & 3];
                break;
            }
            s = mp[A[y][x]];
            tie(dy, dx) = dir[s];
            if(A[y + dy][x + dx] == '.' || A[y + dy][x + dx] == 'X'){
                mov[y + dy][x + dx][(s + 2) & 3] = true;
            }
        };
        for(int i = 0; i <= 180; i++){
            if(fin()){
                ans = i;
                break;
            }
            mov = vector<vector<vector<bool>>>(h, vector<vector<bool>>(w, vector<bool>(4)));
            for(int y = 0; y < h; y++){
                for(int x = 0; x < w; x++){
                    if(A[y][x] == '#')continue;
                    if(A[y][x] == 'X')continue;
                    if(A[y][x] == '.')continue;
                    ope(y, x);
                }
            }
            for(int y = 0; y < h; y++){
                for(int x = 0; x < w; x++){
                    for(int j = 0; j < 4; j++){
                        if(mov[y][x][j]){
                            int dy, dx;
                            tie(dy, dx) = dir[j];
                            if(A[y][x] == '.'){
                                swap(A[y][x], A[y + dy][x + dx]);
                            }else{
                                A[y + dy][x + dx] = '.';
                            }
                            break;
                        }
                    }
                }
            }
        }
        if(ans == 181)cout << "NA" << '\n';
        else cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3620
final_rank: 20

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

void solve(int h, int w){

    vector<string> G(h);
    vector<vector<char>> out(h, vector<char>(w, 0));
    array<int, 4> dy{0, 1, 0, -1}, dx{-1, 0, 1, 0};
    string D = "WSEN";
    for (int i = 0; i < h; ++i) {
        cin >> G[i];
    }
    for (int t = 0; t <= 180; ++t) {
        int cnt = 0;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                for (int k = 0; k < 4; ++k) {
                    if(G[i][j] == D[k]) cnt++;
                }
            }
        }
        if(!cnt){
            cout << t << "\n";
            return;
        }
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                for (int i = 0; i < 4; ++i) {
                    if(G[y][x] == D[i]){
                        for (int j = 3; j < 7; ++j) {

                            int dir = (i+j)&3;
                            if(G[y+dy[dir]][x+dx[dir]] == '.' || G[y+dy[dir]][x+dx[dir]] == 'X'){
                                G[y][x] = D[dir];
                                i = 10;
                                break;
                            }
                        }
                    }
                }
            }
        }
        for (int dir = 0; dir < 4; ++dir) {
            for (int y = 0; y < h; ++y) {
                 for (int x = 0; x < w; ++x) {
                    if(G[y][x] == D[dir]){
                        if(G[y+dy[dir]][x+dx[dir]] == '.'){
                            G[y+dy[dir]][x+dx[dir]] = G[y][x]-'A'+'a';
                            G[y][x] = '|';
                        }else if(G[y+dy[dir]][x+dx[dir]] == 'X'){
                            G[y][x] = '|';
                            G[y+dy[dir]][x+dx[dir]] = '$';
                        }
                    }
                }
            }
        }
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if(islower(G[i][j])){
                    G[i][j] = G[i][j]-'a'+'A';
                }else if(G[i][j] == '$'){
                    G[i][j] = 'X';
                }else if(G[i][j] == '|'){
                    G[i][j] = '.';
                }
            }
        }

    }
    puts("NA");
}

int main() {
    int w, h;
    while(cin >> w >> h, h){
        solve(h, w);
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3004
final_rank: 14

code_snippet: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int w, h;
char s[30][31], b[30][31];
map<char, int>d{ {'N',0},{'E',1},{'S',2},{'W',3} };
char rd[] = "NESW";
int dx[]{ -1,0,1,0 }, dy[]{ 0,1,0,-1 }, a[]{ 1,0,3,2 };
int main() {
	while (scanf("%d%d", &w, &h), h) {
		rep(i, h)scanf("%s", s[i]);
		for (int i = 0; i <= 180; i++) {
			bool ok = true;
			rep(i, h)rep(j, w)for (auto p : d) {
				if (s[i][j] == p.first) { ok = false; break; }
			}
			if (ok) { printf("%d\n", i); goto g; }
			rep(i, h)rep(j, w) {
				if (d.find(s[i][j]) == d.end())continue;
				int b = d[s[i][j]];
				rep(k, 4) {
					int nx = i + dx[(b + a[k]) % 4], ny = j + dy[(b + a[k]) % 4];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w && (s[nx][ny] == '.' || s[nx][ny] == 'X')) {
						s[i][j] = rd[(b + a[k]) % 4]; break;
					}
				}
			}
			rep(i, h)rep(j, w)b[i][j] = s[i][j];
			rep(i, h)rep(j, w) {
				if (s[i][j] != 'X'&&s[i][j] != '.')continue;
				rep(k, 4) {
					int nx = i + dx[a[k]], ny = j + dy[a[k]];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&d.find(s[nx][ny]) != d.end() && nx + dx[d[s[nx][ny]]] == i&&ny + dy[d[s[nx][ny]]] == j) {
						if (s[i][j] == 'X')b[nx][ny] = '.';
						else swap(b[i][j], b[nx][ny]);
						break;
					}
				}
			}
			rep(i, h)rep(j, w)s[i][j] = b[i][j];
		}
		puts("NA");
	g:;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3160
final_rank: 19

code_snippet: #include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

int w, h;
char s[30][31], b[30][31];
map<char, int>dir{ {'N',0},{'E',1},{'S',2},{'W',3} };
char rd[] = "NESW";
int dx[]{ -1,0,1,0 }, dy[]{ 0,1,0,-1 }, a[]{ 1,0,3,2 };
int main() {
	while (scanf("%d%d", &w, &h), h) {
		rep(i, h)scanf("%s", s[i]);
		for (int i = 0; i <= 180; i++) {
			bool ok = true;
			rep(i, h)rep(j, w)for (auto p : dir) {
				if (s[i][j] == p.first) { ok = false; break; }
			}
			if (ok) {
				printf("%d\n", i); goto g;
			}
			rep(i, h)rep(j, w) {
				if (dir.find(s[i][j]) == dir.end())continue;
				int bs = dir[s[i][j]];
				rep(k, 4) {
					int nx = i + dx[(bs + a[k]) % 4], ny = j + dy[(bs + a[k]) % 4];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w && (s[nx][ny] == '.' || s[nx][ny] == 'X')) {
						s[i][j] = rd[(bs + a[k]) % 4]; break;
					}
				}
			}
			rep(i, h)rep(j, w)b[i][j] = s[i][j];
			rep(i, h)rep(j, w) {
				if (s[i][j] != 'X'&&s[i][j] != '.')continue;
				rep(k, 4) {
					int nx = i + dx[a[k]], ny = j + dy[a[k]];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&dir.find(s[nx][ny]) != dir.end() && nx + dx[dir[s[nx][ny]]] == i&&ny + dy[dir[s[nx][ny]]] == j) {
						if (s[i][j] == 'X')b[nx][ny] = '.';
						else swap(b[i][j], b[nx][ny]);
						break;
					}
				}
			}
			rep(i, h)rep(j, w)s[i][j] = b[i][j];
		}
		puts("NA");
	g:;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3108
final_rank: 17

code_snippet: #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;
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define N 30
using namespace std;
int dy[4]={0,-1,0,1};
int dx[4]={1,0,-1,0};
int w,h,ans,cnt,sum;
bool used[N][N];
string s[N];


void Change_dir(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(!('0'<=s[i][j]&&s[i][j]<='3'))continue;
      int dir=s[i][j]-'0';
      for(int k=3;k<=6;k++){
	int ndir=(dir+k)%4;
	int ny=i+dy[ndir],nx=j+dx[ndir];
	if(ny<0||nx<0||h<=ny||w<=nx)continue;
	if(s[ny][nx]=='.'||s[ny][nx]=='X'){
	  s[i][j]=ndir+'0';
	  break;
	}
      }
    }
  }
}

void Change_pls(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(!(s[i][j]=='.'||s[i][j]=='X'))continue;
      if(used[i][j])continue;
      for(int k=0;k<4;k++){
	int ny=i+dy[k],nx=j+dx[k];
	if(ny<0||nx<0||h<=ny||w<=nx)continue;
	if(used[ny][nx])continue;
	if('0'<=s[ny][nx]&&s[ny][nx]<='3'){
	  if(s[ny][nx]!=(k+2)%4+'0')continue;
	  used[i][j]=used[ny][nx]=true;
	  if(s[i][j]=='.')s[i][j]=s[ny][nx];
	  else cnt++;
	  s[ny][nx]='.';
	  break;
	}
      }
    }
  }
}

int main(){
  while(1){
    cin>>w>>h;
    if(!w&&!h)break;
    ans=cnt=sum=0;
    for(int i=0;i<h;i++)cin>>s[i];
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
	if(s[i][j]=='E')s[i][j]='0',sum++;
	if(s[i][j]=='N')s[i][j]='1',sum++;
	if(s[i][j]=='W')s[i][j]='2',sum++;
	if(s[i][j]=='S')s[i][j]='3',sum++;
      }
    while(1){
      ans++;
      if(ans>180)break;
      Change_dir();
      memset(used,0,sizeof(used));
      Change_pls();
      if(cnt==sum)break;
    }
    if(ans>180)cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1208
final_rank: 10

code_snippet: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <unordered_set>
//#include <unordered_map>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
}
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
 
string s = "ENWS";
 
int main(){
    int w, h;
    while(cin >> w >> h, w || h){
        VV(char) vv(w, vector<char>(h));
        rep(i,h) rep(j,w) cin >> vv[j][i];
        int res = -1;
        rep(time, 180) {
            int man = 0;
            rep(j,h){
                rep(i,w){
                    rep(l,4){
                        if(vv[i][j]==s[l]){
                            man++;
                            rep(k,4) {
                                int d = (l+3+k)%4;
                                int xx = i+dx[d];
                                int yy = j+dy[d];
                                if (vv[xx][yy]=='X'||vv[xx][yy]=='.') {
                                    vv[i][j] = s[d];
                                    goto OUT;
                                }
                            }
                        }
                    }
                    OUT:;
                }
            }
            vector< vector<char> > next(w, vector<char>(h));
            next = vv;
            rep(i,w){
                rep(j,h){
                    if (vv[i][j] != '.' && vv[i][j] != 'X') continue;
                    rep(k,4){
                        int xx = i+dx[k];
                        int yy = j+dy[k];
                        if (xx<0||xx>=w||yy<0||yy>=h) continue;
 
                        if (vv[xx][yy] == s[(k+2)%4]){
                            if (vv[i][j] == '.')
                            next[i][j] = vv[xx][yy];
                            else
                            man--;
                            next[xx][yy] = '.';
                            break;
                        }
                    }
                }
            }
            if (man==0){
                res = time + 1;
                break;
            }
            vv = next;
        }
        if (res>=0){
          cout << res << endl;
        }
        else{
          cout << "NA" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1240
final_rank: 9

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <iterator>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

string s[30];
int h,w;
int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};

pair<int,int> NextPoint(int y,int x)
{
	pair<int,int> p;
	if(s[y][x] == 'E')
	{
		if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
		else if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';

			p.first = y;
			p.second = x+1;
			return p;
		}
		else if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
		else if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
	}
	else if(s[y][x] == 'N')
	{
		if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';

			p.first = y;
			p.second = x+1;
			return p;
		}
		else if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
		else if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
		else if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
	}
	else if(s[y][x] == 'W')
	{
		if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
		else if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
		else if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
		else if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';

			p.first = y;
			p.second = x+1;
			return p;
		}
	}
	else if(s[y][x] == 'S')
	{
		if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
		else if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
		else if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';
			p.first = y;
			p.second = x+1;
			return p;
		}
		else if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
	}
}

bool can(int y,int x)
{
	if(0 <= y & y < h && 0 <= x && x < w)
	{
		return true;
	}
	else return false;
}

bool isPerson(int y,int x)
{
	if(s[y][x] == 'E' || s[y][x] == 'W' || s[y][x] == 'S' || s[y][x] == 'N')
	{
		return true;
	}
	else return false;
}

bool onPerson(int h,int w)
{
	bool flag = false;

	rep(i,h)
	{
		rep(j,w)
		{
			if(s[i][j] == 'E' || s[i][j] == 'N' || s[i][j] == 'W' || s[i][j] == 'S')
			{
				flag = true;
			}
		}
	}

	return flag;
}

void printS(int h,int w)
{
	rep(i,h)
	{
		rep(j,w)
		{
			cout << s[i][j];
		}
		cout << endl;
	}
}

int main()
{
	map<pair<int,int>,int> cmp;

	cmp[pair<int,int>(0,1)] = 1;
	cmp[pair<int,int>(-1,0)] = 2;
	cmp[pair<int,int>(0,-1)] = 3;
	cmp[pair<int,int>(1,0)] = 4;


	while(cin >> w >> h)
	{
		if(w == 0 && h == 0) break;

		rep(i,h)
		{
			cin >> s[i];
		}

		if(!onPerson(h,w))
		{
			cout << 0 << endl;
			return 0;
		}


		bool flag = false;

		rep(k,180)
		{
			vector<pair<int,int> > v;
			map<pair<int,int>,vector<pair<int,int> > > ma;
			rep(i,h) 
			{
				rep(j,w) 
				{	
					if(isPerson(i,j)) 
					{
						//v.push_back(NextPoint(i,j));
						ma[NextPoint(i,j)].push_back(make_pair(i,j));
					}
				}
			}
			

			bool used[30][30];
			rep(i,h) rep(j,w) used[i][j] = true;

			map<pair<int,int>,vector<pair<int,int> > >::iterator ite;
			for(ite = ma.begin();ite != ma.end();ite++)
			{
				int nowy = ite->first.first;
				int nowx = ite->first.second;

				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;
				
					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 1 && used[nowy][nowx] && used[ny][nx])
					{
						if(s[nowy][nowx] == 'X')
						{
							s[ny][nx] = '.';
						}
						else swap(s[ny][nx],s[nowy][nowx]);

						used[nowy][nowx] = false;
						used[ny][nx] = false;
					}
				}

				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;

					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 2 && used[nowy][nowx] && used[ny][nx])
					{
						if(s[nowy][nowx] == 'X')
						{
							s[ny][nx] = '.';
						}
						else swap(s[ny][nx],s[nowy][nowx]);

						used[nowy][nowx] = false;
						used[ny][nx] = false;
					}
				}
				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;

					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 3 && used[nowy][nowx] && used[ny][nx])
					{
						if(s[nowy][nowx] == 'X') s[ny][nx] = '.';
						else swap(s[ny][nx],s[nowy][nowx]);

						used[ny][nx] = false;
						used[nowy][nowx] = false;
					}
				}

				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;

					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 4 && used[nowy][nowx] && used[ny][nx])
					{
						
						if(s[nowy][nowx] == 'X')
						{
							s[ny][nx] = '.';
						}
						else swap(s[ny][nx],s[nowy][nowx]);

						used[nowy][nowx] = false;
						used[ny][nx] = false;
					}
				}
			}

			if(!onPerson(h,w))
			{
				cout << k+1 << endl;
				flag = true;
				break;
			}
		}

		if(!flag) cout << "NA" << endl;

	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1236
final_rank: 8

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <iterator>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

string s[30];
int h,w;
int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};

pair<int,int> NextPoint(int y,int x)
{
	pair<int,int> p;
	if(s[y][x] == 'E')
	{
		if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
		else if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';

			p.first = y;
			p.second = x+1;
			return p;
		}
		else if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
		else if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
	}
	else if(s[y][x] == 'N')
	{
		if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';

			p.first = y;
			p.second = x+1;
			return p;
		}
		else if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
		else if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
		else if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
	}
	else if(s[y][x] == 'W')
	{
		if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
		else if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
		else if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
		else if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';

			p.first = y;
			p.second = x+1;
			return p;
		}
	}
	else if(s[y][x] == 'S')
	{
		if(s[y][x-1] == '.' || s[y][x-1] == 'X')
		{
			s[y][x] = 'W';

			p.first = y;
			p.second = x-1;
			return p;
		}
		else if(s[y+1][x] == '.' || s[y+1][x] == 'X')
		{
			s[y][x] = 'S';

			p.first = y+1;
			p.second = x;
			return p;
		}
		else if(s[y][x+1] == '.' || s[y][x+1] == 'X')
		{
			s[y][x] = 'E';
			p.first = y;
			p.second = x+1;
			return p;
		}
		else if(s[y-1][x] == '.' || s[y-1][x] == 'X')
		{
			s[y][x] = 'N';

			p.first = y-1;
			p.second = x;
			return p;
		}
	}
}

bool can(int y,int x)
{
	if(0 <= y & y < h && 0 <= x && x < w)
	{
		return true;
	}
	else return false;
}

bool isPerson(int y,int x)
{
	if(s[y][x] == 'E' || s[y][x] == 'W' || s[y][x] == 'S' || s[y][x] == 'N')
	{
		return true;
	}
	else return false;
}

bool onPerson(int h,int w)
{
	bool flag = false;

	rep(i,h)
	{
		rep(j,w)
		{
			if(s[i][j] == 'E' || s[i][j] == 'N' || s[i][j] == 'W' || s[i][j] == 'S')
			{
				flag = true;
			}
		}
	}

	return flag;
}

void printS(int h,int w)
{
	rep(i,h)
	{
		rep(j,w)
		{
			cout << s[i][j];
		}
		cout << endl;
	}
}

int main()
{
	map<pair<int,int>,int> cmp;

	cmp[pair<int,int>(0,1)] = 1;
	cmp[pair<int,int>(-1,0)] = 2;
	cmp[pair<int,int>(0,-1)] = 3;
	cmp[pair<int,int>(1,0)] = 4;


	while(cin >> w >> h)
	{
		if(w == 0 && h == 0) break;

		rep(i,h)
		{
			cin >> s[i];
		}

		if(!onPerson(h,w))
		{
			cout << 0 << endl;
			return 0;
		}


		bool flag = false;

		rep(k,180)
		{
			vector<pair<int,int> > v;
			map<pair<int,int>,vector<pair<int,int> > > ma;
			rep(i,h) 
			{
				rep(j,w) 
				{	
					if(isPerson(i,j)) 
					{
						//v.push_back(NextPoint(i,j));
						ma[NextPoint(i,j)].push_back(make_pair(i,j));
					}
				}
			}
			

			bool used[30][30];
			rep(i,h) rep(j,w) used[i][j] = true;

			map<pair<int,int>,vector<pair<int,int> > >::iterator ite;
			for(ite = ma.begin();ite != ma.end();ite++)
			{
				int nowy = ite->first.first;
				int nowx = ite->first.second;

				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;
				
					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 1 && used[nowy][nowx] && used[ny][nx])
					{
						if(s[nowy][nowx] == 'X')
						{
							s[ny][nx] = '.';
						}
						else swap(s[ny][nx],s[nowy][nowx]);

						used[nowy][nowx] = false;
						used[ny][nx] = false;
					}
				}

				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;

					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 2 && used[nowy][nowx] && used[ny][nx])
					{
						if(s[nowy][nowx] == 'X')
						{
							s[ny][nx] = '.';
						}
						else swap(s[ny][nx],s[nowy][nowx]);

						used[nowy][nowx] = false;
						used[ny][nx] = false;
					}
				}
				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;

					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 3 && used[nowy][nowx] && used[ny][nx])
					{
						if(s[nowy][nowx] == 'X') s[ny][nx] = '.';
						else swap(s[ny][nx],s[nowy][nowx]);

						used[ny][nx] = false;
						used[nowy][nowx] = false;
					}
				}

				rep(j,ite->second.size())
				{
					int ny = (ite->second)[j].first;
					int nx = (ite->second)[j].second;

					if(cmp[pair<int,int>(ny-nowy,nx-nowx)] == 4 && used[nowy][nowx] && used[ny][nx])
					{
						
						if(s[nowy][nowx] == 'X')
						{
							s[ny][nx] = '.';
						}
						else swap(s[ny][nx],s[nowy][nowx]);

						used[nowy][nowx] = false;
						used[ny][nx] = false;
					}
				}
			}

			if(!onPerson(h,w))
			{
				cout << k+1 << endl;
				flag = true;
				break;
			}
			

			

			if(!onPerson(h,w))
			{
				cout << k+1 << endl;
				flag = true;
				break;
			}

		}

		if(!flag) cout << "NA" << endl;

	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1236
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <string>
using namespace std;

int foo(char c) {
	switch(c) {
		case 'N' : return 0;
		case 'E' : return 1;
		case 'S' : return 2;
		case 'W' : return 3;
	}
	return -1;
}

char bar(int n) {
	switch(n) {
		case 0 : return 'N';
		case 1 : return 'E';
		case 2 : return 'S';
		case 3 : return 'W';
	}
	return '@';
}

int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};

int W, H;
bool valid(int i, int j) {
	return 0 <= i && i < H && 0 <= j && j < W;
}

int main() {
	while(cin >> W >> H, W || H) {
		vector<string> sq;
		for(int i = 0; i < H; i++) {
			string s;
			cin >> s; sq.push_back(s);
		}

		int ret = -1;
		for(int t = 0; t < 180; t++) {
			vector<string> nex;

			/* turn */
			for(int i = 0; i < sq.size(); i++) {
				for(int j = 0; j  < sq[i].size(); j++) {
					if(foo(sq[i][j]) != -1) {
						for(int k = 0; k < 4; k++) {
							int x,y;
							int dir = (k + 4 - foo(sq[i][j])) % 4;
							y = i + dy[dir];
							x = j + dx[dir];
							if(!valid(y, x))
								continue;
							if(sq[y][x] == '.' || sq[y][x] == 'X') {
								sq[i][j] = bar((5 - dir)%4);
								break;
							}
						}
					}
				}
			}

			/* debug 
			if(t < 20) {
				for(int i = 0; i < sq.size(); i++) {
					for(int j = 0; j < sq[i].size(); j++) {
						cout << sq[i][j];
					} cout << endl;
				}
				cout << endl;
			}
			// */

			/* move */
			for(int i = 0; i < 4; i++) {
				/*
				if(t < 20)
					cout << bar((3-i)%4) << endl;
				// */
				for(int j = 0; j < sq.size(); j++) {
					for(int k = 0; k < sq[j].size(); k++) {
						if(sq[j][k] == bar((3-i)%4)) {
							int x, y;
							x = k + dx[(i+2)%4];
							y = j + dy[(i+2)%4];
							if(valid(y,x) && sq[y][x] == '.') {
								sq[y][x] = (int)sq[j][k] - ('A' - 'a');
								sq[j][k] = '@';
							}
							else if(valid(y,x) && sq[y][x] == 'X') {
								sq[y][x] = 'x';
								sq[j][k] = '@';
								ret = t;
							}
						}
					}
				}
			}
			/* debug 
			if(t < 20) {
				for(int i = 0; i < sq.size(); i++) {
					for(int j = 0; j < sq[i].size(); j++) {
						cout << sq[i][j];
					} cout << endl;
				}
				cout << endl;
			}
			// */

			for(int i = 0; i < sq.size(); i++) {
				for(int j = 0; j < sq[i].size(); j++) {
					if(sq[i][j] == '@')
						sq[i][j] = '.';
					else if(sq[i][j] == 'x')
						sq[i][j] = 'X';
					if(sq[i][j] == 'n' || sq[i][j] == 'e' || sq[i][j] == 'w' || sq[i][j] == 's')
						sq[i][j] = (int)sq[i][j] + 'A' - 'a';
				}
			}
		}

		for(int i = 0; i < sq.size(); i++) {
			for(int j = 0; j < sq.size(); j++) {
				if(foo(sq[i][j]) != -1) {
					ret = -1;
				}
			}
		}

		if(ret == -1)
			cout << "NA" << endl;
		else
			cout << ret + 1 << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1220
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <string>
#include <cmath>
#include <cstdio>
#define FOR(I,A,B) for(int I =(A);I<(B);++I)
#define FORE(I,B) for(int I =1;I<=(B);++I)
#define REP(I,A) FOR(I,0,A)
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
const char dc[]={'E','N','W','S'};
const char dcc[]={'e','n','w','s'};
using namespace std;

int main() {
	// your code goes here
	int W,H;
	while (cin >> W >> H && W){
	char tmp;
	char maps[32][32]={};
	FORE(i,H)
		FORE(j,W)
			cin >> maps[i][j];
	bool ans;
	int next=0;
	while(true){
		ans=false;
		FORE(j,W)
			FORE(i,H){
				REP(k,4)
					if(maps[i][j]==dc[k]){
						REP(l,4){
							int n=(k+l+3)%4;
							if(maps[i+dy[n]][j+dx[n]]=='.'||maps[i+dy[n]][j+dx[n]]=='X'){
								maps[i][j]=dc[n];
								break;
							}
						}
					ans=true;
					break;
					}
			}
		FORE(i,H)
			FORE(j,W)
				if(maps[i][j]=='.'||maps[i][j]=='X'){
					REP(k,4)
						if(maps[i+dy[k]][j+dx[k]]==dc[(k+2)%4]){

							if(maps[i][j]=='.')
							maps[i][j]=dcc[(k+2)%4];
							maps[i+dy[k]][j+dx[k]]='x';
							break;
						}
				}
		FORE(i,H){
			FORE(j,W){
				if(maps[i][j]=='x')
					maps[i][j]='.';
				REP(k,4)
					if(maps[i][j]==dcc[k])
						maps[i][j]=dc[k];
			//	cout << maps[i][j];
			}
	//	cout << endl;
		}
		if(!ans || next>180)
			break;
		next++;
	}
	if(next>180)
	cout << "NA" << endl;
	else 
	cout << next << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1164
final_rank: 2

code_snippet: #include<iostream>
using namespace std;
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};
bool isEmpty(char ch){
  if(ch=='.'||ch=='X')return true;
  else return false;
}
int change(char ch){
  if(ch=='N')return 0;
  if(ch=='E')return 1;
  if(ch=='S')return 2;
  if(ch=='W')return 3;
  return -1;
}
char change2(int x){
  if(x==0)return 'N';
  if(x==1)return 'E';
  if(x==2)return 'S';
  if(x==3)return 'W';
  return '-';
}
char t[30][30],tmp[30][30];
int W,H,gy[900],gx[900],size;
 
int main(){
  while(1){
    cin>>W>>H;
    if(W==0&&H==0)break;
    size=0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin>>t[i][j];
	if(t[i][j]=='X'){
	  gy[size]=i;
	  gx[size]=j;
	  size++;
	}
      }
    }

    int ans=0;
    while(1){
      ///////////////////////////////////////////////
      bool flag=false;
      for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	  if(change(t[i][j])==-1)continue;
	  flag=true;
	  int dir=change(t[i][j]);
	  for(int k=5;k>=2;k--){
	    int ndir=(dir+k)%4;
	    int ny=i+dy[ndir],nx=j+dx[ndir];
	    if(isEmpty(t[ny][nx])){
	      t[i][j]=change2(ndir);
	      break;
	    }
	  }//k
	}//j
      }//i
      if(flag==false)break;
      ///////////////////////////////////////////////
      ans++;
      if(ans>180)break;
      /////////////////////////////////////////////// w s e n
      
      for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	  if(change(t[i][j])==-1){
	    tmp[i][j]=t[i][j];
	  }else tmp[i][j]='.';
	}
      }
      
      for(int k=3;k>=0;k--){
	for(int i=0;i<H;i++){
	  for(int j=0;j<W;j++){
	    if(k!=change(t[i][j]))continue;
	    int ny=i+dy[k],nx=j+dx[k];
	    if(isEmpty(t[ny][nx])&&isEmpty(tmp[ny][nx])){
	      tmp[ny][nx]=t[i][j];
	    }else{
	      tmp[i][j]=t[i][j];
	    }
	  }
	}
      }
      
      for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	  t[i][j]=tmp[i][j];
	}
      }

      for(int i=0;i<size;i++){
	t[gy[i]][gx[i]]='X';
      }
      ///////////////////////////////////////////////
      
    }
    if(ans>180)cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1172
final_rank: 4

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int m[50][50];
int w[50][50];
char str[50][50];
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};
int main(){
	int a,b;
	while(scanf("%d%d",&b,&a),a){
		for(int i=0;i<a;i++)scanf("%s",str[i]);
		for(int i=0;i<=180;i++){
			for(int j=0;j<a;j++)
				for(int k=0;k<b;k++)
					m[j][k]=-1;
		}
		int ret=-1;
		for(int i=0;i<a;i++)
			for(int j=0;j<b;j++){
				if(str[i][j]=='E')m[i][j]=0;
				if(str[i][j]=='N')m[i][j]=1;
				if(str[i][j]=='W')m[i][j]=2;
				if(str[i][j]=='S')m[i][j]=3;
			}
		for(int i=0;i<180;i++){
			for(int j=0;j<a;j++){
				for(int k=0;k<b;k++){
					w[j][k]=0;
					if(!~m[j][k])continue;
					for(int l=0;l<4;l++){
						int tx=j+dx[(m[j][k]+l+3)%4];
						int ty=k+dy[(m[j][k]+l+3)%4];
						if(0<=tx&&tx<a&&0<=ty&&ty<b&&!~m[tx][ty]&&str[tx][ty]!='#'){
							m[j][k]=(m[j][k]+l+3)%4;
							w[j][k]=1;
							break;
						}
					}
				}
			}
			for(int j=0;j<4;j++){
				for(int k=0;k<a;k++){
					for(int l=0;l<b;l++){
						if(m[k][l]!=(2+j)%4)continue;
						if(w[k][l]){
							int tx=k+dx[m[k][l]];
							int ty=l+dy[m[k][l]];
							if(!~m[tx][ty]){
								m[tx][ty]=m[k][l];
								m[k][l]=-1;
							}
						}
					}
				}
			}
			for(int j=0;j<a;j++)
				for(int k=0;k<b;k++)
					if(str[j][k]=='X')m[j][k]=-1;
			bool ok=true;
			for(int j=0;j<a;j++)
				for(int k=0;k<b;k++)
					if(~m[j][k])ok=false;
		/*	for(int j=0;j<a;j++){
				for(int k=0;k<b;k++){
					if(~m[j][k])printf("%d",m[j][k]);else printf(".");
				}
				printf("\n");
			}
			printf("\n");
		*/	if(ok){ret=i+1;break;}
		}
		if(ret==-1)printf("NA\n");
		else printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1060
final_rank: 1

code_snippet: #include<iostream>
#include<vector>
using namespace std;

const string Direction = {"ENWS"};
const int Next[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
enum {Y, X};

int main() {
    int w, h;
    while(cin >> w >> h, w | h) {
        int time;
        vector<string> field(h);
        for(int i = 0; i < h; ++i) cin >> field[i];

        for(time = 0; time <= 180; ++time) {
//             cout << time << endl;
//             for(auto& s: field)cout<<s<<endl;
            bool ok = true;
            for(const auto& line: field) for(const auto& c: line)
                if(Direction.find(c) != string::npos) ok = false;
            if(ok) break;
            // turn
            for(int i = 0; i < h; ++i) for(int j = 0; j < w; ++j) {
                int index = Direction.find(field[i][j]);
                if(index == string::npos) continue;
                for(int k = 0; k < 4; ++k) {
                    int d = (index + 3 + k) % 4;
                    int y = i + Next[d][Y];
                    int x = j + Next[d][X];
                    if(field[y][x] != '.' && field[y][x] != 'X') continue;
                    field[i][j] = Direction[d];
                    break;
                }
            }
            // move
            vector<vector<int>> used(h, vector<int>(w, 0));
            for(int i = 0; i < h; ++i) for(int j = 0; j < w; ++j) {
                if(field[i][j] != '.' && field[i][j] != 'X') continue;
                if(used[i][j]) continue;
                for(int d = 0; d < 4; ++d) {
                    int y = i + Next[d][Y];
                    int x = j + Next[d][X];
                    if(y < 0 || h <= y) continue;
                    if(x < 0 || w <= x) continue;
                    if(field[y][x] != Direction[(d + 2) % 4]) continue;
                    if(used[y][x]) continue;
                    used[i][j] = 1;
                    used[y][x] = 1;
                    if(field[i][j] == '.') field[i][j] = field[y][x];
                    field[y][x] = '.';
                    break;
                }
            }
        }

        if(time > 180) cout << "NA" << endl;
        else cout << time << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 1248
final_rank: 12

code_snippet: #include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

class Maze {
public:
	map<char, char> next;
	map<char, char> prev;
	map<char, pair<int,int>> xy;

	Maze(vector<string> maze) : maze(maze), rest(-1) {
		next = { {'N','E'}, {'E','S'}, {'S','W'}, {'W','N'} };
		prev = { {'E','N'}, {'S','E'}, {'W','S'}, {'N','W'} };
		xy = { {'N', {0, -1}}, {'E', {1, 0}}, {'S', {0, 1}}, {'W', {-1, 0}} };
		H = maze.size();
		W = maze[0].size();
		rest = countPeople();
	}

	void move() {
		changeDirection();
		goAhead();
	}

	int countPeople() {
		if (0 <= rest) return rest;
		int count = 0;
		for (string str : maze) {
			for (char c : str) {
				count += isPerson(c);
			}
		}
		return count;
	}

private:
	vector<string> maze;
	int H, W;
	int rest;

	bool isPerson(char c) {
		return (isalpha(c) && c != 'X');
	}

	bool isPassable(char c) {
		return (c == '.' || c == 'X');
	}

	void changeDirection() {
		for (int i=0; i<H; ++i) {
			for (int j=0; j<W; ++j) {
				if ( ! isPerson(maze[i][j])) continue;
				char dir = next[maze[i][j]];
				for (int k=0; k<4; ++k) {
					int x = j + xy[dir].first;
					int y = i + xy[dir].second;
					if (maze[y][x] == '.' || maze[y][x] == 'X') {
						maze[i][j] = dir;
						break;
					}
					dir = prev[dir];
				}
			}
		}
	}

	void goAhead () {
		for (int i=0; i<H; ++i) {
			for (int j=0; j<W; ++j) {
				if ( ! isPassable(maze[i][j])) continue;
				char dir = 'E';
				for (int k=0; k<4; ++k) {
					int x = j + xy[dir].first;
					int y = i + xy[dir].second;
					if (0 <= x && x < W && 0 <= y && y < H && maze[y][x] == next[next[dir]]) {
						maze[y][x] = '*';
						if (maze[i][j] == '.') {
							maze[i][j] = tolower(next[next[dir]]);
						} else {
							rest --;
						}
						break;
					}
					dir = prev[dir];
				}
			}
		}
		for (int i=0; i<(int)maze.size(); ++i) {
			for (int j=0; j<(int)maze[i].size(); ++j) {
				if (maze[i][j] == '*') {
					maze[i][j] = '.';
				} else {
					maze[i][j] = toupper(maze[i][j]);
				}
			}
		}
	}
};

int main() {
	ios::sync_with_stdio(false);

	int W, H;
	while (cin >> W >> H && (W || H)) {
		vector<string> input(H);
		for (int i=0; i<H; ++i) cin >> input[i];

		Maze maze(input);

		if (maze.countPeople() == 0) {
			cout << 0 << endl;
			continue;
		}

		int second = 0;
		while (0 < maze.countPeople() && second <= 180) {
			maze.move();
			second ++;
		}
		if (second <= 180) cout << second << endl;
		else cout << "NA" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1232
final_rank: 15

code_snippet: #include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

class Maze {
public:
	map<char, char> next;
	map<char, char> prev;
	map<char, pair<int,int>> xy;

	Maze(vector<string> maze) : maze(maze), rest(-1) {
		next = { {'N','E'}, {'E','S'}, {'S','W'}, {'W','N'} };
		prev = { {'E','N'}, {'S','E'}, {'W','S'}, {'N','W'} };
		xy = { {'N', {0, -1}}, {'E', {1, 0}}, {'S', {0, 1}}, {'W', {-1, 0}} };
		H = maze.size();
		W = maze[0].size();
		rest = countPeople();
	}

	void move() {
		changeDirection();
		goAhead();
	}

	int countPeople() {
		if (0 <= rest) return rest;
		int count = 0;
		for (string str : maze) {
			for (char c : str) {
				count += isPerson(c);
			}
		}
		return count;
	}

private:
	vector<string> maze;
	int H, W;
	int rest;

	bool isPerson(char c) {
		return (isalpha(c) && c != 'X');
	}

	bool isPassable(char c) {
		return (c == '.' || c == 'X');
	}

	void changeDirection() {
		for (int i=0; i<H; ++i) {
			for (int j=0; j<W; ++j) {
				if ( ! isPerson(maze[i][j])) continue;
				char dir = next[maze[i][j]];
				for (int k=0; k<4; ++k) {
					int x = j + xy[dir].first;
					int y = i + xy[dir].second;
					if (maze[y][x] == '.' || maze[y][x] == 'X') {
						maze[i][j] = dir;
						break;
					}
					dir = prev[dir];
				}
			}
		}
	}

	void goAhead () {
		for (int i=0; i<H; ++i) {
			for (int j=0; j<W; ++j) {
				if ( ! isPassable(maze[i][j])) continue;
				char dir = 'E';
				for (int k=0; k<4; ++k) {
					int x = j + xy[dir].first;
					int y = i + xy[dir].second;
					if (0 <= x && x < W && 0 <= y && y < H && maze[y][x] == next[next[dir]]) {
						maze[y][x] = '*';
						if (maze[i][j] == '.') {
							maze[i][j] = tolower(next[next[dir]]);
						} else {
							rest --;
						}
						break;
					}
					dir = prev[dir];
				}
			}
		}
		for (int i=0; i<(int)maze.size(); ++i) {
			for (int j=0; j<(int)maze[i].size(); ++j) {
				if (maze[i][j] == '*') {
					maze[i][j] = '.';
				} else {
					maze[i][j] = toupper(maze[i][j]);
				}
			}
		}
	}
};

int main() {
	int W, H;
	while (cin >> W >> H && (W || H)) {
		vector<string> input(H);
		for (int i=0; i<H; ++i) cin >> input[i];

		Maze maze(input);

		if (maze.countPeople() == 0) {
			cout << 0 << endl;
			continue;
		}

		int second = 0;
		while (0 < maze.countPeople() && second <= 180) {
			maze.move();
			second ++;
		}
		if (second <= 180) cout << second << endl;
		else cout << "NA" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 1228
final_rank: 18

code_snippet: #include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
typedef pair < int , int > Pi;
typedef pair < int , Pi > Pii;
#define fr first
#define sc second
const int dy[][4] = {{1,0,-1,0},{0,-1,0,1},{-1,0,1,0},{0,1,0,-1}};
const int dx[][4] = {{0,1,0,-1},{1,0,-1,0},{0,-1,0,1},{-1,0,1,0}};
const int dm[][4] = {{3,0,1,2},{0,1,2,3},{1,2,3,0},{2,3,0,1}};
const int my[] = { 0, -1, 0, 1}, mx[] = { 1, 0, -1, 0};
const int md[] = { 2, 3, 0, 1};
int w, h;
char mas[30][30];
vector< Pii > vc;
string tmp = "ENWS";
int search( int y , int x){
  for(int i = 0 ; i < vc.size() ; i++ ){
    if(Pi(y,x) == vc[i].sc){
      return i;
    }
  }
  return -1;
}
vector< Pii >  solve(){
  //向き変えるじゃん
  vector< Pii > java;
  for(int i = 0 ; i < vc.size() ; i++ ){
    //向きは毎回変えるじゃん(震え声)
    for(int j = 0 ; j < 4 ; j++ ){
      int yy = vc[i].sc.fr + dy[vc[i].fr][j];
      int xx = vc[i].sc.sc + dx[vc[i].fr][j];
      if(mas[yy][xx] != '#' && search(yy,xx) == -1){
        vc[i].fr = dm[vc[i].fr][j];
        break;
      }
    }
  }
  //動けないひとをプッシュしよう
  bool list[1000] = {};
  for(int i = 0 ; i < h ; i++ ){
    for(int j = 0 ; j < w ; j++ ){
      if(mas[i][j] == '#' || search(i,j) != -1) continue;
      for(int k = 0 ; k < 4 ; k++ ){
        int ny = i + my[k], nx = j + mx[k], iti;
        if((iti = search(ny,nx)) == -1) continue;
        if(vc[iti].fr == md[k]){
          if(mas[i][j] != 'X') java.push_back(Pii(vc[iti].fr,Pi(i,j)));
          list[iti] = true;
          break;
        }
      }
    }
  }
  for(int i = 0 ; i < vc.size() ; i++ ){
    if(!list[i]) java.push_back(vc[i]);
  }
  return java;
}
int main(){
  while(cin >> w >> h , w){
    for(int i = 0 ; i < h ; i++ ){
      for(int j = 0 ; j < w ; j++ ){
        cin >> mas[i][j];
        if(tmp.find(mas[i][j]) != string::npos){
          vc.push_back(Pii( tmp.find(mas[i][j]), Pi( i, j)));
        }
      }
    }
    int ret = 0;
    while(ret <= 180 && !vc.empty()){
      vc = solve();
      ret++;
    }
    if(ret == 181) cout << "NA" << endl;
    else cout << ret << endl;
    vc.clear();
  }
}
accuracy: 1
time_ms: 30
memory_kb: 1212
final_rank: 13

code_snippet: #include <iostream>
#include <cstring>

using namespace std;

const int max_size = 31;

void p(char a[max_size][max_size], int sizei, int sizej = 0, char *padding = "", int offseti = 0, int offsetj = 0){
	if(!sizej) sizej = sizei;
	for(int i = 0; i < sizei; i++){
		for(int j = 0; j < sizej; j++){
			cout << a[offseti+i][offsetj+j] << padding;
		}
		cout << endl;
	}
}

char dirchr[] = "ENWS";
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int dirnum(char d){
	for(int i = 0; i < 4; i++){
		if(dirchr[i] == d) return i;
	}
	//cout << "invalid char" << d << endl;
	//exit(0);
	return -1;
}

void changeDirection(char (*_map)[max_size], int i, int j){
	int startdir = dirnum(_map[i][j]) + 3; // 現在の向きの右から
	for(int k = 0; k < 4; k++){
		int dir = (startdir+k)%4;
		char destchar = _map[i+dy[dir]][j+dx[dir]];
		if(destchar == '.' || destchar == 'X'){
			//cout << "(" << i << ", " << j << "): " << _map[i][j] << "->" << dirchr[dir] << endl;
			_map[i][j] = dirchr[dir];
			return;
		}
	}
}

void move(char (*_map)[max_size], int i, int j){
	int dir = dirnum(_map[i][j]);
	char destchar = _map[i+dy[dir]][j+dx[dir]];
	if(destchar == '.'){
		_map[i+dy[dir]][j+dx[dir]] = _map[i][j];
		_map[i][j] = 'Z';
	}
	if(destchar == 'X'){
		_map[i+dy[dir]][j+dx[dir]] = 'Y'; // 同時脱出制限用
		_map[i][j] = 'Z';
	}
}

void peopleAct(char (*_map)[max_size], int i, int j, void (*func)(char (*)[max_size],int, int), char* filter = "ENWS", int filtersize = 4){
	for(int k = 0; k < filtersize; k++){
		if(_map[i][j] == filter[k]){
			(*func)(_map, i, j);
			break;
		}
	}
}

int main(){
	char square[max_size][max_size];

	int w, h;
	while(cin >> w >> h){
		if(!w && !h) break;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> square[i][j];
			}
		}
		int time = 0;
		for(time = 0; time <= 180; time++){

			// 人がいなければ避難終了
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w; j++){
					char c = square[i][j];
					if(c != '.' && c != '#' && c != 'X'){
						goto nextloop;
					}
				}
			}
			break;

nextloop: ;
			// 向きを変える処理
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w; j++){
					peopleAct(square, i, j, changeDirection);
					//for(int k = 0; k < 4; k++){
					//	if(square[i][j] == dirchr[k]){
					//		changeDirection(square, i, j);
					//		break;
					//	}
					//}
				}
			}

			// 移動する処理
			// 左、下、右、上の順に処理

			for(int i = 0; i < h; i++){
				for(int j = 0; j < w; j++){ // 左から
					peopleAct(square, i, j, move, "W", 1);
				}
			}

			for(int i = h-1; i >= 0; i--){ // 下から
				for(int j = 0; j < w; j++){
					peopleAct(square, i, j, move, "S", 1);
				}
			}

			for(int i = 0; i < h; i++){
				for(int j = w-1; j >= 0; j-- ){ // 右から
					peopleAct(square, i, j, move, "E", 1);
				}
			}

			for(int i = 0; i < h; i++){ // 上から
				for(int j = 0; j < w; j++){
					peopleAct(square, i, j, move, "N", 1);
				}
			}

			// 後処理
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w; j++){
					if(square[i][j] == 'Y'){
						square[i][j] = 'X';
					}
					if(square[i][j] == 'Z'){
						square[i][j] = '.';
					}
				}
			}

			//cout << endl;
			//p(square, h, w);
			//getchar();
		}

		//p(square, h, w);

		if(time <= 180){
			cout << time << endl;
		}else{
			cout << "NA" << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1168
final_rank: 3

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
// E, N, W, S
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
string ENWS = "ENWS";

int main(){
  int W, H;
  while(cin >> W >> H && W){
    vector<string> grid(H);
    REP(y, H) cin >> grid[y];
    REP(y, H) REP(x, W) if(grid[y][x] == 'X') grid[y][x] = '%';
    int rest = 0;
    REP(y, H) REP(x, W) if(isupper(grid[y][x])) rest++;
    int ans = INF;
    REP(time, 181){
      if(rest == 0) {
        ans = time;
        break;
      }
      REP(y, H) REP(x, W){
        REP(i, 4) if(grid[y][x] == ENWS[i]){
          REP(r, 4){
            int rr = (i + r + 3) % 4;
            int nx = x + dx[rr];
            int ny = y + dy[rr];
            if(valid(nx, ny, W, H) && (grid[ny][nx] == '.' || grid[ny][nx] == '%')){
              grid[y][x] = ENWS[rr];
              goto LOOPOUT;
            }
          }
        }
        LOOPOUT:;
      }
      vector<string> next = grid;
      REP(y, H) REP(x, W) if(grid[y][x] == '.' || grid[y][x] == '%') REP(r, 4){
        int nx = x + dx[r];
        int ny = y + dy[r];
        if(valid(nx, ny, W, H) && grid[ny][nx] == ENWS[(r + 2) % 4]){
          next[ny][nx] = '.';
          if(grid[y][x] == '.') next[y][x] = grid[ny][nx];
          else rest--;

          break;
        }
      }
      grid = next;
    }
    if(ans == INF) cout << "NA" << endl;
    else cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1224
final_rank: 7