【校队排位赛#3 B】 DFS 暴力路径记录

题目如下
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.

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

题目说原来有个连通图,让你加入k个障碍,使得剩下的图依然联通,输出处理后的图形

思路(DFS):

1.题目数据量小,能暴力就暴力。所以会想到DFS。
2.既然原来的图形是联通的,我设原来的empty位置个数为cnt,那我只要随便找个位置,往四周走上(cnt-k)步,然后剩下的路封起来就行了。
3.em。。基本也就这么多吧,就是dfs的同时开个path记录路径,把走过的位置设置成1,然后处理完后找到既为 ’ . ’ 同时path为0的位置,因为它相当于是多余的,就改成X,最后输出即可
刚刚看了一下网上的代码,好像比我的简单,但是我的好像更快一点。
详情见代码,我注释打的蛮清楚的。

#include <iostream>
#include <vector>
#include <cstdio>
#include <map>
#include <climits>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#define maxn  1000+5
using namespace std;
 int n, m , k;
 int cnt=0;
 int cur_cnt = 1;
 int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};  //设置方向数组,分别表示上下左右
 vector<string>  s;
int path[maxn][maxn];  //路径记录数组
 void dfs(int x, int y)
 { 

     if(cur_cnt==cnt-k)  //走到那么多步可以不走了
     {
         path[x][y] = 1;
         return;
     }

        path[x][y] = 1;   //当前走到的地方记录一下
     for(int i=0;i<4;i++)
     {
          if(cur_cnt==cnt-k)  return;
          int tmpx = x, tmpy = y;
           int x = x+dir[i][0];     //改变方向
           int y = y+dir[i][1];
            if(!(x>=n||y>=m||x<0||y<0||path[x][y]||s[x][y]=='#'))  //剪枝
            {
                cur_cnt++;
                dfs(x,y);
            }
            x = tmpx;        //注意路径和cur_cnt不用再回溯回来了,我们是走到的地方就算,之前回溯回来情况就多了导致TL
            y = tmpy;  
     }

 }
int main()
{
    cin>>n>>m>>k;
    memset(path,0,sizeof(path)); //初始化,表示没走过
    char c = getchar();
    for(int i=1;i<=n;i++)     //预处理
    {
        string tmp;
        getline(cin,tmp);
        s.push_back(tmp);
    }
     int obx, oby;
    for(int i=0;i<n;i++)
    for(int j=0;j<m;j++)
    if(s[i][j] == '.')        //找一个empty进入dfs即可,因为是联通的哪个开始都无所谓
    {
        obx = i;
        oby = j;
        cnt++;
    }
    dfs(obx,oby);

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        { 
            if(s[i][j]=='.'&&!path[i][j])       //找到路径之外的empty位置,置为X
            s[i][j] = 'X';
        }
    }
    for(int i=0;i<n;i++)        //输出即可
    {
        for(int j=0;j<m;j++)
        {
            printf("%c",s[i][j]);
        }
        printf("\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值