CodeForces - 377A Maze BFS逆思维
Maze
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.
Example
3 4 2
#..#
..#.
#...
#.X#
X.#.
#...
5 4 5
#...
#.#.
.#..
...#
.#.#
#XXX
#X#.
X#..
...#
.#.# 这是一道好题!题意是通过添加k个障碍使得原图继续保持连通状态。当然不能搜索加点,这会破坏之前的连通性。因此我们可以搜索出一个连通块,使得大小=kk-k(kk为原图可行域.点数) 没被标记的点除#墙外,未搜索的可行域.就是加点X位置。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; int n,m,k,kk;
char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}};
struct Node{
int x,y;
}node; void bfs(int i,int j)
{
int c,tx,ty,ij;
queue<Node> q;
memset(b,,sizeof(b));
b[i][j]=;
node.x=i;
node.y=j;
q.push(node);
c=;
if(c==kk-k) return; //卡在第8个点,不加TLE。。
while(q.size()){
for(ij=;ij<;ij++){
tx=q.front().x+t[ij][];
ty=q.front().y+t[ij][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='.'&&b[tx][ty]==){
c++;
b[tx][ty]=;
node.x=tx;
node.y=ty;
q.push(node);
}
if(c==kk-k) return;
}
q.pop();
}
}
int main()
{
int bx,by,i,j;
scanf("%d%d%d",&n,&m,&k);
kk=;
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='.'){
kk++;
bx=i;
by=j;
}
}
}
bfs(bx,by);
for(i=;i<n;i++){
for(j=;j<m;j++){
if(b[i][j]==&&a[i][j]!='#') printf("X");
else printf("%c",a[i][j]);
}
printf("\n");
}
return ;
}
CodeForces - 377A Maze BFS逆思维的更多相关文章
- Codeforces 377A - Maze
A. Maze 题目链接:http://codeforces.com/contest/377/problem/A time limit per test 2 seconds memory limit ...
- [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)
题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...
- CodeForces - 987E Petr and Permutations (思维+逆序对)
题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...
- Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- CodeForces 1292A NEKO's Maze Game(思维)
#include <stdio.h> #include <string.h> #include <iostream> #include <string> ...
- Amr and Chemistry CodeForces 558C(BFS)
http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...
- Codeforces Gym 100463A Crossings 逆序数
Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
随机推荐
- 流迭代器 + 算法灵活控制IO流
前言 标准算法配合迭代器使用太美妙了,使我们对容器(数据)的处理更加得心应手.那么,能不能对IO流也使用标准算法呢?有人认为不能,他们说因为IO流不是容器,没有迭代器,故无法使用标准算法.他们错了,错 ...
- [Phoenix] 四、加盐表
摘要: 在密码学中,加盐是指在散列之前将散列内容(例如:密码)的任意固定位置插入特定的字符串.这个在散列中加入字符串的方式称为“加盐”.其作用是让加盐后的散列结果和没有加盐的结果不相同,在不同的应用情 ...
- Netty 100万级高并发服务器配置
前言 每一种该语言在某些极限情况下的表现一般都不太一样,那么我常用的Java语言,在达到100万个并发连接情况下,会怎么样呢,有些好奇,更有些期盼. 这次使用经常使用的顺手的netty NIO框架(n ...
- Redis persistence demystified
https://redis.io/topics/persistence http://oldblog.antirez.com/post/redis-persistence-demystified.ht ...
- Error:Execution failed for task ':app:clean'. > Unable to delete directory: ***/app/build/generated/***
第一次从svn拉下来的工程,在clean的时候会出现 Error:Execution failed for task ':app:clean'. > Unable to delete direc ...
- Android笔记之DrawerLayout的基本使用
效果图 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- (转)Android--UI之ImageView
前言 这篇博客聊一聊在Android下ImageView的使用,在此篇博客中,会讲解到ImageView的一些属性的使用,以及ImageView展示图片的放大.缩小.旋转等操作.最后再讲解一下Andr ...
- lAMP下新建维护站点全过程
由于window2003年7.15日微软对此不进行更新和支持,因此换了服务器系统由原来的windows2003直接升级到linux,关于LAMP的环境配置请查看我其他的相关博客,在这仅讲述一下LAMP ...
- Android Weekly Notes Issue #242
Android Weekly Issue #242 January 29th, 2017 Android Weekly Issue #242 本期内容包括: Android中常用的设计模式; 基于No ...
- sdut oj 1163 C语言实验——排列 (当初不会递归生成排列,这个题目现在才补上 刘汝佳给出了写法 *【模板】 当然有生成全排列的函数存在 )
C语言实验——排列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 有4个互不相同的数字,请按序输出由其中三个不重复数字组成的排列 ...