Advertisement
Guest User

Untitled

a guest
May 30th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <queue>
  4. #include <cmath>
  5.  
  6. #define MAXN 1005
  7. using namespace std;
  8.  
  9. int N, M;
  10. bool flag[MAXN][MAXN][2];
  11. int maze[MAXN][MAXN];
  12. int dx[] = {1,-1,0,0};
  13. int dy[] = {0,0,1,-1};
  14.  
  15. struct state {
  16. int dist;
  17. int x;
  18. int y;
  19. bool smell;
  20. int dir;
  21.  
  22. state(int dist, int x, int y, bool smell, int dir) :
  23. dist(dist), x(x), y(y), smell(smell), dir(dir) {
  24. }
  25.  
  26. void dsmell(bool scent){
  27. smell = scent;
  28. }
  29. };
  30.  
  31. queue<state> q;
  32.  
  33. bool able(int r, int c, bool smell){
  34. if(r>=0 && r<N && c>=0 && c<M){
  35. if(maze[r][c] == 0) return false;
  36. else if(maze[r][c] == 3) return smell;
  37. else return true;
  38. }
  39. return false;
  40. }
  41.  
  42. int main(){
  43. freopen("dream.in","r",stdin);
  44. freopen("dream.out", "w", stdout);
  45.  
  46. scanf("%d %d", &N, &M);
  47.  
  48. for(int i = 0; i<N; i++){
  49. for(int j = 0; j<M; j++){
  50. scanf("%d", &maze[i][j]);
  51. }
  52. }
  53.  
  54. q.push(state(0,0,0,false, 0));
  55.  
  56. while(!q.empty()){
  57. state node = q.front();
  58. q.pop();
  59.  
  60. if(node.x == N-1 && node.y == M-1){
  61. printf("%d\n", node.dist);
  62. return 0;
  63. }
  64.  
  65. if(!flag[node.x][node.y][node.smell ? 0 : 1]){
  66. flag[node.x][node.y][node.smell ? 0 : 1] = true;
  67.  
  68. if(maze[node.x][node.y] == 4 && able(node.x+dx[node.dir], node.y+dy[node.dir], node.smell)){
  69. q.push(state(node.dist+1, node.x+dx[node.dir], node.y+dy[node.dir], node.smell, node.dir));
  70. continue;
  71. }
  72.  
  73. else{
  74. for(int i=0; i<4; i++){
  75. if(!able(node.x+dx[i], node.y+dy[i], node.smell)) continue;
  76.  
  77. state child = state(node.dist+1, node.x+dx[i], node.y+dy[i], node.smell, i);
  78.  
  79. if(maze[child.x][child.y] == 2) child.dsmell(true);
  80. if(maze[child.x][child.y] == 4) child.dsmell(false);
  81.  
  82. q.push(child);
  83. }
  84. }
  85. }
  86. }
  87.  
  88. printf("%d\n",-1);
  89.  
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement