グラフのDfs_connectivity検出

グラフのDfs_connectivity検出

/ *正方形のマトリックスが与えられた場合、接続性を定義します。上下、左右に隣接し、値は同じです。
それは、さまざまな領域がさまざまな色で描かれた地図として想像することができます。
入力:
整数N、(N <50)は、マトリックスの
行と列の数を示します。次のN行、各行にN文字、正方形マトリックスの要素を表します。
次の整数M、(M <1000)は、クエリの数と
次のM行を示します。 、各行はクエリを表します。
形式は4つの整数、y1、x1、y2、x2で
、クエリ(行y1、列x1)と(行y2、列x2)が接続されているかどうかを示します。
出力をtrueに接続し、それ以外の場合はfalse

例えば:
10
0010000000
0011100000
0000111110
0001100010
1111010010
0000010010
0000010011
0111111000
0000010000
0000000000
。3
0 9 9 0
0 2 8 6
4 4 4 6。

プログラムは次のように出力する必要があります:
false
true
true * /


import java.util.Scanner;


public class 图的dfs_连通检测 {
    
    
  public static void main(String[] args) {
    
    
    Scanner scanner = new Scanner(System.in);
    int N = scanner.nextInt();
    scanner.nextLine();
    char[][] graph = new char[N][N];
    for (int i = 0; i < N; i++) {
    
    
      graph[i] = scanner.nextLine().toCharArray();
    }
    int M = scanner.nextInt();
    int[][] query = new int[M][4];
    for (int i = 0; i < M; i++) {
    
    
      for (int j = 0; j < 4; j++) {
    
    
        query[i][j] = scanner.nextInt();
      }
    }
    // M个起点和终点
    for (int i = 0; i < M; i++) {
    
    
      //对每个起点和终点,检查是否连通
      boolean ok = check(graph, new int[N][N], query[i]);
      System.out.println(ok);
    }
  }

  /**
   * 检查两个坐标点在这个图中是否连通
   * @param graph 原始图
   * @param label 标记
   * @param points 起点和终点的坐标 x1 y1 x2 y2
   * @return
   */
  private static boolean check(char[][] graph, int[][] label, int[] points) {
    
    
    int x1 = points[0];
    int y1 = points[1];
    int x2 = points[2];
    int y2 = points[3];
    //起点和终点重合了,就可以返回true
    if (x1 == x2 && y1 == y2) {
    
    
      return true;
    }

    int value = graph[x1][y1];
    boolean f1 = false;
    boolean f2 = false;
    boolean f3 = false;
    boolean f4 = false;
    //往左走,1.不能走出去,2.左边的位置没有被访问过,3.左边位置上的值要和现在的值相同
    if (x1 - 1 >= 0 && label[x1 - 1][y1] == 0 && graph[x1 - 1][y1] == value) {
    
    
      label[x1 - 1][y1] = 1; // 坐标的位置标记为已访问
      points[0] = x1 - 1; // 把左边的点作为新起点,递归
      f1 = check(graph, label, points);
      //回溯
      label[x1 - 1][y1] = 0;
      points[0] = x1;
    }
    //往右走
    if (x1 + 1 < graph.length && label[x1 + 1][y1] == 0 && graph[x1 + 1][y1] == value) {
    
    
      label[x1 + 1][y1] = 1;
      points[0] = x1 + 1;
      f2 = check(graph, label, points);
      label[x1 + 1][y1] = 0;
      points[0] = x1;
    }
    //往上走
    if (y1 - 1 >= 0 && label[x1][y1 - 1] == 0 && graph[x1][y1 - 1] == value) {
    
    
      label[x1][y1 - 1] = 1;
      points[1] = y1 - 1;
      f3 = check(graph, label, points);
      label[x1][y1 - 1] = 0;
      points[1] = y1;
    }
    //往下走
    if (y1 + 1 < graph.length && label[x1][y1 + 1] == 0 && graph[x1][y1 + 1] == value) {
    
    
      label[x1][y1 + 1] = 1;
      points[1] = y1 + 1;
      f4 = check(graph, label, points);
      label[x1][y1 + 1] = 0;
      points[1] = y1;
    }
    return f1 || f2 || f3 || f4;
  }
}

おすすめ

転載: blog.csdn.net/weixin_45952706/article/details/109252997