Acm:fzu 2150 Fire game-dfs+bfs+ Shears or pure bfs+ branch scissors

Source: Internet
Author: User
Tags cmath

Fzu 2150 Fire GameTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U

Description

Fat brother and Maze is playing a kind of special (hentai) game on an n*m board (N rows, M columns). At the beginning, each of the grids of this board are consisting of grass or just empty and then they start-to-fire all the grass. Firstly they choose, grids which is consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which are adjacent to this grid would fire at time T+1 which refers to the Grid (x+1, y), (x-1, y), (x, Y+1), (x, y-1). This process ends when the no new grid get fire. If then all the grid which be consisting of grass is get fired, Fat brother and Maze would stand in the middle of the grid And playing a more special (hentai) game. (Maybe it ' s the Ooxx game which decrypted in the last problem, who knows.)

You can assume this grass in the board would never burn out and the empty grid would never get fire.

Note that the both grids they choose can be the the same.

Input

The first line of the date is a integer T, which is the number of the text cases.

Then T cases follow, each case contains the integers N and M indicate the size of the board. Then goes-N line, each line with M character shows the board. "#" indicates the grass. You can assume this there is at least one grid which are consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

Output

For each case, output of the case number first, if they can play the more special (Hentai) game (fire all the grass), output The minimal time they need to wait after they set fire, otherwise just output-1. See the sample, input and output for more details.

Sample Input

43 3.#.###.#.3 3.#.#.#.#.3 3...#.# ... 3 3###. ##.#

Sample Output

Case 1:1case 2: -1case 3:0case 4:2


/* /Today played a team practice game, hit me miserable, the whole field in addition to the water problem seconds A, the title of the pit for a long time Ah, the last three people to force all kinds of branches, finally pressed time a This topic, wrote a fast 5K code, and when the time is 968MS, or hehe ... "= =". After the game, after the optimization of their own code, to the 500MS, the fastest one, finally comfortable, asked the other people's writing, the magic of pure BFS. Although not my optimization after the fast, but the 5K code is still a bit ... Not much to say, or to see the subject. Test instructions is, a person has two fire, he will burn down the whole playground of all grass, fire can only like four directions, along the haystack spread. Ask, whether can burn out, the minimum time is how much. /*/

 /*  /here is the first to say my code ... Code optimization to change the mess of ... First Dfs "by the way dyeing ' 1 ' 2 '", to determine if there are two of the following unicom block, if more than the mark can not burn, direct output, jump back to the loop. If DFS is judged to have 2 unicom blocks, divide the color to go separately BFS two unicom blocks to burn clean the minimum time required, each time update. The maximum time for the last two colors to be output. If DFS has only 1 unicom blocks ... Here I wrote a two-way BFS, never written before, incredibly a bit to write right! To find out how to burn the fastest, the fastest time to return. This time is updated every time to small. The minimum time to output this will be fine. But all kinds of sticks cut ... I marked the place where some branches were cut .... On the code .... AC code: "This code is written at the time of the game, a little change is too messy." Nothing below there is a pure bfs of the wording, learn that, that cost-effective higher. "/ */
#include "algorithm" #include "iostream" #include "CString" #include "vector" #include "string" #include "Cstdio" #include "Queue" #include "Cmath" using namespace std; #define MEMSET (x, y) memset (x,y,sizeof (×)) #define memcpy (y, y) memcpy ( sizeof (x)) typedef long LONG ll;const int MX = 15;struct vnode {int x, y,} vis_node[105];struct node {int x,y,t;node (int xx, int Yy,int tt): X (xx), Y (yy), T (TT) {};node () {};} ; node ans1 (0,0,99), Ans2 (0,0,99); char s[mx][mx];bool vis[mx][mx];int num;int flag=1;//bool ifgo[15][15][15][15];int Erear =0;int cnt,maxx;void BFS1 (int x,int y,int n,int m,int t,int k,int l) {queue<node > Q;while (! Q.empty ()) Q.pop ();//cout<< "s[x][y]==" <<s[x][y]<<endl;if (s[x][y]== ' 1 ' | | s[x][y]== ' 2 ') {node a;a.t=t;a.x=x;a.y=y;//vis[a.x][a.y]=1; Q.push (a); node b;b.t=t;b.x=k;b.y=l; Q.push (b);} while (! Q.empty ()) {node A=q.front (); Q.pop (); if (s[a.x][a.y]!= ' 1 ' &&s[a.x][a.y]!= ' 2 ') continue; if (Vis[a.x][a.y]) continue;if (s[a.x][a.y]== ' 1 ') Ans1=a;if (s[a.x][a.y]== ' 2 ') ans2=a;vis[a.x][a.y]=1; Avoid repeating into the queue: if (a.x+1< n&&vis[a.x+1][a.y]==0) Q.push (node (a.x+1,a.y,a.t+1)); if (a.x-1>=0&&vis[a.x-1][a.y ]==0) Q.push (node (a.x-1,a.y,a.t+1)), if (a.y+1< m&&vis[a.x][a.y+1]==0) Q.push (node (a.x,a.y+1,a.t+1)); a.y-1>=0&&vis[a.x][a.y-1]==0) Q.push (node (a.x,a.y-1,a.t+1));}} void BFS2 (int x,int y,int n,int m,int t) {Queue<node > Q;while (! Q.empty ()) Q.pop ();//cout<< "s[x][y]==" <<s[x][y]<<endl;if (s[x][y]== ' 1 ' | | s[x][y]== ' 2 ') {node a;a.t=t;a.x=x;a.y=y;//vis[a.x][a.y]=1; Q.push (a);} while (! Q.empty ()) {node A=q.front (); Q.pop (); if (s[a.x][a.y]!= ' 1 ' &&s[a.x][a.y]!= ' 2 ') continue; if (s[a.x][a.y]== ' 1 ') ans1=a;if (s[a.x][a.y]== ' 2 ')                  Ans2=a;vis[a.x][a.y]=1; Avoid repeating into the queue: if (a.x+1< n&&vis[a.x+1][a.y]==0) Q.push (node (a.x+1,a.y,a.t+1)); if (a.x-1>=0&&vis[a.x-1][a.y ]==0) Q.push (node (a.x-1,a.y,a.t+1)), if (a.y+1< m&&vis[a.x][a.y+1]==0) Q.push (node (a.x,a.y+1,a.t+1)); a.y-1>=0&&vis[a.x][a.y-1]==0) Q.push (node (a.x,a.y-1,a.t+1));}} void dfs_is_2 (int x,int y,int n,int m) {if (x<0| | y<0| | x>=n| | Y&GT;=M) return, if (Vis[x][y]) return, if (s[x][y]== ' # ') {if (flag) {flag=0;num++;} if (num==1&&s[x][y]== ' # ') s[x][y]= ' 1 ';//With ' 1 ' 2 ' staining if (num==2&&s[x][y]== ' # ') s[x][y]= ' 2 '; vis[x][y]=1  ; vis_node[erear].x=x; The array records the coordinates of the entire grassland, avoiding the BFS when you start searching from the open space. Vis_node[erear].y=y;erear++;if (x-1>=0&&vis[x-1][y]==0) dfs_is_2 (x-1,y,n,m); Avoid repeating DFS: if (x+1< n&&vis[x+1][y]==0) dfs_is_2 (x+1,y,n,m), if (y-1>=0&&vis[x][y-1]==0) DFS_is_2 (X,y-1,n, m); if (y+1< m&&vis[x][y+1]==0) dfs_is_2 (x,y+1,n,m);}} int main () {int t,n,m;scanf ("%d", &t), for (int qq=1; qq<=t; qq++) {scanf ("%d%d", &n,&m), Num=0;memset (Vis, 0); memset (s,0); int sign=1;erear=0;for (int i=0; i<n; i++) {scanf ("%s", S[i]);} for (int i=0, i<n; i++) {for (int j=0; j<m; J + +) {if (s[i][j]== '. ') Continue If this point is empty, do not go to DFS search. Flag=1;dfs_is_2 (i,j,n,m);}} if (num>2) sign=0;//puts ("");//for (int i=0;i<n;i++) puts (s[i]);//Draw Confirmation dyeing//puts (""); memset (vis,0); Maxx=0x3f3f3f3f;node ans22 (0,0,99), ANS11 (0,0,99); ans1.t=99, ans2.t=99;if {for (Int. i=0; i<erear; i++) {if (num==1) {for (int j=i; j<erear; J + +) {//This place is last optimized to avoid duplication of searches, cut off a Half of the complexity, cut from 968ms to 500ms;//cout<< "Insert OK" <<endl;memset (vis,0); BFS1 (VIS_NODE[I].X,VIS_NODE[I].Y,N,M,0,VIS_NODE[J].X,VIS_NODE[J].Y); if (ans22.t>ans2.t) ans22=ans2;if (ans11.t >ans1.t) ans11=ans1;}} else {if (s[vis_node[i].x][vis_node[i].y]== ' 1 ' | | s[vis_node[i].x][vis_node[i].y]== ' 2 ') {memset (vis,0); BFS2 (vis_node[i].x,vis_node[i].y,n,m,0); if (ans22.t>ans2.t) ans22=ans2;if (ans11.t>ans1.t) ans11=ans1;
}}}}if (sign) {if (num==0) printf (' Case%d:%d\n ', qq,0); else if (num==1) printf ("Case%d:%d\n", QQ, (ans11.t)); elseprintf ( "Case%d:%d\n", Qq,max (ans11.t,ans22.t));} else printf ("Case%d:%d\n", qq,-1);} return 0;} /* Below is the Super Data 155 5..#....#. #####.. #....#.. 5 5#####. #.. #####.. #.. # # # # # # #5 5######...##...##...##### #1 1.3 3.#.###.#.3 3.#.#.#.#.3 3...# ..... 3 3###. # #. #9 9##########.......###########.......###########.......###########.......######### #10 10################### ################################################################################ #5 3#.#.#####.##.##*/
/* /This is a form of learning from seniors. Also two-way BFS. Start the search directly at two points and use Tot to record each search burn point. If Tot=sz (the number of haystack) then output the value of T that is recorded at this time, and then maintain the value of this t to find the smallest. If you cannot burn, then return to an infinity, and finally judge whether there is an answer, output answer or-1. This is a fantastic way to use the same piece of code that I have. Time consuming 560ms, a little bit slower than my above, but the code is much less ... AC Code:/*

#include "algorithm" #include "iostream" #include "CString" #include "vector" #include "string" #include "Cstdio" #include "Queue" #include "Cmath" using namespace std; #define MEMSET (x, y) memset (x,y,sizeof (×)) #define memcpy (y, y) memcpy ( sizeof (x)) typedef long Long Ll;int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};int Sz,t,n,m;char s[12][12];bool vis[12][12] ; struct Node {int X,y,t;node () {};node (int xx,int yy,int TT): X (xx), Y (yy), T (TT) {};}; struct Togo {int x, y,} togo[200];int BFS (int u,int v,int k) {int tot=0;queue<node > q;while (! Q.empty ()) q.pop (); node a;a.x=togo[u].x,a.y=togo[u].y,a.t=k;vis[a.x][a.y]=1;//marks the beginning Q.push (a); node b;b.x=togo[v].x, b.y=togo[v].y,b.t=k;vis[b.x][b.y]=1;//Mark beginning Q.push (b); while (! Q.empty ()) {node A=q.front (); Q.pop (); tot++;if (Tot==sz) return a.t;for (int i=0; i<4; i++) {int nx=a.x+dir[i][0],ny=a.y+dir[i][1];if (nx<0| | ny<0| | ny>=m| | nx>=n| | s[nx][ny]== '. ' | | Vis[nx][ny]) continue; The judgment is out of bounds, or has been searched. Q.push (Node (nx,ny,a.t+1)), vis[nx][ny]=1;//tag has been searched, avoid repeating into the queue. }}return 1e9+5;} InchT Main () {scanf ("%d", &t), for (int qq=1; qq<=t; qq++) {sz=0;scanf ("%d%d", &n,&m), for (int i=0; i<n; i++) {  scanf ("%s", S[i]), for (int j=0; j<m; J + +) {if (s[i][j]== ' # ') {togo[sz].x=i;//) The haystack coordinates are pressed into the array togo[sz].y=j; sz++;}}} int ans = 1e9+5;int c=ans;for (int i=0; i<sz; i++) {for (int j=i; j<sz; J + +) {//Avoid re-search, same as my last optimization. memset (vis,0); Ans=min (Ans,bfs (i,j,0));}} printf ("Case%d:%d\n", Qq,ans==c -1:ans);} return 0;}

  

Acm:fzu 2150 Fire game-dfs+bfs+ Shears or pure bfs+ branch scissors

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.