zoukankan      html  css  js  c++  java
  • zzuli OJ 1908: 小火山的围棋梦想 【DFS】

    1908: 小火山的围棋梦想

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 197  Solved: 46

    Description

      小火山最近喜欢上了围棋。
      对于围棋,其实小火山是一窍不通的。现在棋盘上,有很多小火山的棋子。 如果棋盘上有这样的一个位置, 那么这个位置也会变成小火山
    的棋子;这样的位置是指小火山的棋子将该位置围起来。
      现在,小火山想知道实际棋盘是什么样子的。 你快来帮帮他吧!

    Input

    输入第一行是一个整数T(T <= 30), 表示一共有T组数据。
    每组数据,第一行为两个整数n, m(1 <= n, m <= 25),  随后一个n*m的矩阵代表棋盘,其中"."是代表没放棋子的位置, "*"代表小火山的棋子。

    Output

    对于每组数据输出一个n*m的棋盘, 代表实际的棋盘。

    Sample Input

    2
    3 3
    ***
    *.*
    ***
    4 4
    .*..
    *.*.
    *.*.
    .*..

    Sample Output

    Case 1:
    ***
    ***
    ***
    Case 2:
    .*..
    ***.
    ***.
    .*..

    HINT

    用两次DFS,第一次DFS平判断当前位置的“ . ”是否可以被“ * ”替换,如果可以,在DFS将“ . ”替换掉。

    #include <cstdio>
    #include <cstring>
    using namespace std;
    bool vis[40][40], flag;
    int m, n;
    char maze[40][40];
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    void dfs_v(int x, int y) {
        if (!flag) return ;
        for (int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            if (maze[tx][ty] == '*' || vis[tx][ty]) continue;
            if (ty <= 0 || tx <= 0 || ty >= m -1 || tx >= n - 1) {
                flag = false; return ;
            }
            vis[tx][ty] = true;  dfs_v(tx, ty);
            if (!flag) return ;
        }
    }
    void dfs_c(int x, int y) {
        for (int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            if (maze[x][y] == '*') continue;
            maze[x][y] = '*';
            dfs_c(tx, ty);
        }
    }
    int main() {
        int t, cnt = 0;
        scanf("%d", &t);
        while (t--) {
            scanf("%d%d", &n, &m);
            for (int i = 0; i < n; i++) {
                scanf("%s", maze[i]);
            }
            for (int i = 1; i < n - 1; i++) {
                for (int j = 1; j < m - 1; j++) {
                    if (maze[i][j] == '.') {
                        memset(vis, false, sizeof(vis));
                        flag = true; dfs_v(i, j);
                        if (flag) {
                            maze[i][j] = '*'; dfs_c(i, j);
                        }
                    }
                }
            }
            printf("Case %d:
    ", ++cnt);
            for (int i = 0; i < n; i++) {
                printf("%s
    ", maze[i]);
            }
        }
        return 0;
    }
    
    
    


  • 相关阅读:
    [Swift]LeetCode96. 不同的二叉搜索树 | Unique Binary Search Trees
    [Swift]LeetCode95. 不同的二叉搜索树 II | Unique Binary Search Trees II
    [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal
    [Swift]LeetCode93. 复原IP地址 | Restore IP Addresses
    [Swift]LeetCode92. 反转链表 II | Reverse Linked List II
    [Swift]LeetCode91. 解码方法 | Decode Ways
    [Swift]LeetCode90. 子集 II | Subsets II
    谈谈我对P2P网络借贷的一些看法
    辣妈萌宝面试心得体会
    辣妈萌宝面试心得体会
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770844.html
Copyright © 2011-2022 走看看