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的更多相关文章

  1. Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)

    题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #398 (Div. 2) C. Garland —— DFS

    题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...

  5. Codeforces Round #222 (Div. 1) (ABCDE)

    377A Maze 大意: 给定棋盘, 保证初始所有白格连通, 求将$k$个白格变为黑格, 使得白格仍然连通. $dfs$回溯时删除即可. #include <iostream> #inc ...

  6. Codeforces Round #321 (Div. 2)C(tree dfs)

    题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...

  7. Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp

    题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...

  8. 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 ...

  9. Codeforces Round #222 (Div. 1) D. Developing Game 扫描线

    D. Developing Game 题目连接: http://www.codeforces.com/contest/377/problem/D Description Pavel is going ...

随机推荐

  1. C#事件实现文件下载时进度提醒

    C#中的事件是建立在委托的基础上,标准的事件模型应该包括以下几点: 声明一个用于定义事件的委托,这里用系统自带的泛型委托原型EventHandler<TEventArgs>,如:publi ...

  2. 如何扎实自己的Java基础?

    问:如何扎实自己的Java基础? 答:玩好JDK JDK其实就是Java SE Development Kit的缩写,要玩好这东西可不简单.JDK主要包含了三部分,第一部分就是Java运行时环境,这其 ...

  3. Awk基础

    Awk文本处理 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.awk数据可以来自标准输入.一个或多个文件,或其它命令的输出.awk通常是配合脚本进行使用, 是一个强大的文本处 ...

  4. pip离线安装

    pip freeze > requirements.txt pip download <packages> pip install --no-index --find-links=& ...

  5. 【前端开发】localStorage的用法

    localStorage.setItem("name","value")  //存储name的值 var type = localStorage.getItem ...

  6. kafka集群及监控部署

    1. kafka的定义 kafka是一个分布式消息系统,由linkedin使用scala编写,用作LinkedIn的活动流(Activity Stream)和运营数据处理管道(Pipeline)的基础 ...

  7. 【鬼脸原创】谷歌扩展--知乎V2.0

    目的: 用键盘替代鼠标,做一个安静刷知乎的美男(女)子! 功能:   功能 按键 说明 直接定位到搜索框 q   打开 首页 w   打开 话题 e   打开 发现 r   打开 消息 m   打开 ...

  8. Codeforces 225C Barcode(矩阵上DP)

    题目链接:http://codeforces.com/contest/225/problem/C 题目大意: 给出一个矩阵,只有两种字符'.'和'#',问最少修改多少个点才能让每一列的字符一致,且字符 ...

  9. 2016-2017-2 20155309 南皓芯《java程序设计》第八周学习总结

    教材学习内容总结 本周学习的主要是第十四章,第十五章的内容. NIO与NIO2 同步非阻塞IO(Java NIO) : 同步非阻塞,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多 ...

  10. Metronic 5.0.5 bootstrap后台管理模板

    演示地址:http://keenthemes.com/preview/metronic/    下载 Dashboard Table