Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int, int> ii;
- #define OO 0x7fffffff
- #define S second
- #define F first
- int dx[] = {1 , 0, 0,-1};
- int dy[] = {0 ,-1, 1, 0};
- const int N = 1e3 + 3e1;
- char Grid[N][N];
- ii Tom ,Jerry;
- int n ,m;
- inline bool isValid(int i ,int j){
- return (i > 0 && j > 0 && i < n && j < m && Grid[i][j] != 'W');
- }
- vector< vector< int > > BFS( ii Start ){
- vector< vector< int > > Dist(n ,vector< int >(m ,OO));
- Dist[ Start.F ][ Start.S ] = 0;
- queue<ii> Q; Q.push(Start);
- while( !Q.empty() ){
- ii front = Q.front(); Q.pop();
- int i = front.F ,j = front.S;
- for(int move = 0 ; move < 4 ; move ++){
- int newI = i + dx[ move ];
- int newJ = j + dy[ move ];
- if( isValid(newI ,newJ) && Dist[newI][newJ] == OO ){
- Dist[newI][newJ] = Dist[i][j] + 1;
- Q.push( ii(newI ,newJ) );
- }
- }
- }
- return Dist;
- }
- int main() {
- scanf("%d%d" ,&n ,&m);
- for(int i = 0 ; i < n ; i++){
- for(int j = 0 ; j < m ; j++){
- scanf("%c" ,&Grid[i][j]);
- if( Grid[i][j] == 'J' ) Jerry = ii(i ,j);
- if( Grid[i][j] == 'T' ) Tom = ii(i ,j);
- }
- }
- vector< vector< int > > DistJerry = BFS(Jerry);
- vector< vector< int > > DistTom = BFS( Tom );
- bool JerryFlag = false;
- // Check Rows
- for(int j = 0 ; j < m ; j++)
- if( DistJerry[0][j] <= DistTom[0][j] || DistJerry[n - 1][j] <= DistTom[n - 1][j] )
- JerryFlag = true;
- // Check Columns
- for(int i = 0 ; i < n ; i++)
- if( DistJerry[i][0] <= DistTom[i][0] || DistJerry[i][m - 1] <= DistTom[i][m - 1] )
- JerryFlag = true;
- puts( JerryFlag ? "Jerry" : "Tom" );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement