F - Maze自用

2 篇文章 0 订阅
1 篇文章 0 订阅
                                        F - Maze
Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

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 exactlyk 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 letters 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

Input
3 4 2
#..#
..#.
#...
Output
#.X#
X.#.
#...
Input
5 4 5
#...
#.#.
.#..
...#
.#.#
Output
#XXX
#X#.
X#..
...#
.#.#


 此题我的解法是用queue容器,先计算出需要保留的'.'数量,再随机抽取一.点作为出发点,然后遍历出需要的数量,剩下的输出时全部输出为X。
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n,m,k,u,v,shuliang;//数量记录需要连接的数量(总的.数量-k)
int u,v;//出发点位置
bool visit[512][512];//记录是否读取过
char _map[512][512];//记录输入的迷宫
struct Point{
	int x;
	int y;
};//记录坐标

int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

bool OK(struct Point a)//判断坐标是否可取
{
	return a.x>=0 && a.x<m && a.y>=0 && a.y<n && !visit[a.x][a.y];
}
void BFS()
{
	int w=1;
	queue<Point> QE;
	Point top,t;
	shuliang=shuliang-k;//统计需要保留的.数量
	top.x=u;
	top.y=v;
	QE.push(top);
	if(shuliang!=0)//下面算法的漏洞,没法判断全部为x时的情况
	visit[u][v]=1;
	while(QE.empty()==false && shuliang!=0)
	{
		top=QE.front();
		QE.pop();
		for(int i=0;i<4;i++)
		{
			t.x=top.x+dx[i];
			t.y=top.y+dy[i];
			if(OK(t) && _map[t.x][t.y]=='.' && w<shuliang)
			{
				QE.push(t);
				visit[t.x][t.y]=1;
				w++;
			}//此处为核心一,把需要保留的位置遍历一遍,把visit变为1,后面修改时visit为0的就可直接输出x
			if(w==shuliang)
				return;
		}
	}
}
int main()
{	
	int i,w=0;
	char s[512];
	scanf("%d%d%d",&n,&m,&k);
	memset(visit,0,sizeof(visit));
	memset(_map,0,sizeof(_map));
	shuliang=0;
	for(i=0;i<n;i++)
	{
		scanf("%s",s);
		for(int j=0;j<m;j++)
		{
			_map[j][i]=s[j];
			if(s[j]=='.')
				shuliang++;
			if(w==0 && s[j]=='.')
			{
				u=j;
				v=i;
				w=1;//找到随机点,并防止重复寻找
			}
		}
	}
	
	BFS();
	for(i=0;i<n;i++)//输出
	{
		for(int j=0;j<m;j++)
		{
			if(visit[j][i]==0 && _map[j][i]=='.')//没遍历过得 . 就输出x
				printf("X");
			else
				printf("%c",_map[j][i]);
		}
		printf("\n");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值