Sicily 1754 逃离洞穴

比较有技巧性的广搜,一开始在毒气的扩散那里理解错了,死活WA。不过毒气扩散的部分也是这道题区别于一般广搜的地方。

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <deque>
#include <memory.h>
char map[1010][1010];
int row,col;
bool visited[1010][1010];
int second[1010][1010];
deque <int> mapx,mapy;
deque <int> gasx,gasy;
void gas() {
    int x,y;
    int t=gasx.size();
    while ( t-- ) {
        x=gasx.front();
        gasx.pop_front();
        y=gasy.front();
        gasy.pop_front();
        if ( x+1<row&&map[x+1][y]=='.'&&!visited[x+1][y] ) {
            map[x+1][y]='D';
            visited[x+1][y]=true;
            gasx.push_back(x+1);
            gasy.push_back(y);
        }
        if ( x-1>=0&&map[x-1][y]=='.'&&!visited[x-1][y] ) {
            map[x-1][y]='D';
            visited[x-1][y]=true;
            gasx.push_back(x-1);
            gasy.push_back(y);
        }
        if ( y+1<col&&map[x][y+1]=='.'&&!visited[x][y+1] ) {
            map[x][y+1]='D';
            visited[x][y+1]=true;
            gasx.push_back(x);
            gasy.push_back(y+1);
        }
        if ( y-1>=0&&map[x][y-1]=='.'&&!visited[x][y-1] ) {
            map[x][y-1]='D';
            visited[x][y-1]=true;
            gasx.push_back(x);
            gasy.push_back(y-1);
        }
    }
}
int main()
{
    int i,j,k,num;
    int ex,ey,curx,cury;

    scanf( "%d%d",&row,&col );
    while ( row!=0&&col!=0 ) {
        for ( i=0;i<row;i++ ) 
            scanf( "%s",&map[i] );
        for ( i=0;i<row;i++ ) {
            for ( j=0;j<col;j++ ) {
                if ( map[i][j]=='P' ) {
                    mapx.push_back(i);
                    mapy.push_back(j);
                    visited[i][j]=true;
                    second[i][j]=0;
                }
                if ( map[i][j]=='D' ) {

                    visited[i][j]=true;
                    gasx.push_back(i);
                    gasy.push_back(j);
                }
                if ( map[i][j]=='E' ) {
                    ex=i;
                    ey=j;
                }
            }
        }
        memset(visited,false,1010*1010*sizeof(bool));
    //  memset(second,0,1010*1010*sizeof(int));
        while ( !mapx.empty() ) {
            num=mapx.size();
            gas();
            while ( num-- ) {
                curx=mapx.front();
                mapx.pop_front();
                cury=mapy.front();
                mapy.pop_front();
                if ( curx==ex&&cury==ey )
                    break;
                if ( curx+1<row&&(map[curx+1][cury]=='.'||map[curx+1][cury]=='E')&&!visited[curx+1][cury] ) {
                    second[curx+1][cury]=second[curx][cury]+1;
                    mapx.push_back(curx+1);
                    mapy.push_back(cury);
                    visited[curx+1][cury]=true;
                }
                if ( curx-1>=0&&(map[curx-1][cury]=='.'||map[curx-1][cury]=='E')&&!visited[curx-1][cury] ) {
                    second[curx-1][cury]=second[curx][cury]+1;
                    mapx.push_back(curx-1);
                    mapy.push_back(cury);
                    visited[curx-1][cury]=true;
                }
                if ( cury+1<col&&(map[curx][cury+1]=='.'||map[curx][cury+1]=='E')&&!visited[curx][cury+1] ) {
                    second[curx][cury+1]=second[curx][cury]+1;
                    mapx.push_back(curx);
                    mapy.push_back(cury+1);
                    visited[curx][cury+1]=true;
                }
                if ( cury-1>=0&&(map[curx][cury-1]=='.'||map[curx][cury-1]=='E')&&!visited[curx][cury-1] ) {
                    second[curx][cury-1]=second[curx][cury]+1;
                    mapx.push_back(curx);
                    mapy.push_back(cury-1);
                    visited[curx][cury-1]=true;
                }
            }
            if ( curx==ex&&cury==ey )
                break;
        }

    if ( curx==ex&&cury==ey )
        printf( "%d\n",second[curx][cury] );
    else 
        printf( "YYR is extremely dangerous!\n" );
    mapx.clear();
    mapy.clear();
    gasx.clear();
    gasy.clear();
    scanf( "%d%d",&row,&col );
    }
    return 0;
} 

你可能感兴趣的:(Sicily 1754 逃离洞穴)