连连看游戏判断两个块是否可以消去,并计算最小转弯数

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;


public class lianliankan_core {

    //连连看布局
    private int[][] myArray = new int[][]{{0,0,0,0,0},
                                          {0,1,2,1,0},
                                          {0,2,0,5,0},
                                          {0,1,0,4,0},
                                          {0,4,5,1,0},
                                          {0,0,0,0,0}};
    //是否访问过标志
    private int[][] visited = new int[][]{{0,0,0,0,0},
                                          {0,0,0,0,0},
                                          {0,0,0,0,0},
                                          {0,0,0,0,0},
                                          {0,0,0,0,0},
                                          {0,0,0,0,0}};
    //方向左、右、下、上
    private Point[] direction = new Point[]{new Point(-1,0),
                                            new Point(1,0),
                                            new Point(0, -1),
                                            new Point(0, 1)};
    private Point p1;//起始点
    private Point p2;//终点
    //点的队列
    private Queue<Point> q1 = new LinkedBlockingDeque<Point>();
    
    public lianliankan_core(){
        //(x1,y1)=(4,2)
        p1 = new Point(4,2);
        //(x2,y2)=(2,3)
        p2 = new Point(2,3);
        
        visited[p1.x][p1.y] = 1;
        q1.add(p1);
    }
    
    //将point点可以直线到达的点放入队列q1中
    public void find(){
        while(!q1.isEmpty()){
            Point newPoint = (Point) q1.poll();
            for (Point p : direction){
                Point nextPoint = new Point(newPoint.x, newPoint.y, newPoint.turnCount);
                nextPoint.plus(p);
                while (nextPoint.check()){
                    q1.add(new Point(nextPoint.x, nextPoint.y, nextPoint.turnCount + 1));
                    nextPoint.plus(p);
                }
                if(nextPoint.equals(p2) && nextPoint.turnCount <= 2){
                    if(myArray[nextPoint.x][nextPoint.y] == myArray[p2.x][p2.y]){
                        System.out.println("可以消去!");
                        System.out.println("最小转变数为:" + nextPoint.turnCount);
                        return;
                    }else{
                        System.out.println("不能消去!");
                        return;
                    }
                }
            }
        }
    }
    
    private class Point{
        private int x;
        private int y;
        private int turnCount;//转弯数
        
        public Point(int x, int y){
            this.x = x;
            this.y = y;
            this.turnCount = 0;
        }
        
        public Point(int x, int y, int turnCount){
            this.x = x;
            this.y = y;
            this.turnCount = turnCount;
        }
        
        public Point plus(Point p1){
            this.x += p1.x;
            this.y += p1.y;
            this.turnCount += p1.turnCount;
            return this;
        }
        
        public boolean check(){
            if(this.x >= 0 && this.x < 6 && this.y >= 0 && this.y < 5 && myArray[this.x][this.y] == 0 && visited[this.x][this.y] == 0){
                visited[this.x][this.y] = 1;
                return true;
            }
            return false;
        }
        
        public String toString(){
            return "Point(" + x + ", " + y +")";
        }
        
        public boolean equals(Point p){
            if(this.x == p.x && this.y == p.y)
                return true;
            return false;
        }
        
    }
    
    public static void main(String[] args){
        lianliankan_core llk = new lianliankan_core();
        llk.find();
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值