CodeForces 525D D. Arthur and Walls(BFS)

时间:2015-07-07 09:29:24   收藏:0   阅读:109

题目链接:http://codeforces.com/problemset/problem/525/D

题意:n*m的格子,‘*’代表墙壁,‘.’代表房间,要求房间都必须是矩形,输出改动后的 n*m;

思路:看了官方题解,思路蛮巧妙的。因为要求一定是矩形,所有在每个2*2的格子里,若有3个‘.’和1个‘*’,那么就将‘*’改成‘.’,这样就能确保房间一定是矩形了。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

const int N = 2010;
int n , m;
char map[N][N];

void bfs(int x, int y)
{
	if(x == n-1 || y == m-1 || x < 0 || y < 0) return;
	int cnt = 0, tx, ty;
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 2; j++)
		{
			if(map[x+i][y+j] == '*')
				cnt++, tx = x + i, ty = y + j;
		}
	}
	if(cnt == 1)
	{
		map[tx][ty] = '.';
		for(int i = -1; i < 1; i++)
		{
			for(int j = -1; j < 1; j++)
				bfs(tx + i, ty + j);
		}
	}
}

int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++)
		scanf("%s", map[i]);
	for(int i = 0; i < n-1; i++)
	{
		for(int j = 0; j < m-1; j++)
		{
			bfs(i,j);
		}
	}
	for(int i = 0; i < n; i++)
		printf("%s\n", map[i]);
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!