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

UVA 11624 不知道为什么WA

时间:2016-03-17 12:10:30      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

#include<cstdio>
#include<iostream>
#include<queue>
#include<string>
#include<math.h>
#include<stack>
#include<cstdlib>
#include<map>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cctype>
#include<sstream>

typedef long long ll;
using namespace std; 

struct Node{
    int x,y;
    int t;    
    Node(int x,int y,int t):x(x),y(y),t(t){}
};
int n,m;
char maps[1050][1050];
bool vis[1050][1050];
int firet[1050][1050];
int fx,fy,sx,sy;
int tx[4] = {-1,0,1,0 };
int ty[4] = {0,-1,0,1 };
queue<Node> fq;
queue<Node> jq;

bool escape(int dx,int dy){
    return dx==0 || dx==n-1 || dy==0 || dy==m-1;
}
bool ok(int dx,int dy,int t){
    if(maps[dx][dy] == #) return false;
    if(vis[dx][dy])    return false;
    if(firet[dx][dy] != 0 && firet[dx][dy] <= t) return false;
    return true;
}
void input(){
    while(!fq.empty()) fq.pop();
    memset(maps,0,sizeof(maps));
    memset(vis,0,sizeof(vis));
    memset(firet,0,sizeof(firet));
    scanf("%d%d",&n,&m);
    for(int i = 0 ; i < n ; i ++){
        scanf("%s",maps[i]);
        for(int j = 0 ; j < m ; j ++)
            if(maps[i][j] == F){
                fq.push(Node(i,j,1));
                firet[i][j] = 1;
            }else if(maps[i][j] == J){
                sx = i , sy = j;
            }        
    }
}
void bfs_fire(){
    while(!fq.empty()){
        Node tmp = fq.front(); fq.pop();
        int x = tmp.x , y = tmp.y;    
        for(int i = 0 ; i < 4 ; i ++){
            int xx = x + tx[i];
            int yy = y + ty[i];
            int t = tmp.t+1;
            if((maps[xx][yy] == . || maps[xx][yy] == J) 
            && (xx>=0 && xx<n && yy>=0 && yy<m) 
            && firet[xx][yy] == 0){ //着火符合条件     
                firet[xx][yy] = t;
                fq.push(Node(xx,yy,t));
            }
        }
    }    
}
void bfs_escape(){
    Node start(sx,sy,1);
    jq.push(start);
    while(!jq.empty()){
        Node tmp = jq.front(); jq.pop();
        int x = tmp.x , y = tmp.y ,  tmpt = tmp.t;
        if(escape(x,y)){    printf("%d\n",tmpt); return;    }
        for(int i = 0 ; i < 4 ; i ++){
            int xx = x + tx[i];
            int yy = y + ty[i];
            int t = tmp.t + 1;
            if(ok(xx,yy,t)){
                vis[xx][yy] = true;
                jq.push(Node(xx,yy,t));
            }
        }
    }
    printf("IMPOSSIBLE\n");
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        input();    
        bfs_fire();
        bfs_escape();
    }
    return 0;
} 

 

UVA 11624 不知道为什么WA

标签:

原文地址:http://www.cnblogs.com/zstu-jack/p/5286817.html

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