Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<iostream>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<string.h>
  7. #include<string>
  8. #include<functional>
  9. #include<queue>
  10. #include<stack>
  11. #include<cstring>
  12. #include<cmath>
  13. #include<climits>
  14. #include<set>
  15. #include<map>
  16. #include<deque>
  17. #include<bitset>
  18. using namespace std;
  19.  
  20. int R,C;
  21. bool visit[55][55];
  22. int temp_x,temp_y;
  23. char mp[55][55];
  24. int x_move[] = {0,0,-1,1};
  25. int y_move[] = {-1,1,0,0};
  26. int cnt[55][55];
  27. int x,y;
  28. queue<pair<int,int> > q;
  29. void bfs(){
  30. visit[temp_x][temp_y] = true;
  31. q.push({temp_x,temp_y});
  32. while(!q.empty()){
  33. int rx = q.front().first;
  34. int ry = q.front().second;
  35. q.pop();
  36.  
  37. // caution 3
  38. if(mp[rx][ry] == '*'){
  39. for(int i=0;i<4;i++){
  40. int nx = rx+x_move[i];
  41. int ny = ry+y_move[i];
  42. if(nx >0 && nx <=R && ny >0 && ny<=C){
  43. if(mp[nx][ny]!='D'&&mp[nx][ny]!='X'&&mp[nx][ny]!='S'&&!visit[nx][ny]){
  44. visit[nx][ny]=true;
  45. q.push({nx,ny});
  46.  
  47. // caution 4
  48. mp[nx][ny]='*';
  49. }
  50. }
  51. }
  52. }
  53. else if(mp[rx][ry]=='.'||mp[rx][ry]=='S'){
  54. for(int i=0;i<4;i++){
  55. int nx = rx+x_move[i];
  56. int ny = ry+y_move[i];
  57. if(nx >0 && nx <=R && ny >0 && ny<=C){
  58. if(mp[nx][ny]!='X'&&mp[nx][ny]!='*'&&!visit[nx][ny]){
  59. q.push({nx,ny});
  60. visit[nx][ny]=true;
  61. cnt[nx][ny] = cnt[rx][ry]+1;
  62. if(mp[nx][ny]=='D'){
  63. x = nx; y=ny;
  64. return;
  65. }
  66. }
  67. }
  68. }
  69. }
  70.  
  71. }
  72. }
  73. int main(){
  74. cin >> R >> C;
  75. getchar();
  76. for(int i=1;i<=R;i++){
  77. for(int j=1;j<=C;j++){
  78. mp[i][j] = getchar();
  79. if(mp[i][j] == 'S'){
  80. temp_x = i; temp_y = j;
  81. }
  82. else if(mp[i][j] =='*'){
  83. q.push({i,j});
  84. }
  85. }
  86. getchar();
  87. }
  88. bfs();
  89. if(!cnt[x][y]) printf("KAKTUS");
  90. else printf("%d",cnt[x][y]);
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement