Fire UVA 11624

Source: Internet
Author: User

Topic Connection: http://acm.hust.edu.cn/vjudge/problem/28833

/*first to the entire picture BFS once get flame burning timetable after the BFS search path with a fire table judgment pit point is: If the timetable equals 0 should be never burned ... If you don't differentiate it, WA*/#include<bits/stdc++.h>#defineScan (x) scanf ("%d", &x)#defineM (x) memset (x,0,sizeof (x))#defineREF (i,n) for (int i=1;i<=n;i++)using namespacestd;Const intmax=1e3+Ten;CharMat[max][max];intBook[max][max];intVis[max][max];intn,m,stx,sty;structnode{intX,y,step; Node () {x=y=step=0;} Node (intXxintYyintss): X (xx), Y (yy), step (ss) {}};intnex[4][2]={0,1,1,0,0,-1,-1,0};queue<node>que1;voidbfs1 () {M (book);    M (VIS);    Node u,v;  while(!Que1.empty ()) {u=Que1.front ();        Que1.pop ();  for(intk=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&GT;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); }    }}intbfs2 () {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(intk=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&GT;M)returnV.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;//You can't rule out a point that never catches fire.            if(Vis[v.x][v.y])Continue; VIS[V.X][V.Y]=1;        Que.push (v); }    }    return-1;}intMain () {intT;  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 (); intans=BFS2 (); if(ans!=-1) cout<<ans<<Endl; Elsecout<<"Impossible"<<Endl; }    return 0;}

Fire UVA 11624

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.