国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

c++

2019-11-10 22:13:28
字體:
供稿:網(wǎng)友
Description

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. 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 at time t, the grid which is adjacent to this grid will 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 no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will 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 that the grass in the board would never burn out and the empty grid would never get fire.

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

Input

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

Then T cases follow, each case contains two 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 that there is at least one grid which is consisting of grass in the board.

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

Output

For each case, output 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直接枚舉兩個(gè)點(diǎn)用bfs判斷即可。Description

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. 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 at time t, the grid which is adjacent to this grid will 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 no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will 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 that the grass in the board would never burn out and the empty grid would never get fire.

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

Input

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

Then T cases follow, each case contains two 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 that there is at least one grid which is consisting of grass in the board.

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

Output

For each case, output 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直接枚舉兩個(gè)點(diǎn)用bfs判斷即可。
#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;const int maxn = 15;int T, n, m, tot, ans;int mp[maxn][maxn];int t[maxn*maxn];char s[maxn];int a[4] = { 0, 0, 1, -1 };int b[4] = { 1, -1, 0, 0 };void bfs(int x, int y){	memset(t, 1, sizeof(t));	queue<int> p;	p.push(x);	if (x != y) p.push(y);	t[x] = t[y] = 0;	int cnt = 0, Max = 0;	while (!p.empty())	{		int q = p.front();	p.pop();		cnt++;	Max = max(Max, t[q]);		int X = (q - 1) / m + 1, Y = (q - 1) % m + 1;		for (int i = 0; i < 4; i++)		{			if (!mp[X + a[i]][Y + b[i]]) continue;			int QQ = (X + a[i] - 1)*m + (Y + b[i]);			if (t[qq]>t[q] + 1)			{				t[qq] = t[q] + 1;				p.push(qq);			}		}	}	if (cnt == tot)	{		if (Max < ans || ans == -1) ans = Max;	}}	int main(){	scanf("%d", &T);	for (int cas = 1; cas <= T; cas++)	{		scanf("%d%d", &n, &m);		tot = 0;		memset(mp, 0, sizeof(mp));		for (int i = 1; i <= n; i++)		{			scanf("%s", s);			for (int j = 1; j <= m; j++)			{				if (s[j - 1] == '#')				{					tot += (mp[i][j] = 1);				}			}		}		ans = -1;		for (int i = 1; i <= n*m; i++)		{			for (int j = 1; j <= n*m; j++)			{				if (!mp[(i - 1) / m + 1][(i - 1) % m + 1]) continue;				if (!mp[(j - 1) / m + 1][(j - 1) % m + 1]) continue;				bfs(i, j);			}		}		printf("Case %d: %d/n", cas, ans);	}	return 0;}雖然我現(xiàn)在還不懂什么bfs  但是這個(gè)程序我以后一定會(huì)弄懂的
上一篇:C++14特性

下一篇:C++11特性

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 工布江达县| 邵阳县| 勐海县| 桃源县| 本溪市| 丰原市| 漳浦县| 百色市| 玛曲县| 荃湾区| 沾益县| 大足县| 友谊县| 乐平市| 寿阳县| 惠水县| 门源| 华安县| 邵阳县| 沙田区| 蓬安县| 韶山市| 潮州市| 泰安市| 崇礼县| 芮城县| 吕梁市| 青海省| 新乡县| 珠海市| 廊坊市| 海林市| 西丰县| 鄂托克旗| 璧山县| 新泰市| 塘沽区| 大渡口区| 海南省| 晋城| 灌南县|