模板:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 1010
#define INF 0x3f3f3f3f
int n,m;
int T;
char g[MAX][MAX];
bool vis[MAX][MAX];
int fire[MAX][MAX];
int ans=INF;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int sx,sy;
int fx,fy;
struct mask
{
    int x,y,step;
    mask(){}
    mask(int x,int y,int step):x(x),y(y),step(step){};
};
queue<mask>q;
bool check(int a,int b){return 0<=a&&a<n&&0<=b&&b<m;}
void fire_path()
{
    memset(vis,false,sizeof(vis));
    memset(fire,0,sizeof(fire));
    while(q.size()) q.pop();//
    q.push(mask(fx,fy,0));//
    vis[fx][fy]=true;
    fire[fx][fy]=0;
    while(q.size())
    {
        mask tmp=q.front();q.pop();
        for(int i=0;i<4;i++)
        {
            int nx=tmp.x+dx[i];
            int ny=tmp.y+dy[i];
            if(check(nx,ny)&&!vis[nx][ny])
            {
                vis[nx][ny]=true;
                fire[nx][ny]=tmp.step+1;
                q.push(mask(nx,ny,tmp.step+1));
            }
        }
    }
}
void bfs()
{
    memset(vis,false,sizeof(vis));
    while(q.size()) q.pop();
    vis[sx][sy]=true;
    q.push(mask(sx,sy,0));
    while(q.size())
    {
        mask tmp=q.front();q.pop();
        if(g[tmp.x][tmp.y]=='E')
        {
            ans=min(ans,tmp.step);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            int nx=dx[i]+tmp.x;
            int ny=dy[i]+tmp.y;
            if(g[nx][ny]=='E'&&tmp.step+1<=fire[nx][ny])
            {
                 vis[nx][ny]=true;
                q.push(mask(nx,ny,tmp.step+1));
            }
            if(check(nx,ny)&&!vis[nx][ny]&&g[nx][ny]!='#'&&tmp.step+1<fire[nx][ny])
            {
                 vis[nx][ny]=true;
                q.push(mask(nx,ny,tmp.step+1));
            }
        }
    }
}
int main()
{
    while(cin>>n>>m){
   for(int i=0;i<n;i++)
   {
       cin>>g[i];
       for(int j=0;j<m;j++)
       {
           if(g[i][j]=='S')
           {
               sx=i;
               sy=j;
           }
           if(g[i][j]=='F')
           {
               fx=i;
               fy=j;//cout<<i<<j<<endl;
           }
       }
     }
     ans=INF;
      fire_path();
      /*for(int i=0;i<n;i++)
      {
          for(int j=0;j<m;j++)
            cout<<fire[i][j];
          cout<<endl;
      }*/
      bfs();
      if(ans==INF)
        cout<<"A! WO SI LA!"<<endl;
      else cout<<"PIG PIG PIG!"<<endl;
    }
   }
View Code

 

posted on 2018-12-01 22:21  海苔  阅读(119)  评论(0编辑  收藏  举报