码迷,mamicode.com
首页 > 其他好文 > 详细

BFS

时间:2020-03-30 23:34:20      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:ref   lse   解题思路   区域   region   表示   import   string   main   

由斜杠划分区域

在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /\ 或空格构成。这些字符会将方块划分为一些共边的区域。

(请注意,反斜杠字符是转义的,因此 \"\\" 表示。)。

返回区域的数目。

输入:
[
  " /",
  "/ "
]
输出:2
![img](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/15/1.png)
![img](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/15/1.png)

解题思路

我们可以将斜杠转换成3*3的矩阵,因为必须3**3之后才不会有信息的丢失,其实主要是来写一下bfs的模板,毕竟好久不写,手有点生。

代码

import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static int res;
    boolean[][] vis;
    class Node{
        int x;
        int y;
        public Node(){
        }
        public Node(int x,int y){
            this.x = x;
            this.y = y;
        }

    }
    int[] dx = {1,0,0,-1};
    int[] dy = {0,1,-1,0};
    public void bfs(int x,int y,boolean[][] graph){
        Node node = new Node();
        node.x = x;
        node.y = y;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(node);
        while (!queue.isEmpty()){
            Node node1 = queue.poll();
            for(int i =0;i<4;i++){
                int xx = node1.x +dx[i];
                int yy = node1.y +dy[i];
                if(xx >=0 && yy >=0 && xx < graph.length && yy <graph[0].length && !graph[xx][yy] &&!vis[xx][yy] ){		//约束条件
                    queue.offer(new Node(xx,yy));
                    vis[xx][yy] = true;

                }
            }
        }
    }
    public int regionsBySlashes(String[] grid){
        int n = grid.length;
        vis = new boolean[n*3][n*3];
        boolean [][] graph = new boolean[n * 3][n * 3];
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                if(grid[i].charAt(j) == ‘/‘) {
                    graph[i * 3][j * 3 + 2] = true;
                    graph[i * 3 + 1][j * 3 + 1] = true;
                    graph[i * 3 + 2][j * 3] = true;
                } else if(grid[i].charAt(j) == ‘\\‘) {
                    graph[i * 3][j * 3] = true;
                    graph[i * 3 + 1][j * 3 + 1] = true;
                    graph[i * 3 + 2][j * 3 + 2] = true;
                }
            }
        }
        res = 0;
        for(int i  = 0;i<graph.length;i++){
            for(int j = 0;j<graph[0].length;j++){
                if(!graph[i][j] &&!vis[i][j] ){
                    res++;
                    vis[i][j] = true;
                    bfs(i,j,graph);
                }
            }
        }
        return res;
    }
    public static void main(String[] args) {
        F f = new F();
        String[] grid ={" /",
                "/ "};
        System.out.println(f.regionsBySlashes(grid));
    }
}

BFS

标签:ref   lse   解题思路   区域   region   表示   import   string   main   

原文地址:https://www.cnblogs.com/jwthong/p/12601826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!