CodeForces 525D D. Arthur and Walls(BFS)

来源:互联网 发布:linux oracle安装教程 编辑:程序博客网 时间:2024/06/06 05:34

题目链接: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 0
原创粉丝点击