A.Maze
A.Maze
time limit per test: 2 secondsmemory limit per test: 256 megabytes
每次测试的时间限制:每次测试2秒的限制:256兆字节
input: standard input
输入:标准输入
output: standard output
产出:标准产出
Pavel loves grid mazes. A grid maze is an n x m rectangle maze where each cell is either empty, or is a wall ou can go from one ell oanother only if both cells are empty and have a common side.
帕维尔喜欢网格迷宫。网格迷宫是一个n×m矩形迷宫,其中每个单元要么是空的,要么是一个墙,只有当两个单元都是空的并且有一个共同的边时,才能从一个单元中走到另一个。
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
Pavel画了一个网格迷宫,所有的空细胞都形成一个连通的区域,也就是说,你可以从任何一个空细胞转到任何一个。
doesn’t like it when his maze has to ittle walls. He wants to turm exactly k emptycells into wals so that all the remaining cels stil formed aconnected area. Help him.
不喜欢他的迷宫有小墙的时候。他想要完全把k个空细胞转化成WALS,这样所有剩下的细胞仍然形成连着的区域。帮帮他。
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, coespondinglyk is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze.
第一行包含三个整数n,m,k(1<n,m<500,0<k<s),其中n和m是迷宫的高度和宽度,coespondinglyk是墙壁Pavel想要添加的数目,字母s表示原始迷宫中的空单元格数。
Each of the next n lines contains m characters. They describe the original maze. f a character on a line equals “.”, then the corespondingcell is empty and if the character equals “#”, then the cell is a wall.
下面n行中的每一行都包含m个字符。他们描述了最初的迷宫。如果一行上的字符等于“.”,则协同响应单元格为空,如果字符等于“#”,则该单元格为墙。
Output
输出量
Print n lines containing m characters each: the new maze that fis Pavels requirements. Mark the empty cells that you ransiormed intowalls as “x”, the other cells must be left without changes (that is, " . " and “#”)
打印n行,每一行包含m个字符:新迷宫,这是菲斯帕尔斯要求的。将您在毛巾中放置的空单元标记为“x”,其他单元格必须保持不变(即“,”)。和“#”)
lt is guaranteed that a solution exists. If there are multiple solutions you can output any of them.
我们保证有一个解决办法。如果有多个解决方案,您可以输出其中的任何一个。
dfs找边界
对于任意有点最远点都不会改变连通性
int n, m, k;
char map[503][507];
bool vis[503][507];
void dfs(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] != '.' || vis[x][y])
return;
vis[x][y] = true;
dfs(x + 1, y);
dfs(x - 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
if (k)
{
map[x][y] = 'X';
k--;
}
}
int main()
{
cin >> n >> m >> k;
for (int i = 0; i < n; ++i)
cin>>map[i];
for (int i = 0; i < n && k; ++i)
for (int j = 0; j < m && k; ++j)
dfs(i, j);
for (int i = 0; i < n; ++i)
cout<<map[i]<<endl;
return 0;
}