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

首頁(yè) > 編程 > C++ > 正文

c++

2019-11-10 19:15:57
字體:
供稿:網(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ì)弄懂的
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 清徐县| 宁南县| 泸水县| 许昌市| 寿光市| 驻马店市| 岫岩| 彰武县| 宁安市| 信宜市| 金堂县| 修武县| 息烽县| 察雅县| 阿勒泰市| 大化| 崇文区| 江安县| 漳平市| 屏东县| 格尔木市| 许昌市| 兴义市| 玉环县| 阳东县| 竹溪县| 昌江| 谢通门县| 平利县| 三江| 商洛市| 盈江县| 维西| 专栏| 格尔木市| 博白县| 长治县| 凤冈县| 吉木乃县| 库车县| 德惠市|