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 ...
随机推荐
- CentOS系统时间修改
1. 实体机器上安装CentOS $date -s '2015-03-03 13:34:00' 2. 虚拟机上安装的CentOS #查看系统时间和硬件时间 date hwclock --show #设 ...
- mybatis学习总结(二)——配置
在mybatis中要构建sqlSessionFactory对象,让它来产生SqlSession,而在mybatis-spring中,SqlSession的产生是通过SqlSessionTemplate ...
- Entity Framework(EF)(一)之database first
1.EF简介ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案.该框架曾经为.NET Framework的 ...
- 九度OJ 1088:剩下的树 (线段树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5791 解决:2649 题目描述: 有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点 ...
- python中的括号以及元组和列表的区别
1 python中的括号 1.1 花括号 花括号表示的是字典,即键值对. 1.2 方括号 方括号表示的是列表,类似于数组,但是可以允许存放混杂类型的数据. 1.3 圆括号 圆括号表示的是元组,类似于列 ...
- 超实用的 Nginx 极简教程,覆盖了常用场景(转)
概述 安装与使用 安装 使用 nginx 配置实战 http 反向代理配置 负载均衡配置 网站有多个 webapp 的配置 https 反向代理配置 静态站点配置 搭建文件服务器 跨域解决方案 参考 ...
- 解析json的方式
一.Javascrip操作json 原始方式: var str1 = '{ "name": "jacun", "addr": "b ...
- pyinstaller-py2exe-cx_Freeze打包第一个wxPython程序HelloWorld
pyinstaller 打包hello 7Mb ================= www.pyinstaller.org pip install pypiwin32 pip install pyin ...
- linux内核container_of宏定义分析
看见一个哥们分析container_of很好,转来留给自己看 一.#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMB ...
- Windows消息类型及说明
WM_ Window Message 窗口消息,一般用在SendMessage,PostMessage这样的消息函数中 SM_ Static Message 静态标签消息 SS_ Static Sty ...