Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
2 seconds
512 megabytes
standard input
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.
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 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.
- 5 5
.*.*.
*****
.*.*.
*****
.*.*.
- .*.*.
*****
.*.*.
*****
.*.*.
- 6 7
***.*.*
..*.*.*
*.*.*.*
*.*.*.*
..*...*
*******
- ***...*
..*...*
..*...*
..*...*
..*...*
*******
- 4 5
.....
.....
..***
..*..
- .....
.....
.....
.....- 先转一发官方题解:
http://codeforces.ru/blog/entry/17119?locale=en
525D — Arthur and Walls
To solve this problem we need to observe next fact. If in some square whith size 2 × 2 in given matrix there is exactly one asterisk, we must change it on dot. That is if in matrix from dots and asterisks is not square 2 × 2 in which exactly one asterisk and three dots, then all maximum size of the area from dots connected by sides represent rectangles.
Now solve the problem with help of bfs and this fact. Iterate on all asterisks in given matrix and if only this asterisk contains in some 2 × 2 square, change this asterisk on dot and put this position in queue. Than we need to write standart bfs, in which we will change asterisks on dots in all come out 2 × 2 squares with exactly one asterisk.
Asymptotic behavior of this solution — O(n * m), where n and m sizes of given matrix.
题意:
将 由 '.' 构成的联通区域变成矩形(通过将 '*' 改成 '.' )
题解:
很不错的一个想法,2*2 的矩形中,如果有一个 '*' 与三个 '.' ,那么这个 '*' 就一定要变成 ‘.' ,然后就bfs 找类似这种 2*2的矩形。
10495549 | 2015-03-28 05:12:33 | njczy2010 | D - Arthur and Walls | GNU C++ | Accepted | 732 ms | 27100 KB |
- #include <cstdio>
- #include <cstring>
- #include <stack>
- #include <vector>
- #include <map>
- #include <algorithm>
- #include <queue>
- #define ll long long
- int const N = ;
- int const M = ;
- int const inf = ;
- ll const mod = ;
- using namespace std;
- int n,m;
- char s[N][N];
- int dirx[]={-,-,,,,,,-};
- int diry[]={,,,,,-,-,-};
- int used[N][N];
- typedef struct
- {
- int x;
- int y;
- }PP;
- void ini()
- {
- int i;
- for(i=;i<=n;i++){
- scanf("%s",s[i]+);
- }
- memset(used,,sizeof(used));
- }
- int isok(int x,int y)
- {
- if(x>= && x<=n && y>= && y<=m && s[x][y]=='.'){
- return ;
- }
- else{
- return ;
- }
- }
- int check(int x,int y)
- {
- if(x< || x>n || y< || y>m || s[x][y]=='.') return ;
- int i;
- int d[];
- for(i=;i<;i++){
- d[i]=isok(x+dirx[i],y+diry[i]);
- }
- d[]=d[];
- // printf(" x=%d y=%d\n",x,y);
- // for(i=0;i<=8;i++){
- // printf(" i=%d d=%d\n",i,d[i]);
- // }
- for(i=;i<;i+=){
- //printf(" i=%d d=%d\n",i,d[i]);
- if(d[i] && d[i+] && d[i+])
- return ;
- }
- return ;
- }
- void solve()
- {
- queue< PP > que;
- PP te,nt;
- int i,j;
- for(i=;i<=n;i++){
- for(j=;j<=m;j++){
- if(check(i,j)==){
- te.x=i;te.y=j;
- //printf(" x=%d y=%d\n",te.x,te.y);
- que.push(te);
- used[i][j]=;
- }
- }
- }
- while(que.size()>=){
- te=que.front();que.pop();
- //printf(" x=%d y=%d\n",te.x,te.y);
- s[te.x][te.y]='.';
- for(i=;i<;i++){
- nt.x=te.x+dirx[i];
- nt.y=te.y+diry[i];
- if(used[nt.x][nt.y]== && check(nt.x,nt.y)==){
- used[nt.x][nt.y]=;
- que.push(nt);
- }
- }
- }
- }
- void out()
- {
- int i;
- for(i=;i<=n;i++){
- printf("%s\n",s[i]+);
- }
- }
- int main()
- {
- //freopen("data.in","r",stdin);
- // freopen("data.out","w",stdout);
- //scanf("%d",&T);
- //for(int cnt=1;cnt<=T;cnt++)
- //while(T--)
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- ini();
- solve();
- out();
- }
- }
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 Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
随机推荐
- JS进阶-特殊形式的函数-返回函数的函数/重写自己的函数
返回函数的函数 // 返回函数的函数 function a() { alert("aa"); return function () { alert("bb"); ...
- IOS之GCD记录
在 GCD 中,加入了两个非常重要的概念: 任务 和 队列. 任务:即操作,你想要干什么,说白了就是一段代码,在 GCD 中就是一个 Block,所以添加任务十分方便.任务有两种执行方式: 同步执行 ...
- Jenkins执行sudo权限的设置
Jenkins系统中添加执行脚本的时候,有一些命令是需要sudo权限和来执行的,可以在root权限下添加一下Jenkins账号的权限 1.添加不需要密码可sudo执行指定命令的权限 cd /etc c ...
- 微信小程序开发系列四:微信小程序之控制器的初始化逻辑
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 这个教程的前两篇文章,介绍了如何 ...
- 1-3 编程基础 makefile工程管理
GNU make Linux程序员必须学会使用GNU make来构建和管理自己的软件工程.GNU的make能够使整个工程的编译.链接只需要一个命令就可以完成. makefile make在执行时,需要 ...
- hasOneOf # if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) {
if (data.otherDescArr.some(_ => '7'.indexOf(_) > -1)) { export const hasOneOf = (targetarr, ar ...
- iview upload on-format-error 事件 在 before-upload 事件 之后,导致在before里面阻止上传后,监测事件失效,需要自己手工写
iview upload on-format-error 事件 在 before-upload 事件 之后,导致在before里面阻止上传后,监测事件失效,需要自己手工写
- Qt 之 QApplication
1.QApplication QApplication类管理GUI程序的控制流和主要设置,是基于QWidget的,为此特化了QGuiApplication的一些功能,处理QWidget特有的初始化和结 ...
- 【搜索】P1468 派对灯 Party Lamps
P1468 派对灯 Party Lamps 我们来分析一下对灯的操作 1.对所有灯的,这时吧所有灯看成一个整体 2.奇偶数的操作,这时可以把每两个数看成一个循环节 3.对3X+ 1的操作,这时可以把每 ...
- 使用plsql导入dmp文件缺少imp*.exe
在C:\app\Administrator\product\11.2.0\client_2\BIN 找到imp.exe 导入