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

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

c++

2019-11-10 19:32:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(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)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 同心县| 宁陵县| 余姚市| 嘉善县| 巴中市| 酒泉市| 鸡泽县| 太仓市| 丰原市| 安新县| 台中市| 西贡区| 鄂托克旗| 宁化县| 金昌市| 黔东| 神农架林区| 镇赉县| 二连浩特市| 公安县| 宁阳县| 慈溪市| 舟山市| 舟曲县| 芒康县| 阿巴嘎旗| 汉川市| 龙胜| 宜兰市| 瑞丽市| 潍坊市| 朝阳区| 高陵县| 凉城县| 师宗县| 左贡县| 汤原县| 民权县| 正宁县| 永丰县| 师宗县|