时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 在矩阵中查找模式
Java 在矩阵中查找模式
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
383
查看 / 运行 实例源码
在矩阵中查找模式
实例源码:
源代码:
执行
执行中...
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++) { /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
NO 9 --- 7
NO 9 --- 2
NO 9 --- 8
NO 9 --- 3
NO 9 --- 4
NO 9 --- 5
NO 9 --- 5
NO 9 --- 8
NO 9 --- 6
NO 9 --- 6
NO 9 --- 7
NO 9 --- 3
NO 9 --- 1
NO 9 --- 1
NO 9 --- 5
NO 9 --- 8
NO 9 --- 6
NO 9 --- 1
NO 9 --- 8
1 --- 2
NO 5 --- 8
NO 9 --- 8
NO 9 --- 8
NO 9 --- 2
NO 9 --- 4
NO 9 --- 2
NO 9 --- 6
NO 9 --- 4
NO 9 --- 3
NO 9 --- 8
NO 9 --- 3
3 --- 3
4 --- 3
5 --- 3
NO 3 --- 9
NO 9 --- 5
NO 9 --- 0
NO 9 --- 5
NO 9 --- 3
NO 9 --- 2
0 --- 4
1 --- 4
2 --- 4
0 --- 5
1 --- 5
2 --- 5
NO 3 --- 6
NO 9 --- 5
NO 9 --- 0
3 --- 4
4 --- 4
NO 0 --- 1
NO 9 --- 5
NO 9 --- 1
NO 9 --- 5
NO 9 --- 8
NO 9 --- 1
NO 9 --- 3
NO 9 --- 8
NO 9 --- 4
NO 9 --- 3
NO 9 --- 8
NO 9 --- 4
NO 9 --- 5
NO 9 --- 3
NO 9 --- 8
NO 9 --- 6
NO 9 --- 4
NO 9 --- 7
NO 9 --- 3
NO 9 --- 5
NO 9 --- 3
NO 9 --- 0
NO 9 --- 2
8 --- 6
NO 5 --- 3
NO 9 --- 7
NO 9 --- 0
NO 9 --- 5
NO 9 --- 3
NO 9 --- 1
NO 9 --- 0
NO 9 --- 6
NO 9 --- 6
NO 9 --- 0
-1---1
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部