Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
2015-12-23 23:42
295 查看
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.
Sample test(s)
input
output
input
output
input
output
题目链接:点击打开链接
给出n * m的地图, '*'代表墙, '.'代表房间, 墙可以变为房间, 现要求房间为长方形, 问最少需要变化后的房间.
dfs的条件是四个点组成的形状只有一个'*', 于是将这个墙变为房间, 继续dfs, 可以使得变化最少.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 2005;
int n, m;
char maze[MAXN][MAXN];
void Dfs(int x, int y)
{
if(x >= n - 1 || y >= m - 1 || x < 0 || y < 0) return;
int xx, yy, s = 0;
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
if(maze[x + i][y + j] == '*') {
s++;
xx = x + i;
yy = y + j;
}
if(s == 1) {
maze[xx][yy] = '.';
for(int i = -1; i < 1; ++i)
for(int j = -1; j < 1; ++j)
Dfs(xx + i, yy + j);
}
}
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", maze[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", maze[i]);
return 0;
}
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
..... ..... ..... .....
题目链接:点击打开链接
给出n * m的地图, '*'代表墙, '.'代表房间, 墙可以变为房间, 现要求房间为长方形, 问最少需要变化后的房间.
dfs的条件是四个点组成的形状只有一个'*', 于是将这个墙变为房间, 继续dfs, 可以使得变化最少.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 2005;
int n, m;
char maze[MAXN][MAXN];
void Dfs(int x, int y)
{
if(x >= n - 1 || y >= m - 1 || x < 0 || y < 0) return;
int xx, yy, s = 0;
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
if(maze[x + i][y + j] == '*') {
s++;
xx = x + i;
yy = y + j;
}
if(s == 1) {
maze[xx][yy] = '.';
for(int i = -1; i < 1; ++i)
for(int j = -1; j < 1; ++j)
Dfs(xx + i, yy + j);
}
}
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", maze[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", maze[i]);
return 0;
}
相关文章推荐
- spring+mybatis返回json日期处理(日期全局处理相差8个小时解决方法)
- codeforces 301B. Yaroslav and Time(下标的艺术)
- 源码探索系列10---替代Listview的RecycleView
- 欢迎使用CSDN-markdown编辑器
- 在线网页编辑器全功能版
- pymongo带认证连接mongo
- kickstart部署
- 2-6-静态链表-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- 6S大气传输模型修改源码添加、自定义卫星光谱响应(以HJ-1B CCD为例)
- 浅谈产品测试人员的KPI
- LeetCode 258. Add Digits
- RippleDrawable 的简单使用
- Servlet之过滤器
- 解决方法:64位系统-未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
- MySQL复制 -- binlog(2)
- hdu 4638 Group 莫队算法
- c#面试题
- 修改lenet网络进行训练(一)
- [转]一步步使用3Dmax+Photoshop为游戏关卡设计瓦片化3D模型和无缝贴图(视频教程)
- c#面试题