Arthur and Walls - CodeForces 525 D 搜索
2015-04-16 21:53
435 查看
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.
Sample test(s)
input
output
input
output
input
output
题意:*是墙,.是空点,要求拆墙使得所有连续的空点必须组成矩形,并且拆最少的墙。输出最后的结果。
思路:如果对于一个墙点,其周围的8个点中,有三个空点组成一个角,那么这个墙就要拆。一直搜索直到最后不存在这样的点。
AC代码如下:
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.
Sample test(s)
input
5 5 .*.*. ***** .*.*. ***** .*.*.
output
.*.*. ***** .*.*. ***** .*.*.
input
6 7 ***.*.* ..*.*.* *.*.*.* *.*.*.* ..*...* *******
output
***...* ..*...* ..*...* ..*...* ..*...* *******
input
4 5 ..... ..... ..*** ..*..
output
..... ..... ..... .....
题意:*是墙,.是空点,要求拆墙使得所有连续的空点必须组成矩形,并且拆最少的墙。输出最后的结果。
思路:如果对于一个墙点,其周围的8个点中,有三个空点组成一个角,那么这个墙就要拆。一直搜索直到最后不存在这样的点。
AC代码如下:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,m; char s[2010][2010]; int _x[8]={-1,-1,0,1,1,1,0,-1},_y[8]={0,1,1,1,0,-1,-1,-1}; void solve(int x,int y) { if(s[x][y]=='.') return; if(s[x-1][y-1]=='.' && s[x-1][y]=='.' && s[x][y-1]=='.') s[x][y]='.'; else if(s[x-1][y+1]=='.' && s[x-1][y]=='.' && s[x][y+1]=='.') s[x][y]='.'; else if(s[x+1][y+1]=='.' && s[x+1][y]=='.' && s[x][y+1]=='.') s[x][y]='.'; else if(s[x+1][y-1]=='.' && s[x+1][y]=='.' && s[x][y-1]=='.') s[x][y]='.'; if(s[x][y]=='.') { for(int k=0;k<8;k++) solve(x+_x[k],y+_y[k]); } } int main() { int i,j,k; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%s",s[i]+1); for(i=0;i<=m+1;i++) s[0][i]=s[n+1][i]='*'; for(i=0;i<=n+1;i++) s[i][0]=s[i][m+1]='*'; for(i=1;i<=n;i++) for(j=1;j<=m;j++) solve(i,j); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) printf("%c",s[i][j]); printf("\n"); } }
相关文章推荐
- D. Arthur and Walls (CF 525 D 搜索bfs)
- CodeForces 525D D. Arthur and Walls(BFS)
- codeforces 525 D Arthur and Walls
- codeforces D - Arthur and Walls
- 【CodeForces 525D】【dfs+思维】 Arthur and Walls 【只包含点和星的矩阵,需要将部分星变成点使点能组成矩形 】
- D. Arthur and Walls(Codeforces Round #297 (Div. 2) 搜索(BFS))
- codefrces 525 D. Arthur and Walls (bfs-小矩形)
- Codeforces--525D--Arthur and Walls(DFS)
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
- Codeforces Round #297 (Div. 2) D - Arthur and Walls
- codeforces 785 C. Anton and Fairy Tale (数学 二分搜索)
- CodeForces 586D Phillip and Trains (基础搜索 -- DFS)
- Codeforces Round #297 (Div. 2) D - Arthur and Walls [思维]【思维】
- CodeForces 164A Variable, or There and Back Again 搜索
- CodeForces 164A Variable, or There and Back Again(搜索)
- CodeForces 611B - New Year and Old Property(搜索)
- 【Henu ACM Round#18 F】Arthur and Walls
- Codeforces Round #297 (Div. 2) D题. Arthur and Walls(BFS)
- codeforces 508 E. Arthur and Brackets