集册 Java实例教程 在矩阵中查找模式

在矩阵中查找模式

欢马劈雪     最近更新时间:2020-01-02 10:19:05

506
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在矩阵中查找模式

public class Main{/**来 自 时代Java公众号 - nowjava.com**/

  public static void main(String[] args) {

    int[][] matrix = new int[][] { 

      { 7, 2, 8, 3, 4, 5, 5, 8, 6, 4 }, 

      { 6, 7, 3, 1, 1, 5, 8, 6, 1, 9 },

      { 8, 9, 8, 8, 2, 4, 2, 6, 4, 3 }, 

      { 3, 8, 3, 9, 5, 0, 5, 3, 2, 4 }, 

      { 9, 5, 0, 9, 5, 1, 5, 8, 1, 3 },

      { 3, 8, 4, 3, 8, 4, 5, 3, 8, 4 }, 

      { 6, 4, 7, 3, 5, 3, 0, 2, 9, 3 }, 

      { 7, 0, 5, 3, 1, 0, 6, 6, 0, 1 },

      { 0, 8, 3, 4, 2, 8, 2, 9, 5, 6 }, 

      { 4, 6, 0, 7, 9, 2, 4, 1, 3, 7 }, 

      { 4, 6, 0, 7, 9, 2, 4, 1, 3, 7 } };

    int[][] pattern = new int[][] { 

      { 9, 5, 0 }, // 来 自 NowJava.com - 时代Java

      { 3, 8, 4 }, 

      { 3, 5, 3 } };


    System.out.println(findPattern(matrix, pattern).toString());


  }


  public static class Result {

    int R;

    int C;


    public Result(int R, int C) {

      this.R = R;

      this.C = C;

    }


    @Override

    public String toString() {

      return R + "--" + C;

    }

  }

  public static Result findPattern(int[][] matrix, int[][] pattern) {

    if (matrix.length < pattern.length || matrix[0].length < pattern[0].length) {

      return new Result(-1, -1);

    }


    int count = 0;

    for (int i = 0; i < matrix[0].length - pattern[0].length + 1; i++) {

      for (int j = 0; j < matrix.length - pattern.length + 1; j++) {

        count++;

        int I = i;

        int J = j;

        if (match(matrix, I, J, pattern)) {

          return new Result(I, J);

        }

      }

    }

    return new Result(-1, -1);

  }

  public static boolean match(int[][] matrix, int i, int j, int[][] pattern) {

    int J = j;

    for (int m = 0; m < pattern[0].length; m++) {

      for (int n = 0; n < pattern.length; n++) {

        if (ma
展开阅读全文