Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ff first
  5. #define ss second
  6.  
  7. int n, m, vis[25][25];
  8. char grid[25][25];
  9.  
  10. bool push_this(int i, int j)
  11. {
  12. if(i >= 0 && i < n && j >= 0 && j < m && vis[i][j] == 0 && grid[i][j] != '#')
  13. return 1;
  14. return 0;
  15. }
  16.  
  17. int bfs(int i, int j)
  18. {
  19. pair<int, int> point;
  20. queue <pair<int, int> > q;
  21. q.push({i, j});
  22.  
  23. while(!q.empty())
  24. {
  25. point = q.front();
  26. q.pop();
  27.  
  28. if(grid[point.ff][point.ss] == 'h')
  29. return vis[point.ff][point.ss];
  30.  
  31. if(push_this(point.ff - 1, point.ss))
  32. {
  33. q.push({point.ff - 1, point.ss});
  34. vis[point.ff - 1][point.ss] = vis[point.ff][point.ss] + 1;
  35. }
  36. if(push_this(point.ff + 1, point.ss))
  37. {
  38. q.push({point.ff + 1, point.ss});
  39. vis[point.ff + 1][point.ss] = vis[point.ff][point.ss] + 1;
  40. }
  41. if(push_this(point.ff, point.ss - 1))
  42. {
  43. q.push({point.ff, point.ss - 1});
  44. vis[point.ff][point.ss - 1] = vis[point.ff][point.ss] + 1;
  45. }
  46. if(push_this(point.ff, point.ss + 1))
  47. {
  48. q.push({point.ff, point.ss + 1});
  49. vis[point.ff][point.ss + 1] = vis[point.ff][point.ss] + 1;
  50. }
  51. }
  52. }
  53.  
  54. int main()
  55. {
  56. int i, j, t, caseno = 0, ax[3], ay[3];
  57. scanf("%d", &t);
  58. while(t--)
  59. {
  60. scanf("%d %d", &n, &m);
  61. for(i = 0; i < n; i++)
  62. {
  63. scanf("%s", grid[i]);
  64. for(j = 0; j < m; j++)
  65. {
  66. if(grid[i][j] == 'm')
  67. grid[i][j] == '#';
  68. else if(grid[i][j] >= 'a' && grid[i][j] <= 'c')
  69. {
  70. ax[grid[i][j] - 'a'] = i;
  71. ay[grid[i][j] - 'a'] = j;
  72.  
  73. grid[i][j] = '.';
  74. }
  75. }
  76. }
  77. int mx = -1, temp;
  78. for(i = 0; i < 3; i++)
  79. {
  80. memset(vis, 0, sizeof(vis));
  81. mx = max(mx, bfs(ax[i], ay[i]));
  82. }
  83. printf("Case %d: %d\n", ++caseno, mx);
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement