A. Maze
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
题意:
给你一个n*m的迷宫,‘#’代表墙,‘.’代表路
初始给你的路便是连通的
要求将k个‘.’改为‘X’,使剩下的路仍为连通的
思路:
将连通路进行广搜,从最后一个节点开始置为‘X’,共k个
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char map[550][550];
int visit[550][550];
int n,m,k;
struct node
{
int x;
int y;
int f;
}que[250050];
int main()
{
int next[4][2]=
{
{1,0},
{0,1},
{-1,0},
{0,-1}
};
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++)
scanf("%s",&map[i]);
int sx,sy;
int flag=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='.')
{
sx=i;
sy=j;
flag=1;
break;
}
}
if(flag==1)
break;
}
int head=1;
int tail=1;
que[tail].x=sx;
que[tail].y=sy;
que[tail].f=0;
visit[sx][sy]=1;
tail++;
int tx;
int ty;
while(head<tail)
{
for(int i=0;i<4;i++)
{
tx=que[head].x+next[i][0];
ty=que[head].y+next[i][1];
if(tx<0||tx>=n||ty<0||ty>=m)
continue;
if(map[tx][ty]=='.'&&visit[tx][ty]==0)
{
visit[tx][ty]=1;
que[tail].x=tx;
que[tail].y=ty;
que[tail].f=head;
tail++;
//printf("%d %d\n",tx,ty);
}
}
head++;
}
//printf("%d\n",head);
tail--;
while(k!=0)
{
map[que[tail].x][que[tail].y]='X';
tail--;
k--;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
printf("%c",map[i][j]);
}
printf("\n");
}
return 0;
}
PS:因为忘了将开始节点标记,wa两次 mdzz