zoukankan      html  css  js  c++  java
  • CodeForces

    Maze

    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 nmk (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.

    Example

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



    这是一道好题!题意是通过添加k个障碍使得原图继续保持连通状态。当然不能搜索加点,这会破坏之前的连通性。因此我们可以搜索出一个连通块,使得大小=kk-k(kk为原图可行域.点数) 没被标记的点除#墙外,未搜索的可行域.就是加点X位置。


    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    
    int n,m,k,kk;
    char a[505][505];
    int b[505][505];
    int t[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    struct Node{
        int x,y;
    }node;
    
    void bfs(int i,int j)
    {
        int c,tx,ty,ij;
        queue<Node> q;
        memset(b,0,sizeof(b));
        b[i][j]=1;
        node.x=i;
        node.y=j;
        q.push(node);
        c=1;
        if(c==kk-k) return;    //卡在第8个点,不加TLE。。
        while(q.size()){
            for(ij=0;ij<4;ij++){
                tx=q.front().x+t[ij][0];
                ty=q.front().y+t[ij][1];
                if(tx<0||ty<0||tx>=n||ty>=m) continue;
                if(a[tx][ty]=='.'&&b[tx][ty]==0){
                    c++;
                    b[tx][ty]=1;
                    node.x=tx;
                    node.y=ty;
                    q.push(node);
                }
                if(c==kk-k) return;
            }
            q.pop();
        }
    }
    int main()
    {
        int bx,by,i,j;
        scanf("%d%d%d",&n,&m,&k);
        kk=0;
        for(i=0;i<n;i++){
            getchar();
            scanf("%s",a[i]);
            for(j=0;j<m;j++){
                if(a[i][j]=='.'){
                    kk++;
                    bx=i;
                    by=j;
                }
            }
        }
        bfs(bx,by);
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(b[i][j]==0&&a[i][j]!='#') printf("X");
                else printf("%c",a[i][j]);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    三、 复杂对象类型的WebService
    Axis2.x WebService开发指南目录索引
    eclipse/MyEclipse 日期格式、注释日期格式、时区问题
    IE6、IE7、IE8的CSS、JS兼容
    一、CXF WebService准备工作
    十二、用Axis操作 Header头部信息
    六、 跨多个WebService管理Session
    jQuery autocomplete 自扩展插件、自动补全示例
    二、Axis2的简单WebService示例
    六、传递、返回复杂类型的对象
  • 原文地址:https://www.cnblogs.com/yzm10/p/7230982.html
Copyright © 2011-2022 走看看