Advertisement
shamiul93

1175 - Jane and the Frost Giants

May 13th, 2017
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. /**
  2.  
  3. @author - Rumman BUET CSE'15
  4. Problem - 1175 - Jane and the Frost Giants
  5.  
  6. Concept - BFS
  7.  
  8. Speciality - A lot of corner cases. Tedious. BFS 2 times.
  9.  
  10. There may be as many F as they want. So, you have to put the shortest value in the grid by comparing them.
  11.  
  12. Tedious and lengthy. Easy one though.
  13.  
  14. */
  15.  
  16.  
  17.  
  18. // LightOJ always needs this format for sure..so I made a copy of it...
  19. #include <bits/stdc++.h>
  20. #include<vector>
  21. #define ll                                      long long int
  22. #define fi                                      freopen("in.txt", "r", stdin)
  23. #define fo                                      freopen("out.txt", "w", stdout)
  24. #define m0(a) memset(a , 0 , sizeof(a))
  25. #define m1(a) memset(a , -1 , sizeof(a))
  26.  
  27. #define mx 150000
  28. #define pll pair<ll,ll>
  29.  
  30. using namespace std;
  31.  
  32. ll dx[] = {0, 0, 1, -1 } ;
  33. ll dy[] = {-1, 1, 0, 0} ;
  34.  
  35. char grid[210][210] = {} ;
  36. ll fireGrid[210][210] = {} ;
  37. ll janeGrid[210][210] = {} ;
  38.  
  39. pll jane, fire ;
  40. ll r, c ;
  41.  
  42. void printgrid()
  43. {
  44.     cout << "Main Grid: " << endl ;
  45.     for(ll i = 0 ; i < r ; i++)
  46.     {
  47.         for(ll j = 0 ; j < c ; j++)
  48.         {
  49.             cout << grid[i][j] << " " ;
  50.         }
  51.         cout << endl ;
  52.     }
  53.  
  54.     cout << endl << endl << endl ;
  55.  
  56.  
  57.     cout << "Fire Grid: " << endl ;
  58.     for(ll i = 0 ; i < r ; i++)
  59.     {
  60.         for(ll j = 0 ; j < c ; j++)
  61.         {
  62.             cout << fireGrid[i][j] << " " ;
  63.         }
  64.         cout << endl ;
  65.     }
  66.  
  67.     cout << endl << endl << endl ;
  68.     cout << "Jane Grid:" << endl ;
  69.  
  70.     for(ll i = 0 ; i < r ; i++)
  71.     {
  72.         for(ll j = 0 ; j < c ; j++)
  73.         {
  74.             cout << janeGrid[i][j] << " " ;
  75.         }
  76.         cout << endl ;
  77.     }
  78.  
  79.     cout << endl << endl ;
  80. }
  81.  
  82. void FireBFS()
  83. {
  84.     queue<pll> q ;
  85.     pll pos ;
  86.  
  87.     q.push(fire) ;
  88.     fireGrid[fire.first][fire.second] = 1 ;
  89.  
  90.     while(!q.empty())
  91.     {
  92.         pll p ;
  93.         p = q.front() ;
  94.         q.pop() ;
  95.  
  96.         for(ll i = 0 ; i < 4 ; i++)
  97.         {
  98.             ll tx, ty ;
  99.             tx = p.first + dx[i] ;
  100.             ty = p.second + dy[i] ;
  101.  
  102.             if(tx >= 0 && tx < r && ty >= 0 && ty < c && grid[tx][ty] != '#')
  103.             {
  104.                 if(fireGrid[tx][ty] == -1)
  105.                 {
  106.                     fireGrid[tx][ty] = fireGrid[p.first][p.second] + 1 ;
  107.                     q.push(make_pair(tx,ty)) ;
  108.                 }
  109.                 else
  110.                 {
  111.                     if(fireGrid[tx][ty] > fireGrid[p.first][p.second] + 1 )
  112.                     {
  113.                         fireGrid[tx][ty] = fireGrid[p.first][p.second] + 1 ;
  114.                         q.push(make_pair(tx,ty)) ;
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.     }
  120.     return ;
  121. }
  122.  
  123. void JaneBFS()
  124. {
  125.     if(jane.first == 0 || jane.first == r-1 || jane.second == 0 || jane.second == c-1)
  126.     {
  127.         printf("1\n") ;
  128.         return ;
  129.     }
  130.  
  131.     queue<pll> q ;
  132.  
  133.     q.push(jane) ;
  134.     janeGrid[jane.first][jane.second] = 1 ;
  135.  
  136.     while(!q.empty())
  137.     {
  138.         pll p ;
  139.         p = q.front() ;
  140.         q.pop() ;
  141.  
  142.         bool dead = true ;
  143.  
  144.         for(ll i = 0 ; i < 4 ; i++)
  145.         {
  146.             ll tx, ty ;
  147.             tx = p.first + dx[i] ;
  148.             ty = p.second + dy[i] ;
  149.  
  150.             if(tx >= 0 && tx < r && ty >= 0 && ty < c && grid[tx][ty] != '#')
  151.             {
  152.                 if(janeGrid[tx][ty] == -1)
  153.                 {
  154.                     janeGrid[tx][ty] = janeGrid[p.first][p.second] + 1 ;
  155.                     q.push(make_pair(tx,ty)) ;
  156.  
  157.                     if(tx == r-1 || tx == 0 || ty == 0 || ty == c-1)
  158.                     {
  159.                         if(fireGrid[tx][ty] > janeGrid[tx][ty] || fireGrid[tx][ty] == -1)
  160.                         {
  161.                             printf("%lld\n",janeGrid[tx][ty]) ;
  162.                             return ;
  163.                         }
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170.     printf("IMPOSSIBLE\n");
  171.     return ;
  172. }
  173.  
  174.  
  175. void BFS()
  176. {
  177.     for(ll i = 0 ; i < r ; i++)
  178.     {
  179.         for(ll j = 0 ; j < c ; j++)
  180.         {
  181.             if(grid[i][j] == 'F')
  182.             {
  183.                 fire = make_pair(i,j) ;
  184.                 FireBFS();
  185.             }
  186.         }
  187.     }
  188.  
  189.     JaneBFS() ;
  190.     return ;
  191. }
  192.  
  193.  
  194. int main()
  195. {
  196. //    fi ;
  197. //    fo ;
  198.     ll T, t = 0 ;
  199.     scanf("%lld",&T) ;
  200.     while(T--)
  201.     {
  202.         for(ll i = 0 ; i < 210 ; i++)
  203.         {
  204.             for(ll j = 0 ; j < 210 ; j++)
  205.             {
  206.                 grid[i][j] = '\0' ;
  207.             }
  208.         }
  209.  
  210.         m1(fireGrid) ;
  211.         m1(janeGrid) ;
  212.  
  213.         t++ ;
  214.         scanf("%lld %lld",&r, &c) ;
  215. //        cout << r << " " << c << endl ;
  216.  
  217.         for(ll i = 0 ; i < r ; i++)
  218.         {
  219.             for(ll j = 0 ; j < c ; j++)
  220.             {
  221.                 scanf(" %c",&grid[i][j]) ;
  222.                 if(grid[i][j] == 'J')
  223.                 {
  224.                     jane = make_pair(i,j) ;
  225.                 }
  226.             }
  227.         }
  228.  
  229.         printf("Case %lld: ",t) ;
  230.         BFS() ;
  231. //        printgrid() ;
  232.     }
  233.     return 0 ;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement