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 ...
随机推荐
- git clone命令使用
git clone命令使用 分类: 项目构建2013-06-26 15:43 38660人阅读 评论(2) 收藏 举报 GitClone git clone 命令参数: usage: git clon ...
- 洛谷P2422 良好的感觉
题目意思就是:最大化一个区间的和与这个区间的最小值的乘积. 换一个角度看问题,如果我们穷举一个最小值 $ a_i $ ,然后往左右扩展,显然是对的,复杂度 $ O(n^2) $.所以我们要优化一下这个 ...
- 如何阻止点击scrollviewer里面的单位内容时,自动滚动
<Style TargetType="{x:Type ListBoxItem}"> <Setter Property="FocusVisualStyle ...
- new Function和eval区别
eval和new Function都可以动态解析和执行字符串.但是它们对解析内容的运行环境判定不同. eval中的代码执行时的作用域为当前作用域.它可以访问到函数中的局部变量. new Functio ...
- 20155309南皓芯2016-2017 2《Java程序设计》第一周学习总结
关于java学习笔记的思考问题 第一章:JDK与JRE,JVM之间有没有必然的联系 第二章:可执行文件夹找到相关链接库 第三章:for与while循环的用法与比较,break与continue跳出的注 ...
- 对于ElasticSearch与Hadoop是如何互相调用的?
1.在HDFS中,数据是以文件形式保存的,比如JSON: https://blog.csdn.net/napoay/article/details/68945483 2.python读写HDFS,一般 ...
- SSIS 学习之旅 数据同步
这一章 别人也有写过但是我觉得还是写写比较好.数据同步其实就是想仿照 数据库的发布订阅功能 第一章:SSIS 学习之旅 第一个SSIS 示例(一)(上) 第二章:SSIS 学习之旅 第一个SSIS 示 ...
- #JS 异步处理机制的几种方式
Javascript语言的执行环境是"单线程"(single thread,就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个任务完成,再执行后面一个任务,以此类推) ...
- CCF CSP 201412-3 集合竞价
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201412-3 集合竞价 问题描述 某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确 ...
- Spark(十七)图计算GraphX
一.图概念术语 1.1 基本概念 图是由顶点集合(vertex)及顶点间的关系集合(边edge)组成的一种数据结构. 这里的图并非指代数中的图.图可以对事物以及事物之间的关系建模,图可以用来表示自然发 ...