FZU-2150 Fire Game (two point BFs) __ Search

Source: Internet
Author: User
Tags cas
Problem 2150 Fire Game accept:623 submit:2445
Time limit:1000 mSec Memory limit:32768 KB
Problem Description

Fat brother and Maze are playing a kind of special (hentai) game on a n*m board (N rows, M columns). At the beginning, each grid of this board are consisting of grass or just empty and then all the They. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing in time t, the grid which are adjacent to this grid would fire at time T+1 which to the Grid (x+1, y), (x-1, y), (x, Y+1), (x, y-1). This process ends as no new grid get fire. If then all the grid which are consisting of grass are get fired, Fat brother and Maze would stand in the middle of the 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 to get never.

The two grids they choose can be the same. Input

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

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

For each case, output the case number I, if they can play the more special (Hentai) game (fire all grass), output The minimal time they need to wait after they set fire, otherwise just. The sample input and output for more details. Sample Input 4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
.. #
#.# Sample Output case 1:1
Case 2:-1
Case 3:0
Case 4:2


Two bear children on the plains of N*m, #表示草, two bear children choose a # lattice ignition, the fire can be up and down to the left to the right in the grass lattice spread, ignition of the place time is 0, spread to the next time in turn add one. Ask for the minimum time required to burn all the grass. If you can't burn out the output-1.

Enumerate two # lattices in sequence, BFS the required time and take the smallest one.

#include <iostream> #include <cstring> #include <algorithm> #include <string> #include <
Cstdio> #include <queue> using namespace std;
	#define INF 0x3f3f3f struct Point {int x;
int y;

};
int n, m;
Char map[15][15];
int dis[15][15];
queue<point>que;
int dirx[4] = {1,-1, 0, 0};

int diry[4] = {0, 0,-1, 1};
	int BFS (int x1, int y1, int x2, int y2) {int xx, yy;
	Point P1, p2, cur, nex;
	memset (DIS, inf, sizeof (DIS)); p1.x = x1;
	P1.y = y1; p2.x = x2;
	P2.y = y2;
	Dis[x1][y1] = 0;
	Dis[x2][y2] = 0;
	Que.push (p1);
	Que.push (p2);
		while (!que.empty ()) {cur = Que.front ();
		Que.pop ();
			for (int i = 0; i<4; i++) {xx = cur.x + dirx[i];
			yy = Cur.y + diry[i]; if (xx >= 0 && xx<n&&yy>=0 && yy<m&&map[xx][yy] = = ' # ' &&dis[xx][yy]
				&GT;DIS[CUR.X][CUR.Y] + 1) {Dis[xx][yy] = Dis[cur.x][cur.y] + 1;
				nex.x = XX;
				nex.y = yy;
			Que.push (NEX);
	'} ' int maxx = 0; For(int i = 0; i < n;i++) for (int j = 0; J < m; j +) if (map[i][j] = = ' # ') {Maxx = Max (Maxx, Dis[i][j]);
Return Maxx;
	int main () {int casen;
	Cin >> Casen;
		for (int cas = 1; CAs <= casen. cas++) {while (!que.empty ()) Que.pop ();

		CIN >> n >> m;
	
		for (int i = 0; i < n; i++) cin >> Map[i];
		int temp;
		int ans = inf;
		for (int i = 0, i < n;i++) for (int j = 0; J < m;j++) if (map[i][j] = = ' # ') for (int II = 0; II < n;ii++)
			for (int jj = 0; JJ < m;jj++) if (map[ii][jj] = = ' # ') {temp = BFS (i, J, II, JJ);
		ans = min (ans, temp);     if (ans = = inf) ans =-1;
	The various points of the enumeration exist with grass not burned to cout << "case" << cas << ": << ans << endl; }
}



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.