UVA 11624 Fire!

题目描述:

Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it. Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R,C ≤ 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of: • #, a wall • ., a passable square • J, Joe’s initial position in the maze, which is a passable square • F, a square that is on fire There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2 4 4

####

#JF#

#..#

#..#

3 3

###

#J.

#.F


Sample Output
3

IMPOSSIBLE

思路:广度优先算法。只能预处理地图,得出某点着火时间

每个着火点直接入队,广搜全图确定每个位置的着火时间。

队列注意清空

 AC代码:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;

int t,c,r;
char g[1010][1010];
int mark[1010][1010];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int vis[1010][1010];

struct node{
    int x,y;
};
queue <node> que;

void bfsf(){
    while(!que.empty()){
        node a=que.front();
        int X=a.x,Y=a.y;
        que.pop();

        for(int i=0;i<4;i++){
            int xx=dx[i]+X;
            int yy=dy[i]+Y;
            if(xx>=0&&xx<r&&yy>=0&&yy<c&&g[xx][yy]!='#'&&!vis[xx][yy]){
                mark[xx][yy]=mark[X][Y]+1;
                //printf("%d   %d     %d     %d\n",xx,yy,X,Y);
                node b;
                b.x=xx,b.y=yy;
                que.push(b);
                vis[xx][yy]=1;
            }
        }
    }
}

struct node2{
    int x,y,n;
};
queue <node2>p;

void bfss(int x,int y){
    int ans=-1;
    memset(vis,0,sizeof(vis));
    while(!p.empty())
        p.pop();
    node2 a;
    a.x=x,a.y=y,a.n=0;
    p.push(a);

    while(!p.empty()){
        a=p.front();
        p.pop();
        int X=a.x,Y=a.y,n=a.n+1;

        if(X==r-1||X==0||Y==c-1||Y==0){
            //printf("%d %d\n",a.x,a.y);
            ans=n;
            break;
        }

        for(int i=0;i<4;i++){
            int xx=dx[i]+X;
            int yy=dy[i]+Y;
            if(g[xx][yy]=='.'&&!vis[xx][yy]&&n<mark[xx][yy]){
                //printf("%d %d %d*\n",xx,yy,mark[xx][yy]);
                node2 b;
                b.x=xx,b.y=yy,b.n=n;
                p.push(b);
                vis[xx][yy]=1;
            }
        }
    }
    printf(ans==-1?"IMPOSSIBLE\n":"%d\n",ans);
}

void Debug(){
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++)
            printf("%d ",mark[i][j]);
        printf("\n");
    }
}

int main(){
    int jx,jy;
    scanf("%d",&t);
    while(t--){
        memset(mark,0x3f,sizeof(mark));
        memset(vis,0,sizeof(mark));
        while(!que.empty())
            que.pop();
        scanf("%d%d",&r,&c);
        for(int i=0;i<r;i++){
            getchar();
            for(int j=0;j<c;j++){
                scanf("%c",&g[i][j]);
                if(g[i][j]=='F'){
                    node a;
                    a.x=i,a.y=j;
                    que.push(a);
                    mark[i][j]=0;
                    vis[i][j]=1;
                }
                if(g[i][j]=='J')
                    jx=i,jy=j;
            }
        }

        bfsf();
        //Debug();
        bfss(jx,jy);
    }
}

TLE:

写这个代码的时候没多想,着火点挨个广搜,这样做太耗时!直接每个着火点入队,设置每个着火点时间为0,全图广搜一次即可

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;

int t,c,r;
char g[1010][1010];
int mark[1010][1010];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int vis[1010][1010];

struct node{
    int x,y;
};
queue <node> que;

void bfsf(int x,int y){
    memset(mark,0x3f,sizeof(mark));
    memset(vis,0,sizeof(vis));
    while(!que.empty())
        que.pop();

    node a;
    a.x=x,a.y=y;
    vis[x][y]=1;
    mark[x][y]=0;
    que.push(a);
    while(!que.empty()){
        a=que.front();
        que.pop();

        for(int i=0;i<4;i++){
            int xx=dx[i]+a.x;
            int yy=dy[i]+a.y;
            if(xx>=0&&xx<r&&yy>=0&&yy<c&&g[xx][yy]!='#'&&!vis[xx][yy]){
                mark[xx][yy]=min(mark[xx][yy],mark[a.x][a.y]+1);
                node b;
                b.x=xx,b.y=yy;
                que.push(b);
                vis[xx][yy]=1;
            }
        }
    }
}

struct node2{
    int x,y,n;
};
queue <node2>p;

void bfss(int x,int y){
    int ans=-1;
    memset(vis,0,sizeof(vis));
    node2 a;
    a.x=x,a.y=y,a.n=0;
    p.push(a);

    while(!p.empty()){
        a=p.front();
        p.pop();

        if(a.x==r-1||a.x==0||a.y==c-1||a.y==0){
            //printf("%d %d\n",a.x,a.y);
            ans=a.n+1;
            break;
        }

        for(int i=0;i<4;i++){
            int xx=dx[i]+a.x;
            int yy=dy[i]+a.y;
            if(g[xx][yy]=='.'&&!vis[xx][yy]&&a.n+1<mark[xx][yy]){
                //printf("%d %d %d*\n",xx,yy,mark[xx][yy]);
                node2 b;
                b.x=xx,b.y=yy,b.n=a.n+1;
                p.push(b);
                vis[xx][yy]=1;
            }
        }
    }
    printf(ans==-1?"IMPOSSIBLE\n":"%d\n",ans);
}

int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&r,&c);
        for(int i=0;i<r;i++){
            getchar();
            for(int j=0;j<c;j++){
                scanf("%c",&g[i][j]);
            }
        }
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(g[i][j]=='F'){
                    bfsf(i,j);
                }
            }
        }

        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(g[i][j]=='J')
                    bfss(i,j);
            }
        }
    }
}

 错误代码:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <string.h>

using namespace std;

int t,row,column;
char square[1020][1020];
int mark[1020][1020];
int jr,jc;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int min_time;

struct fire{
    int x,y;
};
queue<fire>que;

struct node{
    int x,y,t;
};
queue<node>que2;  //用来依次存储合格点
//y-x>=a.y-a.x-t&&y-x<=a.y-a.x+t&&x+y<=a.x+a.y+t&&x+y>=a.x+a.y-t
void UpdateFire(int t){
    char ch[5]={'f','F'};
    int flag=t%2;

    for(int j=0;j<row;j++){
        for(int k=0;k<column;k++){
            if(flag&&square[j][k]=='.'&&(square[j+1][k]=='F'||square[j-1][k]=='F'||square[j][k-1]=='F'||square[j][k+1]=='F')){
                square[j][k]='f';
            }
            if(!flag&&square[j][k]=='.'&&(square[j+1][k]=='f'||square[j-1][k]=='f'||square[j][k-1]=='f'||square[j][k+1]=='f')){
                square[j][k]='F';
            }
        }
    }

}

void bfs(){
    while(!que2.empty()){
        que2.pop();
    }

    node a;
    a.x=jr,a.y=jc,a.t=0;
    que2.push(a);
    mark[jr][jc]=1;

    while(!que2.empty()){
        node b=que2.front();
        que2.pop();

        if(b.x==0||b.x==row-1||b.y==0||b.y==column-1){
            min_time=b.t+1;
            break;
        }

        for(int i=0;i<4;i++){
            int x=b.x+dx[i];
            int y=b.y+dy[i];
            int t=b.t+1;
            UpdateFire(t);
            if(x>=0&&x<row&&y>=0&&y<column&&!mark[x][y]&&square[x][y]!='#'&&square[x][y]!='F'&&square[x][y]!='f'){
                node c;
                c.t=t,c.x=x,c.y=y;
                que2.push(c);
                mark[x][y]=1;
            }
        }
    }
    if(min_time!=2100000000){
        printf("%d\n",min_time);
    }
    else {
        printf("IMPOSSIBLE\n");
    }
}

int main()
{
    scanf("%d",&t);
    while(t--){
        min_time=2100000000;
        while(!que.empty()){
            que.pop();
        }
        memset(mark,0,sizeof(mark));
        scanf("%d%d",&row,&column);
        for(int i=0;i<row;i++){
            getchar();
            for(int j=0;j<column;j++){
                scanf("%c",&square[i][j]);
                if(square[i][j]=='F'){
                    fire a;
                    a.x=i,a.y=j;
                    que.push(a);
                }
                if(square[i][j]=='J'){
                    jr=i,jc=j;
                }
            }
        }

        bfs();
    }
    return 0;
}
发布了62 篇原创文章 · 获赞 7 · 访问量 1648

猜你喜欢

转载自blog.csdn.net/qq_43331910/article/details/97668589
今日推荐