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. O_NONBLOCK与O_NDELAY有何不同?

    O_NONBLOCK和O_NDELAY所产生的结果都是使I/O变成非搁置模式(non-blocking),在读取不到数据或是写入缓冲区已满会马上return,而不会搁置程序动作,直到有数据或写入完成. ...

  2. 转载:Google 官方应用架构的最佳实践指南 赞👍

    官方给的实践指南,很有实际的指导意义,  特别是对一些小公司,小团队,给了很好的参考意义. 原文地址: https://developer.android.com/topic/libraries/ar ...

  3. 字符串匹配算法之 kmp算法 (python版)

    字符串匹配算法之 kmp算法 (python版) 1.什么是KMP算法 KMP是三位大牛:D.E.Knuth.J.H.MorriT和V.R.Pratt同时发现的.其中第一位就是<计算机程序设计艺 ...

  4. 【前端vue开发架构】vue开发单页项目架构总结

    为营销活动设计的前端架构 主要的技术栈为 Vuejs,Webpack,请自行阅读如下技术或者框架的文档: 一.基础说明: node (https://nodejs.org/en/) npm (http ...

  5. centos7的防火墙(firewalld)

    Centos7中默认将原来的防火墙iptables升级为了firewalld,firewalld跟iptables比起来至少有两大好处: 1.firewalld可以动态修改单条规则,而不需要像ipta ...

  6. 转:localStorage 还能这么用

    地址:https://iammapping.com/the-other-ways-to-use-localstorage/ localStorage 还能这么用 HTML5中 Web Storage ...

  7. git —— 远程仓库(操作)

    运行目录:本地仓库目录 1.本地关联远程仓库 $ git remote add origin 你的远程库地址(SSH和HTTP都可以) 2.远程仓库为空,可选择合并远程仓库和本地仓库,远程库不为空时, ...

  8. java 多线程总结篇3之——生命周期和线程同步

    一.生命周期 线程的生命周期全在一张图中,理解此图是基本: 线程状态图 一.新建和就绪状态 当程序使用new关键字创建了一个线程之后,该线程就处于新建状态,此时它和其他的Java对象一样,仅仅由Jav ...

  9. R语言学习笔记:choose、factorial、combn排列组合函数

    一.总结 组合数:choose(n,k) —— 从n个中选出k个 阶乘:factorial(k) —— k! 排列数:choose(n,k) * factorial(k) 幂:^ 余数:%% 整数商: ...

  10. Linux下的IPC机制

    Linux下的IPC机制 IPC(Inter-Process Communication)是多个进程之间相互沟通的一种方法.在linux下有多种进程间通信的方法. 共享内存 Linux内存共享有多种, ...