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

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

c++

2019-11-10 19:46:52
字體:
來源:轉載
供稿:網友
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直接枚舉兩個點用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直接枚舉兩個點用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;}雖然我現在還不懂什么bfs  但是這個程序我以后一定會弄懂的
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 区。| 桃园县| 年辖:市辖区| 枣庄市| 格尔木市| 军事| 建始县| 金门县| 师宗县| 黄大仙区| 新建县| 横峰县| 明溪县| 延庆县| 南宫市| 香格里拉县| 商南县| 安多县| 科技| 荆州市| 乌鲁木齐市| 辽源市| 临邑县| 荔波县| 通化县| 昌宁县| 河北区| 镇原县| 剑河县| 祁东县| 临湘市| 巴彦淖尔市| 集贤县| 镇安县| 西峡县| 远安县| 鹿邑县| 兴化市| 宁国市| 湘西| 武宣县|