UVA 11624 fire!

Source: Internet
Author: User

Data:

3
4 4
####
#JF #
#.. #
#.. #
3 3
FFF
Fjf
FFF
3 3
...
. J.
...

Well, the third data card for half a day ....

Two BFS pre-processing the earliest fire time of each point.

/************************************************author:p k29created time:2015/8/19 19:26:13File name:4.c pp*************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<iomanip>#include<list>#include<deque>#include<stack>#defineull unsigned long Long#definell Long Long#defineMoD 90001#defineINF 0x3f3f3f3f#defineMAXN 10000+10#defineCLE (a) memset (A,0,sizeof (a))Constull inf = 1LL << A;Const Doubleeps=1e-5;using namespacestd;structnode{intx,y,dist;};BOOLcmpintAintb) {    returnA>b;}intdir[4][2]={1,0,0,1,0,-1,-1,0};Charmp[1100][1100];intvis[1100][1100],val[1100][1100];intN,m,sx,sy,fx,fy;queue<node>p;voidBFS1 () {CLE (VIS);    Node u,v;  while(!P.empty ()) {u=P.front (), P.pop ();  for(intI=0;i<4; i++){            intnx=u.x+dir[i][0]; intny=u.y+dir[i][1]; if(!vis[nx][ny]&&nx<=n&&ny>=1&&nx>=1&&ny<=m&&mp[nx][ny]=='.') {v.x=nx,v.y=ny,v.dist=u.dist+1; Vis[nx][ny]=1; Val[nx][ny]=v.dist;            P.push (v); }}}}queue<node>Q;voidbfs2 () {intmark=0;    CLE (VIS);  while(!q.empty ()) Q.pop ();    Node u,v; u.x=sx,u.y=sy,u.dist=0;    Q.push (U); VIS[U.X][U.Y]=1;  while(!Q.empty ()) {u=Q.front (), Q.pop (); if(u.x==n| | u.x==1|| u.y==1|| u.y==m) {Mark=1;p rintf ("%d\n", u.dist+1); Break; }         for(intI=0;i<4; i++){            intnx=u.x+dir[i][0]; intny=u.y+dir[i][1]; if(vis[nx][ny]==0&&nx<=n&&ny>=1&&nx>=1&&ny<=m&&mp[nx][ny]=='.') {v.dist=u.dist+1; if(v.dist<Val[nx][ny]) {v.x=nx,v.y=NY;                    Q.push (v); Vis[nx][ny]=1; }            }        }    }    if(!mark) printf ("impossible\n");}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif    //freopen ("OUT.txt", "w", stdout);    intT; CIN>>T;  while(t--) {cin>>n>>m;        CLE (MP); memset (Val,inf,sizeofval);  for(intI=1; i<=n;i++)             for(intj=1; j<=m;j++) {cin>>Mp[i][j]; if(mp[i][j]=='J') {SX=i,sy=J; }                if(mp[i][j]=='F') {node o;o.x=i,o.y=j,o.dist=0;                P.push (o);        }} BFS1 ();    BFS2 (); }    return 0;}

UVA 11624 fire!

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.