codefrces 525 D. Arthur and Walls (bfs-小矩形)
2016-08-19 09:42
399 查看
D. Arthur and Walls
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Finally it is a day when Arthur has enough money for buying an apartment. He found a great option close to the center of the city with a nice price.
Plan of the apartment found by Arthur looks like a rectangle n × m consisting of squares of size 1 × 1.
Each of those squares contains either a wall (such square is denoted by a symbol "*" on the plan) or a free space (such square is denoted on the plan by a
symbol ".").
Room in an apartment is a maximal connected area consisting of free squares. Squares are considered adjacent if they share a common side.
The old Arthur dream is to live in an apartment where all rooms are rectangles. He asks you to calculate minimum number of walls you need to remove in order to achieve this goal. After removing a wall from a square it becomes a free square. While removing the
walls it is possible that some rooms unite into a single one.
Input
The first line of the input contains two integers n, m (1 ≤ n, m ≤ 2000)
denoting the size of the Arthur apartments.
Following n lines each contain m symbols
— the plan of the apartment.
If the cell is denoted by a symbol "*" then it contains a wall.
If the cell is denoted by a symbol "." then it this cell is free from walls and also this cell is contained in some of the rooms.
Output
Output n rows each consisting of m symbols
that show how the Arthur apartment plan should look like after deleting the minimum number of walls in order to make each room (maximum connected area free from walls) be a rectangle.
If there are several possible answers, output any of them.
Examples
input
output
input
output
input
output
题目大意:矩阵'.'代表空地,'*'代表围墙,问怎样去掉最少的围墙使得到最大的矩形空地;
解:别人的想法好好o,把大矩形转为小矩形,那么有四种情况可以成为小矩形:
.. ..
.*
*.
.* *.
.. ..
用bfs搜索,把符合情况的入队,出队的时候注意判断是否在这之前'*'已经改为'.',
然后出队后,搜索对周围的影响
代码:
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Finally it is a day when Arthur has enough money for buying an apartment. He found a great option close to the center of the city with a nice price.
Plan of the apartment found by Arthur looks like a rectangle n × m consisting of squares of size 1 × 1.
Each of those squares contains either a wall (such square is denoted by a symbol "*" on the plan) or a free space (such square is denoted on the plan by a
symbol ".").
Room in an apartment is a maximal connected area consisting of free squares. Squares are considered adjacent if they share a common side.
The old Arthur dream is to live in an apartment where all rooms are rectangles. He asks you to calculate minimum number of walls you need to remove in order to achieve this goal. After removing a wall from a square it becomes a free square. While removing the
walls it is possible that some rooms unite into a single one.
Input
The first line of the input contains two integers n, m (1 ≤ n, m ≤ 2000)
denoting the size of the Arthur apartments.
Following n lines each contain m symbols
— the plan of the apartment.
If the cell is denoted by a symbol "*" then it contains a wall.
If the cell is denoted by a symbol "." then it this cell is free from walls and also this cell is contained in some of the rooms.
Output
Output n rows each consisting of m symbols
that show how the Arthur apartment plan should look like after deleting the minimum number of walls in order to make each room (maximum connected area free from walls) be a rectangle.
If there are several possible answers, output any of them.
Examples
input
5 5 .*.*. ***** .*.*. ***** .*.*.
output
.*.*. ***** .*.*. ***** .*.*.
input
6 7 ***.*.* ..*.*.* *.*.*.* *.*.*.* ..*...* *******
output
***...* ..*...* ..*...* ..*...* ..*...* *******
input
4 5 ..... ..... ..*** ..*..
output
..... ..... ..... .....
题目大意:矩阵'.'代表空地,'*'代表围墙,问怎样去掉最少的围墙使得到最大的矩形空地;
解:别人的想法好好o,把大矩形转为小矩形,那么有四种情况可以成为小矩形:
.. ..
.*
*.
.* *.
.. ..
用bfs搜索,把符合情况的入队,出队的时候注意判断是否在这之前'*'已经改为'.',
然后出队后,搜索对周围的影响
代码:
#include<cstdio> #include<cstring> #include<queue> using namespace std; char map[2010][2010]; int n,m; int check(int i,int j) { if(map[i][j]=='.'||i<0||i>=n||j<0||j>=m) return 0; if(map[i-1][j-1]=='.'&&map[i-1][j]=='.'&&map[i][j-1]=='.') return 1; if(map[i-1][j]=='.'&&map[i-1][j+1]=='.'&&map[i][j+1]=='.') return 1; if(map[i][j-1]=='.'&&map[i+1][j-1]=='.'&&map[i+1][j]=='.') return 1; if(map[i][j+1]=='.'&&map[i+1][j]=='.'&&map[i+1][j+1]=='.') return 1; return 0; } int main() { int i,j; scanf("%d%d",&n,&m); queue<pair<int,int> > q; for(i=0;i<n;i++) { scanf("%s",map[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(check(i,j)) { q.push(make_pair(i,j));//符合小矩形的入队 } } } while(!q.empty()) { int x=q.front().first; int y=q.front().second; q.pop(); //出队,并改为'.' if(!check(x,y)) continue;//可能出队前就已经被改过了 map[x][y]='.'; for(i=-2;i<=2;i++) //改了这个点之后 对周围的点的影响,符合小矩形就入队 { for(j=-2;j<=2;j++) { if(i||j&&check(x+i,y+j)) //i||是除check(x,y)之外,因为已经判断过了 q.push(make_pair(x+i,y+j)); } } } for(i=0;i<n;i++) printf("%s\n",map[i]); return 0; }
相关文章推荐
- CodeForces 525D D. Arthur and Walls(BFS)
- D. Arthur and Walls (CF 525 D 搜索bfs)
- Codeforces Round #297 (Div. 2) D题. Arthur and Walls(BFS)
- 【CodeForces 525D】【dfs+思维】 Arthur and Walls 【只包含点和星的矩阵,需要将部分星变成点使点能组成矩形 】
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
- Arthur and Walls - CodeForces 525 D 搜索
- D. Arthur and Walls(Codeforces Round #297 (Div. 2) 搜索(BFS))
- codeforces 525 D Arthur and Walls
- Codeforces Round #297 (Div. 2) D Arthur and Walls(bfs)
- Codeforces Round #297 (Div. 2) D. Arthur and Walls
- Codeforces #297( div2) D. Arthur and Walls ( DFS
- Codeforces Round #297 (Div. 2) D - Arthur and Walls [思维]【思维】
- 【CF】D. Arthur and Walls(BFS + 贪心)
- Codeforces Round #297 (Div. 2) D - Arthur and Walls
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
- 【Henu ACM Round#18 F】Arthur and Walls
- Codeforces Round #297 (Div. 2) D - Arthur and Walls (深搜)
- #297 (div.2) D. Arthur and Walls
- codeforces D - Arthur and Walls