CF #222 div1 A Maze

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值