现在的位置: 首页 > 综合 > 正文

查找同色围棋子的广度和深度遍历方法

2013年05月04日 ⁄ 综合 ⁄ 共 4901字 ⁄ 字号 评论关闭
原文地址:http://www.java2000.net/p9798

从A点开始进行4方向遍历

目的是找到和A点相连并且拥有相同棋面的棋子个数

  1. package net.java2000.ag;
  2. import java.awt.Point;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /**
  6.  * 查找同色围棋子的广度遍历方法。
  7.  * 
  8.  * @author 赵学庆,Java世纪网(java2000.net)
  9.  * 
  10.  */
  11. public class ChessWideTraversal {
  12.   static char[][] chess = { { '1''1''1''1''1''1''1''1''1' },
  13.     { '1''1''1''1''1''1''1''1''1' },
  14.     { '1''1''1''1''0''0''0''1''1' },
  15.     { '1''1''1''1''0''1''0''1''1' },
  16.     { '1''0''0''1''A''1''1''1''1' },
  17.     { '1''1''0''0''0''1''1''1''1' },
  18.     { '1''1''1''1''0''1''1''1''1' },
  19.     { '1''1''1''1''0''1''1''1''1' },
  20.     { '1''1''1''1''1''1''1''1''1' },
  21.     { '1''1''1''1''1''1''1''1''1' } };
  22.   
  23.   // 如果已经遍历了,则不再重复
  24.   static boolean[][] haveFound = new boolean[chess.length][chess[0].length];
  25.   /**
  26.    * 被查找的广度遍历。
  27.    * 
  28.    * @param x
  29.    *          种子横坐标
  30.    * @param y
  31.    *          种子纵坐标
  32.    * @param sign
  33.    *          颜色
  34.    * @return 同色数组
  35.    */
  36.   public static List<Point> find(int x, int y, char sign) {
  37.     List<Point> list = new ArrayList<Point>();
  38.     Point p = new Point(x, y);
  39.     list.add(p); // 起始的种子
  40.     for (int i = 0; i < list.size(); i++) {
  41.       p = list.get(i);// 当前位置
  42.       // 左侧,没有到边界
  43.       if (p.x > 0) {
  44.         // 数值等于0 且没有找过
  45.         if (chess[p.y][p.x - 1] == sign && !haveFound[p.y][p.x - 1]) {
  46.           list.add(new Point(p.x - 1, p.y));
  47.           haveFound[p.y][p.x - 1] = true;
  48.         }
  49.       }
  50.       // 上侧,没有到边界
  51.       if (p.y > 0) {
  52.         // 数值等于0 且没有找过
  53.         if (chess[p.y - 1][p.x] == sign && !haveFound[p.y - 1][p.x]) {
  54.           list.add(new Point(p.x, p.y - 1));
  55.           haveFound[p.y - 1][p.x] = true;
  56.         }
  57.       }
  58.       // 右侧,没有到边界
  59.       if (p.x < chess[0].length - 1) {
  60.         // 数值等于0 且没有找过
  61.         if (chess[p.y][p.x + 1] == sign && !haveFound[p.y][p.x + 1]) {
  62.           list.add(new Point(p.x + 1, p.y));
  63.           haveFound[p.y][p.x + 1] = true;
  64.         }
  65.       }
  66.       // 下侧,没有到边界
  67.       if (p.y < chess[0].length - 1) {
  68.         // 数值等于0 且没有找过
  69.         if (chess[p.y + 1][p.x] == sign && !haveFound[p.y + 1][p.x]) {
  70.           list.add(new Point(p.x, p.y + 1));
  71.           haveFound[p.y + 1][p.x] = true;
  72.         }
  73.       }
  74.     }
  75.     return list;
  76.   }
  77.   /**
  78.    * 以图形形式显示遍历结果
  79.    */
  80.   private static void printGraph() {
  81.     for (int i = 0; i < chess.length; i++) {
  82.       for (int j = 0; j < chess[0].length; j++) {
  83.         System.out.print(haveFound[i][j] ? '*' : ' ');
  84.       }
  85.       System.out.println();
  86.     }
  87.   }
  88.   
  89.   public static void main(String[] args) {
  90.     List<Point> list = find(44'0');
  91.     printGraph();
  92.     for (Point p : list) {
  93.       System.out.println(p.x + "," + p.y);
  94.     }
  95.   }
  96. }

运行结果

        
        
    *** 
    * * 
 **     
  ***   
    *   
    *   
        
        
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
   

  1. package net.java2000.ag;
  2. /**
  3.  * 查找同色围棋子的深度遍历方法
  4.  * 
  5.  * @author xifo,Java世纪网(java2000.net)
  6.  * 
  7.  */
  8. public class ChessDeepTraversal {
  9.   private char[][] layout = { { '1''1''1''1''1''1''1''1''1' },
  10.       { '1''1''1''1''1''1''1''1''1' },
  11.       { '1''1''1''1''0''0''0''1''1' },
  12.       { '1''1''1''1''0''1''0''1''1' },
  13.       { '1''0''0''1''A''1''1''1''1' },
  14.       { '1''1''0''0''0''1''1''1''1' },
  15.       { '1''1''1''1''0''1''1''1''1' },
  16.       { '1''1''1''1''0''1''1''1''1' },
  17.       { '1''1''1''1''1''1''1''1''1' },
  18.       { '1''1''1''1''1''1''1''1''1' } };
  19.   // 9行 10列
  20.   private int w = 9, h = 10;
  21.   private boolean[][] flag = new boolean[h][w];
  22.   /**
  23.    * 以 x, y 为起点进行深度优先遍历
  24.    */
  25.   private void traverse(int x, int y) {
  26.     flag[x][y] = true;
  27.     // 尝试遍历左侧结点
  28.     if(x > 0 && !flag[x-1][y] && layout[x-1][y] == '0'){
  29.       traverse(x - 1, y);
  30.     }
  31.     // 尝试遍历上侧结点
  32.     if(y > 0 && !flag[x][y-1] && layout[x][y-1] == '0'){
  33.       traverse(x, y - 1);
  34.     }
  35.     // 尝试遍历右侧结点
  36.     if(x < w - 1 && !flag[x + 1][y] && layout[x + 1][y] == '0'){
  37.       traverse(x + 1, y);
  38.     }
  39.     // 尝试遍历下侧结点
  40.     if(y < h - 1 && !flag[x][y + 1] && layout[x][y + 1] == '0'){
  41.       traverse(x, y + 1);
  42.     }
  43.   }
  44.   /**
  45.    * 以图形形式显示遍历结果
  46.    */
  47.   private void printGraph() {
  48.     for (int i = 0; i < h; i++) {
  49.       for (int j = 0; j < w; j++) {
  50.         System.out.print(flag[i][j] ? '*' : ' ');
  51.       }
  52.       System.out.println();
  53.     }
  54.   }
  55.   /**
  56.    * 以坐标形式显示遍历结果
  57.    */
  58.   private void printCoordinates() {
  59.     System.out.println("遍历到的结点坐标:");
  60.     for (int i = 0; i < h; i++) {
  61.       for (int j = 0; j < w; j++) {
  62.         System.out.print(flag[i][j] ? (j + ", " + i + "/n") : "");
  63.       }
  64.     }
  65.   }
  66.   public static void main(String[] args) {
  67.     // 遍历起点坐标
  68.     int x = 4, y = 4;
  69.     ChessDeepTraversal cdt = new ChessDeepTraversal();
  70.     cdt.traverse(x, y);
  71.     cdt.printGraph();
  72.     cdt.printCoordinates();
  73.   }
  74. }

     
   运行结果

        
        
    *** 
    * * 
 ** *   
  ***   
    *   
    *   
        
        
遍历到的结点坐标:
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
     

抱歉!评论已关闭.