Codeforces Round #222 (Div. 1) A. Maze dfs
A. Maze
题目连接:
http://codeforces.com/contest/377/problem/A
Description
Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.
Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.
Input
The first line contains three integers n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze.
Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.
Output
Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#").
It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.
Sample Input
3 4 2
..#
..#.
...
Sample Output
.X#
X.#.
...
Hint
题意
在一个nm的矩阵里面,你需要画k个'X'使得,剩下的.都在一个连通块里面
题解:
我们这么想,我们只要按照dfs的顺序去涂X就好了
如果一开始就有多个连通块的话,我们最后剩下的是最大的连通块,其他的一定都可以被填满的
然后再dfs去填就好了
代码
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y,z,k;
};
const int maxn = 505;
int n,m,k;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
char mp[maxn][maxn];
int vis[maxn][maxn];
int cnt=1,sum=0;
vector<node>P;
bool cmp(node a,node b)
{
return a.k>b.k;
}
void dfs(int x,int y)
{
sum++;
vis[x][y]=cnt;
for(int i=0;i<4;i++)
{
int xx = x+dx[i];
int yy = y+dy[i];
if(xx<1||xx>n)continue;
if(yy<1||yy>m)continue;
if(vis[xx][yy])continue;
if(mp[xx][yy]=='#')continue;
dfs(xx,yy);
}
}
void dfs2(int x,int y)
{
vis[x][y]=1;
for(int i=0;i<4;i++)
{
int xx = x+dx[i];
int yy = y+dy[i];
if(xx<1||xx>n)continue;
if(yy<1||yy>m)continue;
if(vis[xx][yy])continue;
if(mp[xx][yy]=='#')continue;
dfs2(xx,yy);
}
if(k>0){
mp[x][y]='X';
k--;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
scanf("%s",mp[i]+1);
node tmp;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='.'&&vis[i][j]==0)
{
sum=0;
dfs(i,j);
tmp.x=i,tmp.y=j,tmp.z=cnt,tmp.k=sum;
cnt++;
P.push_back(tmp);
}
}
}
if(cnt==1)
{
for(int i=1;i<=n;i++,cout<<endl)
for(int j=1;j<=m;j++)
cout<<mp[i][j];
return 0;
}
sort(P.begin(),P.end(),cmp);
int ans1 = P[0].z,ans2 = k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='#')continue;
if(vis[i][j]!=ans1)
{
mp[i][j]='X';
k--;
}
}
}
memset(vis,0,sizeof(vis));
dfs2(P[0].x,P[0].y);
for(int i=1;i<=n;i++,cout<<endl)
{
for(int j=1;j<=m;j++)
{
cout<<mp[i][j];
}
}
}
Codeforces Round #222 (Div. 1) A. Maze dfs的更多相关文章
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
C. Xor-tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...
- Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈
D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- Codeforces Round #398 (Div. 2) C. Garland —— DFS
题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...
- Codeforces Round #222 (Div. 1) (ABCDE)
377A Maze 大意: 给定棋盘, 保证初始所有白格连通, 求将$k$个白格变为黑格, 使得白格仍然连通. $dfs$回溯时删除即可. #include <iostream> #inc ...
- Codeforces Round #321 (Div. 2)C(tree dfs)
题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...
- Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp
题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...
- Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #222 (Div. 1) D. Developing Game 扫描线
D. Developing Game 题目连接: http://www.codeforces.com/contest/377/problem/D Description Pavel is going ...
随机推荐
- shell脚本实现监控shell脚本的执行流程及变量的值
这篇文章主要介绍了shell脚本实现监控shell脚本的执行流程及变量的值本文使用shell完成对执行过程中条件语句中的变量的变化的监控和整个程序的执行流程的观察功能,需要的朋友可以参考下 很多时候, ...
- 浅析Postgres中的并发控制(Concurrency Control)与事务特性(上)
转载:https://www.cnblogs.com/flying-tiger/p/9567213.html#4121483#undefined PostgreSQL为开发者提供了一组丰富的工具来管理 ...
- 64_t8
trytond-account-de-skr03-4.0.0-3.fc26.noarch.rpm 12-Feb-2017 13:06 53278 trytond-account-invoice-4.0 ...
- python网络编程--线程使用threading
一:线程使用 线程使用有两种方法,一种是直接使用,二是通过继承threading.Thread类使用 二:函数式使用 函数式:调用thread模块中的start_new_thread()函数来产生新线 ...
- 浅谈malloc/free和new/delete 的区别
malloc和new的区别 malloc是库函数,需要包头文件才能成功运行编译:new是操作符(C++中的关键字),需要在C++的环境下使用. malloc既可以在C语言中使用也可以在C++中使用,n ...
- sp_executesql动态执行sql语句并将结果赋值给一变量
需求场景: 需动态拼接sql语句进行执行,并将执行的结果赋值给一指定变量. 样例代码如下: SELECT @tableName = TAB_NAME FROM dbo.NMR_BLYWBDY WHER ...
- bms_output.put_line使用方法
https://blog.csdn.net/sxww321/article/details/4020300
- ASP.NET MVC 3升级至MVC 5.1的遭遇:“已添加了具有相同键的项”
最近将一个项目从ASP.NET MVC 3升级至刚刚发布的ASP.NET MVC 5.1,升级后发现一个ajax请求出现了500错误,日志中记录的详细异常信息如下: System.ArgumentEx ...
- USACO 6.5 Closed Fences
Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N c ...
- Python全栈开发之13、CSS
一.css简介 CSS 是 Cascading Style Sheets的缩写,用来设计网页的样式布局,以及大小来适应不同的屏幕等,使网页的样式和网页数据分离, 二.导入css 导入css有4种方式: ...