DFS+并查集思想求被围绕的区域

class Solution {
    private int[][] dir= {{0,-1},{-1,0},{0,1},{1,0}};
    private boolean[][] used;
    public boolean isMove(char[][] board,int x,int y)
    {
        if(x>=0&&x<board.length&&y>=0&&y<board[0].length)
        return true;
        return false;
    }

    public boolean dfs(char[][] board,int x,int y)
    {
        board[x][y]=‘*‘;   //一旦发现为立即赋值
        used[x][y]=true;   //并设为已访问
        for(int i=0;i<4;++i)
        {
            int newX=x+dir[i][0];
            int newY=y+dir[i][1];
            if(isMove(board,newX,newY)&&board[newX][newY]==‘o‘&&!used[newX][newY]&&dfs(board,newX,newY))
            {
                return true;
            }
        }
        return false;
    }
    //并查集思想==找到一个点并把与它相关的点都标记起来再按这个标记做相应的处理
    public void solve(char[][] board) {
        used=new boolean[board.length][board[0].length];
        for(int i=0;i<board[0].length;++i)
        {
            if(board[0][i]==‘o‘)
                dfs(board,0,i);
            if(board[board.length-1][i]==‘o‘)
                dfs(board,board.length-1,i);
        }
        for(int i=0;i<board.length;++i)
        {
            if(board[i][0]==‘o‘)
                dfs(board,i,0);
            if(board[i][board[0].length-1]==‘o‘)
                dfs(board,i,board[0].length-1);
        }

        for(int i=0;i<board.length;++i)
        {
            for(int j=0;j<board[0].length;++j)
            {
                if(board[i][j]==‘*‘)
                    board[i][j]=‘o‘;
                else if(board[i][j]==‘o‘)
                    board[i][j]=‘x‘;
            }
        }

        for(int i=0;i<board.length;++i)
        {
            for(int j=0;j<board[0].length;++j) {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
    }
}

public class Main {
    public static void main(String[] args) {
      Solution space=new Solution();
      char[][]board= {
              {‘x‘,‘x‘,‘o‘,‘x‘,‘x‘},
              {‘x‘,‘x‘,‘x‘,‘x‘,‘x‘},
              {‘x‘,‘x‘,‘x‘,‘x‘,‘x‘},
              {‘x‘,‘o‘,‘o‘,‘x‘,‘x‘},
              {‘x‘,‘x‘,‘o‘,‘x‘,‘x‘},
              {‘x‘,‘o‘,‘x‘,‘x‘,‘x‘}
    };
   space.solve(board);
 }
}

原文地址:https://www.cnblogs.com/z2529827226/p/11731487.html

时间: 2024-07-29 23:47:56

DFS+并查集思想求被围绕的区域的相关文章

DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta&#39;s Colorful Graph

题目传送门 1 /* 2 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 3 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 4 注意:无向图 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <string> 11 #include <vector> 1

CF 115 A 【求树最大深度/DFS/并查集】

CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who

分珠(dfs+并查集)

1140 分珠 时间限制:500MS  内存限制:65536K提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不同,珠子之间有一些细线将它们连在一起.现要求切断一些细线,将它们分成两部分, 分割后,单独每一部分的珠子仍保持相连,且要求尽量做到两部分总重相等或相差最少. 请编一程序,给定珠子个数.每颗珠子的重量以及珠子之间的连接情况,输出按上述要求分割后两部分总重的差值的绝对值. 输入格式 第一行有两个数N与M

Network POJ - 3694(lca并查集+连通图求桥)

就是求出原先图中的桥的数量,在每一次询问时加入一条新边,求加入当前边后图中剩余的桥的数量 求出原先图中的桥的数量,然后减去新加入边的两端点之间的桥的数量,就是剩余桥的数量.. 用并查集把属于同一集合的放到一起(即两个点之间没有桥的) #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algo

hdu5438 dfs+并查集 长春网赛

先dfs对度小于2的删边,知道不能删为止. 然后通过并查集来计算每一个分量里面几个元素. #include<iostream> #include<cstring> #include<vector> #define maxn 10010 #define LL __int64 using namespace std; int in[maxn],vis[maxn],p,pa[maxn],cou[maxn]; LL sum[maxn]; vector<int>mp[

ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure t

hdu 1198 Farm Irrigation(深搜dfs || 并查集)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 ----------------------------------------------------------------------------------------------------------------------

51nod1307(暴力树剖/二分&amp;dfs/并查集)

题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数组 num[i] 维护编号为 i 的边当前最大能承受的重量. 在加边的过程中根据给出的父亲节点将当前边所在的链上所有边的num都减去当前加的边的重量, 注意当前边也要减自重. 那么当num首次出现负数时加的边号即位答案: 事实上这个算法的时间复杂度是O(n^2)的, 不过本题并没有出那种退化成单链的

1013 Battle Over Cities (25分) DFS | 并查集

1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any othe