Advertisement
Guest User

Zatvor

a guest
Jan 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n, m;
  8. cin>>n>>m;
  9.  
  10. int ri, rj, fi, fj;
  11. vector<pair<int, int> > govci;
  12. char matrix[n][m];
  13. int value[n][m];
  14. for(int i=0; i<n; i++){
  15. for(int j=0; j<m; j++){
  16. cin>>matrix[i][j];
  17. if(matrix[i][j]=='#'){
  18. value[i][j]=-3;
  19. continue;
  20. }
  21. value[i][j]=98799697;
  22. if(matrix[i][j]=='R'){
  23. ri=i;
  24. rj=j;
  25. }
  26. if(matrix[i][j]=='Z'){
  27. fi=i;
  28. fj=j;
  29. }
  30. if(matrix[i][j]=='G')
  31. govci.push_back(make_pair(i, j));
  32. }
  33. }
  34.  
  35.  
  36. int g=govci.size();
  37. int diri[]={1, -1, 0, 0};
  38. int dirj[]={0, 0, 1, -1};
  39. bool visited[n][m];
  40. for(int k=0; k<g; k++){
  41. queue <pair< int, int> > q;
  42. q.push(make_pair(govci[k].first, govci[k].second));
  43.  
  44. memset(visited, 0, sizeof(visited));
  45. value[govci[k].first][govci[k].second]=0;
  46. // cout<<endl<<endl<<endl;
  47. while(!q.empty()){
  48. int ci=q.front().first;
  49. int cj=q.front().second;
  50. q.pop();
  51. visited[ci][cj]=1;
  52. // cout<<ci<<" "<<cj<<endl;
  53. for(int d=0; d<4; d++){
  54. int ni=ci+diri[d];
  55. int nj=cj+dirj[d];
  56.  
  57. if(value[ni][nj]==-3) continue;
  58. if(ni>-1 && ni<n && nj>-1 && nj<m && visited[ni][nj]==0){
  59. visited[ni][nj]=1;
  60. value[ni][nj]=min(value[ci][cj]+1, value[ni][nj]);
  61. q.push(make_pair(ni, nj));
  62. }
  63. }
  64. }
  65. }
  66.  
  67. // for(int i=0; i<n; i++){
  68. // for(int j=0; j<m; j++){
  69. // cout<<value[i][j]<<" ";
  70. // if(value[i][j]<10) cout<<" ";
  71. // }
  72. // cout<<endl;
  73. // }
  74. priority_queue<pair<int, pair<int, int> > > p;
  75. p.push(make_pair(value[ri][rj], make_pair(ri, rj)));
  76. memset(visited, 0, sizeof(visited));
  77. while(!p.empty()){
  78.  
  79. int cd=p.top().first;
  80. int ci=p.top().second.first;
  81. int cj=p.top().second.second;
  82. p.pop();
  83. //cout<<ci<<" "<<cj<<" "<<cd<<endl;
  84.  
  85. if(ci==fi && cj==fj){
  86. cout<<cd<<endl;
  87.  
  88. return 0;
  89. }
  90. for(int d=0; d<4; d++){
  91. int ni=ci+diri[d];
  92. int nj=cj+dirj[d];
  93. if(matrix[ni][nj]=='*') continue;
  94. if(ni>-1 && ni<n && nj>-1 && nj<m && visited[ni][nj]==0){
  95. visited[ni][nj]=1;
  96. p.push(make_pair(min(cd, value[ni][nj]), make_pair(ni, nj)));
  97. }
  98. }
  99. }
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement