#include #include #include #include #include #include #include using namespace std; ifstream fin; ofstream fout; int dx[4] = { 0, 1, 0, -1 }; int dy[4] = { 1, 0, -1, 0 }; char dc[4] = { 'S', 'E', 'N', 'W' }; int t, k; int n, m, chTest; char input[256][256]; int stX[256][256], copStX[256][256]; int stY[256][256], copStY[256][256]; bool usedBfs[256][256]; int bfsDist[256][256]; void refr( int test ) { for ( int x= 0 ;x < k ; x ++ ) { stX[test][x] = copStX[test][x]; stY[test][x] = copStY[test][x]; } } int targetX, targetY; queue < pair < int, int > > q; void bfs ( int x, int y ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { usedBfs[i][j] = 0; bfsDist[i][j] = 0; } } bfsDist[y][x] = 0; q.push( make_pair( x, y ) ); for ( ; !q.empty() ; ) { x = q.front().first; y = q.front().second; q.pop(); if ( usedBfs[y][x] ) { continue; } usedBfs[y][x] = 1; for ( int i = 0 ; i < 4 ; i ++ ) { if ( x + dx[i] < 0 || x+dx[i] >= m || y+dy[i] <0 || y+dy[i] >= n ) { continue; } if ( input[y+dy[i]][x+dx[i]] == '#' || usedBfs[y+dy[i] ][x+dx[i] ] ) { continue; } bfsDist[ y+dy[i] ] [x+dx[i] ] = bfsDist[y][x]+1; q.push( make_pair( x+dx[i], y+dy[i] ) ); } } } vector < char > ansPath; void moveAll( char cp ) { for ( int i = 0 ; i < k ; i ++ ) { for ( int d = 0 ; d < 4 ; d ++ ) { if ( dc[d] != cp ) { continue; } int x = stX[chTest][i], y = stY[chTest][i]; if ( x +dx[d] >= 0 && x+dx[d] = 0 && y+dy[d] = m || y >= n || input[y][x] == '#' ) { cntF++; } } if ( cntF == nCnt ) { blX = cx; blY = cy; return; } } } } vector < char > finalAns; vector < int > order; bool okD[4]; bool getSol () { ansPath.clear(); for ( int s = 0 ; s < 10 ; s ++ ) { for ( int i = 0 ; i < k ; i ++ ) { int ck = order[i]; int x = stX[chTest][ck]; int y = stY[chTest][ck]; if ( x == blX && y == blY ) { continue; } for ( ; x != blX || y != blY ; ) { int cd = bfsDist[y][x]; for ( int d = 0 ; d < 4 ; d ++ ) { if ( !okD[d] && rand()%6 ) { continue; } x += dx[d]; y += dy[d]; if ( x < 0 || y < 0 || x >= m || y >= n || input[y][x] == '#' || bfsDist[y][x] >= cd ) { x -= dx[d]; y -= dy[d]; }else { moveAll( dc[d] ); ansPath.push_back( dc[d] ); } } } } } for ( int i = 0 ; i < k ; i ++ ) { if ( stX[chTest][i] != blX || stY[chTest][i] != blY ) { return 0; } } return 1; } void pre () { blY = -1; blX = -1; nCnt = 3; findBl(); // cout << blY << " " << blX << " -> " << input[blY][blX] << "\n"; for ( int i = 0 ; i < 4 ; i ++ ) { int x = blX + dx[i]; int y = blY + dy[i]; if ( x < 0 || x >= m || y < 0 || y >= n || input[y][x] =='#' ) { okD[i] = 1; } else { okD[i] = 0; } } bfs ( blX, blY ); order.clear(); for ( int i = 0 ; i < k ; i ++ ) { order.push_back( i ); } random_shuffle( order.begin(), order.end() ); } clock_t startT; bool bomb () { clock_t crrT = clock(); if ( ( crrT - startT )*1./CLOCKS_PER_SEC > 4.80 ) { return 1; } return 0; } int main () { fin.open( "robots.in" ); fout.open( "robots.out" ); srand ( 420 ); startT = clock(); fin >> n >> m; for ( int i = 0 ; i < n ; i ++ ) { fin >> input[i]; } fin >> t >> k; for ( int i = 0 ; i < t ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) { fin >> stY[i][j] >> stX[i][j]; stY[i][j] --; stX[i][j] --; copStX[i][j] = stX[i][j]; copStY[i][j] = stY[i][j]; } } pre(); cout << getSol() << " <----- \n" ; refr( chTest ); finalAns = ansPath; int minTest = 0; while ( 1 ) { if ( bomb() ) { break; } pre(); for ( int ctest = 0 ; ctest < t ; ctest ++ ) { if ( bomb() ) { break; } chTest = ctest; cout << getSol() << " <---- \n"; refr( chTest ); cout << ansPath.size() << "\n"; if ( ansPath.size() < finalAns.size() ) { minTest = ctest; finalAns = ansPath; } } } cout << finalAns.size() << "\n"; fout << minTest+1 << "\n"; for ( int i = 0 ; i < 100000 && i < finalAns.size() ; i ++ ) { fout << finalAns[i]; } fout << "\n"; return 0; } /* 10 10 ###....... #...#.#... ####..#.#. ###..##... ..###...#. #..##...#. #.......## ..#...#.## ...####... .#.##..... 5 4 1 10 2 2 10 8 9 1 4 4 1 9 10 9 10 1 6 7 9 2 1 10 2 3 5 8 2 4 7 2 10 6 8 4 5 8 1 5 10 10 4 5 .#..# ....# #.#.. ...#. 2 2 1 1 4 5 4 1 4 5 5 5 ..... ..... ..... ..... ..... 1 3 1 1 5 5 3 3 */