【CodeForces 525D】【dfs+思维】 Arthur and Walls 【只包含点和星的矩阵,需要将部分星变成点使点能组成矩形 】
2016-10-04 23:49
681 查看
传送门:D. Arthur and Walls
描述:
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
题意:
给出一个n*m(1 ≤ n, m ≤ 2000)的表格,里面有'*'和'.',求把最少的'*'移除掉,使得'.'所在的连通块是矩形。
思路:
2*2地考虑,如果2*2的格子里只有一个'*',说明这个'*'要去掉,其他情况都不用去掉。然后去掉这个'*'后,又会对其他四个格子有影响。 只要想到这个,就很好办了。
复杂度不太好估计。
推荐和CF 723D一起食用,题解见Here
代码:
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using namespace std;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
const int N=2222;
int n,m;
char s
;
void dfs(int x,int y){
if(x<0 || x>=n-1 || y<0 || y>=m-1)return ;
int sum=0,tx,ty;
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
if(s[x+i][y+j]=='*'){
sum++;
tx=x+i;
ty=y+j;
}
}
}
if(sum==1){//四个格子中有一个*就修改,并搜索因此影响到的区域
s[tx][ty]='.';
for(int i=-1; i<=1; i++)
for(int j=-1; j<=1; j++)
dfs(tx+i, ty+j);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
read(n);read(m);
for(int i=0; i<n; i++)scanf("%s", s[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++)puts(s[i]);
return 0;
}
描述:
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
5 5 .*.*. ***** .*.*. ***** .*.*.
output
.*.*. ***** .*.*. ***** .*.*.
input
6 7 ***.*.* ..*.*.* *.*.*.* *.*.*.* ..*...* *******
output
***...* ..*...* ..*...* ..*...* ..*...* *******
input
4 5 ..... ..... ..*** ..*..
output
..... ..... ..... .....
题意:
给出一个n*m(1 ≤ n, m ≤ 2000)的表格,里面有'*'和'.',求把最少的'*'移除掉,使得'.'所在的连通块是矩形。
思路:
2*2地考虑,如果2*2的格子里只有一个'*',说明这个'*'要去掉,其他情况都不用去掉。然后去掉这个'*'后,又会对其他四个格子有影响。 只要想到这个,就很好办了。
复杂度不太好估计。
推荐和CF 723D一起食用,题解见Here
代码:
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using namespace std;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
const int N=2222;
int n,m;
char s
;
void dfs(int x,int y){
if(x<0 || x>=n-1 || y<0 || y>=m-1)return ;
int sum=0,tx,ty;
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
if(s[x+i][y+j]=='*'){
sum++;
tx=x+i;
ty=y+j;
}
}
}
if(sum==1){//四个格子中有一个*就修改,并搜索因此影响到的区域
s[tx][ty]='.';
for(int i=-1; i<=1; i++)
for(int j=-1; j<=1; j++)
dfs(tx+i, ty+j);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
read(n);read(m);
for(int i=0; i<n; i++)scanf("%s", s[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++)puts(s[i]);
return 0;
}
相关文章推荐
- Codeforces--525D--Arthur and Walls(DFS)
- CodeForces 525D D. Arthur and Walls(BFS)
- Codeforces 682C Alyona and the Tree【Dfs+YY思维】好题
- CF 525D(Arthur and Walls-贪心2*2方格补全)
- codefrces 525 D. Arthur and Walls (bfs-小矩形)
- Codeforces #297( div2) D. Arthur and Walls ( DFS
- codeforces 525 D Arthur and Walls
- Codeforces Round #297 (Div. 2) D - Arthur and Walls [思维]【思维】
- Codeforces-199D:Jumping on Walls(DFS+思维)
- codeforces-Fox And Two Dots【DFS】(思维)
- Arthur and Walls - CodeForces 525 D 搜索
- Codeforces 760C Pavel and barbecue【思维+Dfs】
- codeforces D - Arthur and Walls
- CodeForces 135 B. Rectangle and Square(判断正方形和 矩形)
- Codeforces 519D - A and B and Interesting Substrings (思维)
- Arthur and Brackets - CodeForces 508 E dp贪心
- Codeforces Round #297 (Div. 2) D. Arthur and Walls
- Codeforces 366D Dima and Trap Graph (DFS剪枝水过...)
- codeforces 508 E. Arthur and Brackets
- Codeforces 6D Lizards and Basements 2 dfs+暴力