#include #include "stdio.h" #include "stdlib.h" #include #include #define UP 0 #define RIGHT 1 #define DOWN 2 #define LEFT 3 #define WALL 4 #define EXIT 5 #define GRASS 6 #define NOT 10 #define SUC 11 #define FAIL 12 using namespace std; vector< vector > map(101, vector(101)); int movesNr; int x, y; string moves; int move(int &px, int &py, int &dir) { //cout << map[px][py] << endl; for(int i = 0; i < moves.length(); i++) { if(moves[i] == 'R') {dir++; if(dir > LEFT) dir = UP; continue;} if(moves[i] == 'L') {dir--; if(dir < UP) dir = LEFT; continue;} if(moves[i]!= 'S') break; if(dir == UP) { if(py > 0 && map[px][py-1] != WALL) py--; } if(dir == DOWN) { if(py < y-1 && map[px][py+1] != WALL) py++; } if(dir == RIGHT) { if(px < x-1 && map[px+1][py] != WALL) px++; } if(dir == LEFT) { if(px > 0 && map[px-1][py] != WALL) px--; } if(map[px][py] == EXIT) {return 1;} } //cout << "moving to " << px <<" " << py << " " << dir << endl; return 0; } int main() { while(scanf("%d %d\n", &x, &y) == 2) { // cout << x << y << endl; // vector< vector < vector > > resolved(101, vector (101)); vector< vector < vector > > resolved(101, vector< vector >(101, vector(4,10))); for(int i = 0; i < x; i++) { //cout << "line " << i << endl; for (int j = 0; j < y; j++) { char foo = getchar(); // cout << foo; if(foo == 'X') {map[i][j] = WALL; } else if(foo == 'E') map[i][j] = EXIT; else map[i][j] = GRASS; } //cout << endl; getchar(); } /* for(int i = 0; i < x; i++) { //cout << "line " << i << endl; for (int j = 0; j < y; j++) { cout << map[x][y]; }cout << endl;}*/ cin >> movesNr; getchar(); cin >> moves; // cout << movesNr << endl; //cout << moves << endl; int total = 0; int good = 0; int posX, posY, dir; for(int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { if(map[i][j] == WALL) continue; //cout << "trying" << x << " " << y << " with res " << resolved[x][y][UP] << endl; total++; if(resolved[i][j][UP] == SUC){good++; continue;} //if(resolved[i][j][UP] == FAIL){continue;} int pi = i, pj = j, pd = UP; vector < vector > all; while(1) { int ppi = pi, ppj = pj, ppd = pd; if(map[pi][pj]==EXIT){resolved[i][j][UP] = SUC; good++; break;} int fine = move(pi, pj, pd); if(fine) {resolved[i][j][UP] = SUC; good+=2; break;} // if(resolved[pi][pj][pd] == FAIL) { break;} bool br = false; for(int xxx = 0; xxx < all.size(); xxx++){ //cout << "comparing: " << all[xxx][0] << " " << all[xxx][1] << " " << all[xxx][2]; // cout << " and " << pi << " " << pj << " " << pd << endl; if(all[xxx][0] == pi && all[xxx][1] == pj && all[xxx][2] == pd) { br = true; break;} } if (br) { break;} vector in; in.push_back(pi);in.push_back(pj);in.push_back(pd); all.push_back(in); } } } if(total <= good) cout << "OK" << endl; else cout << good << endl; } return 0; }