码迷,mamicode.com
首页 > 其他好文 > 详细

Fire uva 11624

时间:2016-07-20 11:48:23      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

题目连接:http://acm.hust.edu.cn/vjudge/problem/28833

/*
首先对整个图bfs一次得到火焰燃烧的时刻表
之后在bfs搜路径时加一个火烧表的判断
坑点在于:如果时刻表等于0应该是从未烧过。。。如果不加以区分就会wa
*/
#include <bits/stdc++.h>
#define scan(x) scanf("%d",&x)
#define M(x) memset(x,0,sizeof(x))
#define REF(i,n) for(int i=1;i<=n;i++)
using namespace std;
const int Max=1e3+10;
char mat[Max][Max];
int book[Max][Max];
int vis[Max][Max];
int n,m,stx,sty;
struct node
{
    int x,y,step;
    node(){x=y=step=0;}
    node(int xx,int yy,int ss):x(xx),y(yy),step(ss){}
};
int nex[4][2]={0,1,1,0,0,-1,-1,0};
queue<node>que1;
void bfs1()
{
    M(book);M(vis);
    node u,v;
    while(!que1.empty())
    {
        u=que1.front();
        que1.pop();
        for(int k=0;k<4;k++)
        {
            v.x=u.x+nex[k][0];
            v.y=u.y+nex[k][1];
            v.step=u.step+1;
            if(v.x<1||v.y<1||v.x>n||v.y>m) continue;
            if(mat[v.x][v.y]==#) continue;
            if(book[v.x][v.y]) continue;
            book[v.x][v.y]=v.step;
            que1.push(v);
        }
    }
}
int bfs2()
{
    M(vis);
    queue<node>que;
    node u,v;
    que.push(node(stx,sty,0));
    vis[stx][sty]=1;
    while(!que.empty())
    {
        u=que.front();
        que.pop();
        for(int k=0;k<4;k++)
        {
            v.x=u.x+nex[k][0];
            v.y=u.y+nex[k][1];
            v.step=u.step+1;
            if(v.x<1||v.y<1||v.x>n||v.y>m) return v.step;
            if(book[v.x][v.y]!=0&&book[v.x][v.y]<=v.step) continue;//book[i][j]!=0
            if(mat[v.x][v.y]==#||mat[v.x][v.y]==F) continue;//不能把从未着火的点也排除
            if(vis[v.x][v.y]) continue;
            vis[v.x][v.y]=1;
            que.push(v);
        }
    }
    return -1;
}
int main()
{
    int T;
    for(scan(T);T;T--)
    {
        cin>>n>>m;getchar();
        while(!que1.empty()) que1.pop();
        REF(i,n)
        {
            REF(j,m)
            {
               scanf("%c",&mat[i][j]);
               if(mat[i][j]==J) stx=i,sty=j;
               if(mat[i][j]==F) que1.push(node(i,j,0));
            }
            getchar();
        }
        bfs1();
        int ans=bfs2();
        if(ans!=-1) cout<<ans<<endl;
        else cout<<"IMPOSSIBLE"<<endl;
    }
    return 0;
}

 

Fire uva 11624

标签:

原文地址:http://www.cnblogs.com/zsyacm666666/p/5687690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!