Simple UVA 11624 fire! , BFS.

Source: Internet
Author: User

Description

Joe works in a maze. Unfortunately, portions of the maze has 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 is on fire, you must determine whether Joe can exit the MA Ze before the fire reaches him, and what fast he can do it.

Joe and the fire all move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square, that's on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire could enter a square that's occupied by a wall.

The question is the maze problem, and then added the fire, but this problem is a pit, the original fire is not just one, can have a lot of ...

This problem first from the fire BFS again, so that you can know how long after the fire burned to a piece, and then from the person BFS, when the fire has been burned over the words even if they can not pass the good.

The code is as follows:

#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>using namespacestd;BOOLmap1[1005][1005];intrem[1005][1005];intfrem[1005][1005];intn,m;intsi,sj,fi[1000006],fj[1000006];intcou;BOOLJudgeintXintYinttemp) {    if(x<=0|| y<=0|| x>n| | Y>M)return 0; if(!Map1[x][y])return 0; if(Rem[x][y])return 0; if(frem[x][y]>-1&&temp>=Frem[x][y])return 0; return 1;}BOOLJudge1 (intXinty) {    if(x<=0|| y<=0|| x>n| | Y>M)return 0; if(!Map1[x][y])return 0; if(frem[x][y]!=-1)        return 0; return 1;}intSlove () {Queue<int>que; inttemp,t1,t2; Que.push (Si*1010+Sj); REM[SI][SJ]=1;  while(!Que.empty ()) {Temp=Que.front ();        Que.pop (); T1=temp/1010; T2=temp%1010; Temp=REM[T1][T2]; if(t1==1|| t2==1|| t1==n| | t2==M)returntemp; --T1; if(judge (t1,t2,temp)) {Que.push (T1*1010+T2); REM[T1][T2]=temp+1; } T1+=2; if(judge (t1,t2,temp)) {Que.push (T1*1010+T2); REM[T1][T2]=temp+1; }        --T1; --T2; if(judge (t1,t2,temp)) {Que.push (T1*1010+T2); REM[T1][T2]=temp+1; } T2+=2; if(judge (t1,t2,temp)) {Que.push (T1*1010+T2); REM[T1][T2]=temp+1; }    }    return-1;}voidinit () {Queue<int>que; inttemp,t1,t2;  for(intI=0; i<cou;++i) {Que.push (Fi[i]*1010+Fj[i]); Frem[fi[i]][fj[i]]=0; }     while(!Que.empty ()) {Temp=Que.front ();        Que.pop (); T1=temp/1010; T2=temp%1010; Temp=FREM[T1][T2]; --T1; if(Judge1 (t1,t2)) {Que.push (T1*1010+T2); FREM[T1][T2]=temp+1; } T1+=2; if(Judge1 (t1,t2)) {Que.push (T1*1010+T2); FREM[T1][T2]=temp+1; }        --T1; --T2; if(Judge1 (t1,t2)) {Que.push (T1*1010+T2); FREM[T1][T2]=temp+1; } T2+=2; if(Judge1 (t1,t2)) {Que.push (T1*1010+T2); FREM[T1][T2]=temp+1; }    }}intMain () {Ios::sync_with_stdio (false); intT; intans; CharC; CIN>>u;  while(t--) {cou=0; CIN>>N>>M;  for(intI=1; i<=n;++i) for(intj=1; j<=m;++j) {Rem[i][j]=0; FREM[I][J]=-1; CIN>>C; if(c=='#'|| c=='F') Map1[i][j]=0; ElseMap1[i][j]=1; if(c=='J') Si=i,sj=J; if(c=='F') {Fi[cou]=i; Fj[cou++]=J;        }} init (); Ans=Slove (); if(ans==-1) cout<<"impossible\n"; Elsecout<<ans<<Endl; }    return 0;}
View Code

Simple UVA 11624 fire! , BFS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.