Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int m,n,i,j,ans=0,a,b;
  4. bool vis[110][110];
  5. string s[110];
  6. int dx[]= {0,1,0,-1};
  7. int dy[]= {1,0,-1,0};
  8. bool check(int x,int y)
  9. {
  10. return x>=0 && x<m && y>=0 && y<n && vis[x][y]==false && s[x][y]!='#';
  11. }
  12. void bfs(int x,int y)
  13. {
  14. int j=0;
  15. queue<pair<int,int> > q;
  16. q.push(make_pair(x,y));
  17. vis[x][y]=true;
  18. while(!q.empty())
  19. {
  20. pair<int,int>p;
  21. p=q.front();
  22. q.pop();
  23. for(int i=0; i<4; i++)
  24. {
  25. int vx,vy;
  26. vx=p.first+dx[i];
  27. vy=p.second+dy[i];
  28. if(check(vx,vy))
  29. {
  30. vis[vx][vy]=true;
  31. q.push(make_pair(vx,vy));
  32. if(j%2==0) ans+=a;
  33. else ans+=b;
  34. j++;
  35. }
  36. }
  37. }
  38. }
  39. int main()
  40. {
  41.  
  42. cin>>n>>m;
  43. cin>>a>>b;
  44. for(i=0; i<m; i++)
  45. {
  46. cin>>s[i];
  47. }
  48. for(i=0; i<m; i++)
  49. {
  50. for(j=0; j<n; j++)
  51. {
  52. vis[i][j]=false;
  53. }
  54. }
  55. bfs(0,0);
  56. if(vis[m-1][n-1]== false ) cout<<"IMPOSSIBLE"<<endl;
  57. else cout<<ans<<endl;
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement