Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- #include <cmath>
- #define MAXN 1005
- using namespace std;
- int N, M;
- bool flag[MAXN][MAXN][2];
- int maze[MAXN][MAXN];
- int dx[] = {1,-1,0,0};
- int dy[] = {0,0,1,-1};
- struct state {
- int dist;
- int x;
- int y;
- bool smell;
- int dir;
- state(int dist, int x, int y, bool smell, int dir) :
- dist(dist), x(x), y(y), smell(smell), dir(dir) {
- }
- void dsmell(bool scent){
- smell = scent;
- }
- };
- queue<state> q;
- bool able(int r, int c, bool smell){
- if(r>=0 && r<N && c>=0 && c<M){
- if(maze[r][c] == 0) return false;
- else if(maze[r][c] == 3) return smell;
- else return true;
- }
- return false;
- }
- int main(){
- freopen("dream.in","r",stdin);
- freopen("dream.out", "w", stdout);
- scanf("%d %d", &N, &M);
- for(int i = 0; i<N; i++){
- for(int j = 0; j<M; j++){
- scanf("%d", &maze[i][j]);
- }
- }
- q.push(state(0,0,0,false, 0));
- while(!q.empty()){
- state node = q.front();
- q.pop();
- if(node.x == N-1 && node.y == M-1){
- printf("%d\n", node.dist);
- return 0;
- }
- if(!flag[node.x][node.y][node.smell ? 0 : 1]){
- flag[node.x][node.y][node.smell ? 0 : 1] = true;
- if(maze[node.x][node.y] == 4 && able(node.x+dx[node.dir], node.y+dy[node.dir], node.smell)){
- q.push(state(node.dist+1, node.x+dx[node.dir], node.y+dy[node.dir], node.smell, node.dir));
- continue;
- }
- else{
- for(int i=0; i<4; i++){
- if(!able(node.x+dx[i], node.y+dy[i], node.smell)) continue;
- state child = state(node.dist+1, node.x+dx[i], node.y+dy[i], node.smell, i);
- if(maze[child.x][child.y] == 2) child.dsmell(true);
- if(maze[child.x][child.y] == 4) child.dsmell(false);
- q.push(child);
- }
- }
- }
- }
- printf("%d\n",-1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement