zoukankan      html  css  js  c++  java
  • Codeforces 377A

    A. Maze
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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 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.

    Examples
    input
    3 4 2
    #..#
    ..#.
    #...
    output
    #.X#
    X.#.
    #...
    input
    5 4 5
    #...
    #.#.
    .#..
    ...#
    .#.#
    output
    #XXX
    #X#.
    X#..
    ...#
    .#.#
    题目大意:
    找k个空白快'.'填充成'X',使剩下的空白快仍然组成联通块。
    方法一:
    dfs找到边缘块,从边缘块开始填充;如果你理解不了边缘块,可以看方法二。
    代码1:
    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int N=505;
    int n,m,k;
    int dir[4][2]={
        1,0,0,1,-1,0,0,-1
    };
    int vis[N][N]={
        0
    };
    char map[N][N];
    void dfs(int x,int y) 
    {
        for(int i=0;i<4;i++)
        {
            int a=x+dir[i][0];
            int b=y+dir[i][1];
            if(0<=a&&a<n&&0<=b&&b<m&&vis[a][b]==0&&map[a][b]=='.')
            {
                vis[a][b]=1;
                dfs(a,b);
            }
        }
        if(k)
        {
            k--;
            map[x][y]='X';
        }
    }
    int main()
    {
        scanf("%d %d %d",&n,&m,&k);
        getchar();
        for(int i=0;i<n;i++)
        {
            gets(map[i]); 
        } 
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            if(vis[i][j]==0&&map[i][j]=='.')
            {
                vis[i][j]=1;
                dfs(i,j);
            }
        }
        for(int i=0;i<n;i++)
        puts(map[i]);
        return 0;
    } 

     方法二:

    逆向思维,先把所有的空白块'.'变成'X',记录个数为ans,再用dfs把ans-k个'X'变成'.';这样就保证了所有的空白块都是连通块。

    代码2:

    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int N=505;
    int n,m,k;
    int cnt=0;
    int ans=0;
    int dir[4][2]={
        1,0,0,1,-1,0,0,-1
    };
    char map[N][N];
    void dfs(int x,int y) 
    {
        for(int i=0;i<4;i++)
        {
            int a=x+dir[i][0];
            int b=y+dir[i][1];
            if(0<=a&&a<n&&0<=b&&b<m&&map[a][b]=='X')
            {
                if(cnt==ans-k)return ;//控制'.'的个数为ans-k个 
                map[a][b]='.'; 
                cnt++;
                dfs(a,b);
            }
        }
    }
    int main()
    {
        scanf("%d %d %d",&n,&m,&k);
        getchar();
        for(int i=0;i<n;i++)
        {
            gets(map[i]); 
        } 
        int x=0,y=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            if(map[i][j]=='.')
            {
                 map[i][j]='X';
                 ans++;
                 x=i;
                 y=j;
            }
        }
        map[x][y]='.';
        cnt++;
        dfs(x,y);
        for(int i=0;i<n;i++)
        puts(map[i]);
        return 0;
    }
  • 相关阅读:
    收听网络状态广播
    常用工具类
    BroadcastReceiver study
    NIO2
    ip route,ip rule, iptables和docker的端口映射
    Hystrix使用小结
    mysql CPU占用高
    mysql隔离级别与锁,接口并发响应速度的关系(2)
    TOMCAT调优内容
    jvm 锁Lock
  • 原文地址:https://www.cnblogs.com/widsom/p/6710660.html
Copyright © 2011-2022 走看看