Fzu 2150 Fire Game (violent BFS)

Source: Internet
Author: User

" Topic link " click here~~

"To the effect of the topic ":

Two bear children to burn a square on the grass, they set fire at the same time, burning the first piece of time is not spent, each fire can be burned in the next second about four # on behalf of the grass. Ask you to spend at least how much time can burn, if cannot burn off on output-1

" problem-solving ideas ":

Data is weak in the case of direct violence enumeration on each lawn can be placed on the position, relatively high-end writing at present did not think, later thought of the article updated under ~ ~

PS: Because of a detail did not pay attention, led to WA almost a page, also thought fzu judgment problem, and then suddenly found every time from the queue to take out the elements of the team, is the most likely to compare with Maxx time!

Code:

Fzu 2150#ifndef _glibcxx_no_assert#include <cassert> #endif # include <cctype> #include <cerrno># Include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath > #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include < cstdio> #include <cstdlib> #include <cstring> #include <ctime>//C + + #include <algorithm># Include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream > #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include < iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include < ostream> #include <queue> #include <set> #include <sstream> #include <stack&GT, #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector>using namespace std; #define REP (i,j,k) for (int i= (int) j;i< (int) k;++i) #define PER (i,j,k) for (int i= (int) j;i> (int) k;--i) #define LOWBIT (a) A&-a#define Max (A, B) a >b?a:b#define Min (A, B) a>b?b:a#define mem (A, B) memset (A,b,sizeof (a)) typedef long Long ll;typedef unsigned long Long llu;typedef double db;const int n=105;const int inf=0x3f3f3f3f;int n,m,t;char mat[25][25];bool vis[n][n];int dir4[4] [2]= {{1,0},{0,1},{-1,0},{0,-1}};int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};int dir6[    6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};///six-direction struct node{int coor_x;    int coor_y; int step;}    Q,p,mapp[n];bool ok (int dx,int dy) {if (dx>=0&&dx<n&&dy>=0&&dy<m) return true; return false;}    int ans;void Getmat () {ans=0; for (int i=0; i<n;        ++i) {scanf ("%s", Mat[i]);                for (int j=0; j<m; ++j) {if (mat[i][j]== ' # ') {ans++; Mapp[ans].                Coor_x=i; Mapp[ans].            Coor_y=j;    }}}}int bfs (int x1,int y1,int x2,int y2) {int maxx=0;    Q.coor_x=x1,q.coor_y=y1,q.step=0;    P.coor_x=x2,p.coor_y=y2,p.step=0;    Queue <node> Vall;    Vall.push (q);    Vall.push (P);        while (!vall.empty ()) {node Q1,p1=vall.front ();        Vall.pop (); for (int i=0; i<4; ++i) {int dx=p1.            COOR_X+DIR4[I][0]; int DY=P1.            COOR_Y+DIR4[I][1];                if (OK (dx,dy) &&!vis[dx][dy]&&mat[dx][dy]== ' # ') {vis[dx][dy]=true; Q1.                COOR_X=DX; Q1.                Coor_y=dy;                q1.step=p1.step+1;            Vall.push (Q1); }} Maxx=max (Maxx,p1.step);///Tai Hang! , note and Vall.front () compare} return Maxx;}    int main () {scanf ("%d", &t);    int tot=1;   while (t--) {     scanf ("%d%d", &n,&m);        Getmat ();        printf ("Case%d:", tot++);            if (ans<=2) {puts ("0");        Continue        } int minn=inf;                for (int i=0, i<ans; ++i) {for (int j=i; j<ans; ++j) {mem (vis,false); Vis[mapp[i]. Coor_x][mapp[i].                Coor_y]=true; VIS[MAPP[J]. COOR_X][MAPP[J].                Coor_y]=true;                BOOL Flag=false; int _minn=bfs (Mapp[i]. Coor_x,mapp[i]. COOR_Y,MAPP[J]. COOR_X,MAPP[J].                COOR_Y); for (int k=0, k<n; ++k) {for (int l=0; l<m; ++l) {if (mat[k][l]!= ' # ') continu                        E                            if (!vis[k][l]) {flag=true;                        Break            }}} if (!flag) minn=min (_minn,minn);        }} if (Minn==inf) puts ("-1");    else printf ("%d\n", Minn); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fzu 2150 Fire Game (violent 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.