Codeforces #297( div2) D. Arthur and Walls ( DFS
来源:互联网 发布:淘宝助理找不到图片 编辑:程序博客网 时间:2024/06/02 07:31
D. Arthur and Walls
Description
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 Input
5 5.*.*.*****.*.*.*****.*.*.
Sample Output
.*.*.*****.*.*.*****.*.*.
题意
给一个
很有思维的DFS我们知道 矩形最小是
AC代码
#include <bits/stdc++.h>using namespace std;#define LL long long#define CLR(a,b) memset(a,(b),sizeof(a))const int MAXN = 2e3+10;char mps[MAXN][MAXN];int dx[] = {0,1,1,0};int dy[] = {1,1,0,0};int n, m;void dfs(int x, int y){ if(x<0 || x>=n-1 || y<0 || y>=m-1) return; int res = 0; int fx, fy; for(int i = 0; i < 4; i++) { int xx = x+dx[i]; int yy = y+dy[i]; if(mps[xx][yy] == '*') { res++; fx = xx; fy = yy; } } if(res == 1) { mps[fx][fy] ='.'; for(int i = -1; i <= 1; i++) { for(int j = -1; j <= 1; j++) { dfs(fx+i,fy+j); } } }}int main(){ scanf("%d%d",&n,&m); for(int i = 0; i < n; i++) scanf("%s",mps[i]); for(int i = 0; i < n-1; i++) { for(int j = 0;j < m-1; j++) { dfs(i,j); } } for(int i = 0; i < n; i++) printf("%s\n",mps[i]);return 0;}
- Codeforces #297( div2) D. Arthur and Walls ( DFS
- Codeforces 525D Arthur and Walls 【DFS】
- Codeforces 525D Arthur and Walls 【DFS】
- CodeForces 525D Arthur and Walls(DIV2 D) (DFS+YY)
- Codeforces--525D--Arthur and Walls(DFS)
- Codeforces 525D Arthur and Walls 【Dfs+思维】
- DFS(思维) CodeForces - 525D Arthur and Walls
- Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
- Codeforces Round #297 (Div. 2) D. Arthur and Walls
- Codeforces Round #297 (Div. 2) D - Arthur and Walls
- Codeforces Round #297 (Div. 2) D Arthur and Walls
- codeforces 525 D Arthur and Walls
- codeforces 525D Arthur and Walls
- Arthur and Walls - CodeForces 525 D 搜索
- #297 (div.2) D. Arthur and Walls
- CodeForces 525D D. Arthur and Walls(BFS)
- CodeForces - 525D Arthur and Walls(bfs)
- Codeforces Round #297 (Div. 2) -- D. Arthur and Walls (判断矩形)
- Unity3d-NGUI的原理机制:深入剖析UIPanel,UIWidget,UIDrawCall底层原理
- UVa 100|The 3n+1 Problem|暴力|坑点多
- Linux系统安装(手动分区挂载)
- C++ Primer练习题
- composer下载安装laravel太慢解决方法
- Codeforces #297( div2) D. Arthur and Walls ( DFS
- Unity3d导入工程出现错误“Creating unique file”的解决方法
- 2014-03-24-sqlserver-SSIS问题
- Java Webservice指定超时时间
- setInterval 和 setTimeout 的几种传参方式
- python基础 list常用方法
- Python趣事-1
- Spring Aop中四个重要概念,切点,切面,连接点,通知
- django中HttpRequest对象