原文地址:http://www.java2000.net/p9798
从A点开始进行4方向遍历
目的是找到和A点相连并且拥有相同棋面的棋子个数
- package net.java2000.ag;
- import java.awt.Point;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 查找同色围棋子的广度遍历方法。
- *
- * @author 赵学庆,Java世纪网(java2000.net)
- *
- */
- public class ChessWideTraversal {
- static char[][] chess = { { '1', '1', '1', '1', '1', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '1', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '0', '0', '0', '1', '1' },
- { '1', '1', '1', '1', '0', '1', '0', '1', '1' },
- { '1', '0', '0', '1', 'A', '1', '1', '1', '1' },
- { '1', '1', '0', '0', '0', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '0', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '0', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '1', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '1', '1', '1', '1', '1' } };
- // 如果已经遍历了,则不再重复
- static boolean[][] haveFound = new boolean[chess.length][chess[0].length];
- /**
- * 被查找的广度遍历。
- *
- * @param x
- * 种子横坐标
- * @param y
- * 种子纵坐标
- * @param sign
- * 颜色
- * @return 同色数组
- */
- public static List<Point> find(int x, int y, char sign) {
- List<Point> list = new ArrayList<Point>();
- Point p = new Point(x, y);
- list.add(p); // 起始的种子
- for (int i = 0; i < list.size(); i++) {
- p = list.get(i);// 当前位置
- // 左侧,没有到边界
- if (p.x > 0) {
- // 数值等于0 且没有找过
- if (chess[p.y][p.x - 1] == sign && !haveFound[p.y][p.x - 1]) {
- list.add(new Point(p.x - 1, p.y));
- haveFound[p.y][p.x - 1] = true;
- }
- }
- // 上侧,没有到边界
- if (p.y > 0) {
- // 数值等于0 且没有找过
- if (chess[p.y - 1][p.x] == sign && !haveFound[p.y - 1][p.x]) {
- list.add(new Point(p.x, p.y - 1));
- haveFound[p.y - 1][p.x] = true;
- }
- }
- // 右侧,没有到边界
- if (p.x < chess[0].length - 1) {
- // 数值等于0 且没有找过
- if (chess[p.y][p.x + 1] == sign && !haveFound[p.y][p.x + 1]) {
- list.add(new Point(p.x + 1, p.y));
- haveFound[p.y][p.x + 1] = true;
- }
- }
- // 下侧,没有到边界
- if (p.y < chess[0].length - 1) {
- // 数值等于0 且没有找过
- if (chess[p.y + 1][p.x] == sign && !haveFound[p.y + 1][p.x]) {
- list.add(new Point(p.x, p.y + 1));
- haveFound[p.y + 1][p.x] = true;
- }
- }
- }
- return list;
- }
- /**
- * 以图形形式显示遍历结果
- */
- private static void printGraph() {
- for (int i = 0; i < chess.length; i++) {
- for (int j = 0; j < chess[0].length; j++) {
- System.out.print(haveFound[i][j] ? '*' : ' ');
- }
- System.out.println();
- }
- }
- public static void main(String[] args) {
- List<Point> list = find(4, 4, '0');
- printGraph();
- for (Point p : list) {
- System.out.println(p.x + "," + p.y);
- }
- }
- }
运行结果
***
* *
**
***
*
*
4,4
4,3
4,5
4,2
3,5
4,6
5,2
2,5
4,7
6,2
2,4
6,3
1,4
- package net.java2000.ag;
- /**
- * 查找同色围棋子的深度遍历方法
- *
- * @author xifo,Java世纪网(java2000.net)
- *
- */
- public class ChessDeepTraversal {
- private char[][] layout = { { '1', '1', '1', '1', '1', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '1', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '0', '0', '0', '1', '1' },
- { '1', '1', '1', '1', '0', '1', '0', '1', '1' },
- { '1', '0', '0', '1', 'A', '1', '1', '1', '1' },
- { '1', '1', '0', '0', '0', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '0', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '0', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '1', '1', '1', '1', '1' },
- { '1', '1', '1', '1', '1', '1', '1', '1', '1' } };
- // 9行 10列
- private int w = 9, h = 10;
- private boolean[][] flag = new boolean[h][w];
- /**
- * 以 x, y 为起点进行深度优先遍历
- */
- private void traverse(int x, int y) {
- flag[x][y] = true;
- // 尝试遍历左侧结点
- if(x > 0 && !flag[x-1][y] && layout[x-1][y] == '0'){
- traverse(x - 1, y);
- }
- // 尝试遍历上侧结点
- if(y > 0 && !flag[x][y-1] && layout[x][y-1] == '0'){
- traverse(x, y - 1);
- }
- // 尝试遍历右侧结点
- if(x < w - 1 && !flag[x + 1][y] && layout[x + 1][y] == '0'){
- traverse(x + 1, y);
- }
- // 尝试遍历下侧结点
- if(y < h - 1 && !flag[x][y + 1] && layout[x][y + 1] == '0'){
- traverse(x, y + 1);
- }
- }
- /**
- * 以图形形式显示遍历结果
- */
- private void printGraph() {
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- System.out.print(flag[i][j] ? '*' : ' ');
- }
- System.out.println();
- }
- }
- /**
- * 以坐标形式显示遍历结果
- */
- private void printCoordinates() {
- System.out.println("遍历到的结点坐标:");
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- System.out.print(flag[i][j] ? (j + ", " + i + "/n") : "");
- }
- }
- }
- public static void main(String[] args) {
- // 遍历起点坐标
- int x = 4, y = 4;
- ChessDeepTraversal cdt = new ChessDeepTraversal();
- cdt.traverse(x, y);
- cdt.printGraph();
- cdt.printCoordinates();
- }
- }
运行结果
***
* *
** *
***
*
*
遍历到的结点坐标:
4, 2
5, 2
6, 2
4, 3
6, 3
1, 4
2, 4
4, 4
2, 5
3, 5
4, 5
4, 6
4, 7