连连看游戏判断两个块是否可以消去,并计算最小转弯数
2015-05-19 15:09
387 查看
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);
4000
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();
}
}
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);
4000
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();
}
}
相关文章推荐
- 连连看游戏判断两个块是否可以消去,并计算最小转弯数
- Naive and Silly Muggles(计算几何 判断点是否在三点形成的最小圆内)
- 每天一道LeetCode-----计算二叉树的最大深度及最小深度,判断二叉树是否是高度平衡二叉树
- 2013华为杯编程大赛成都第三组试题--------C 连连看-判断两个图案是否可以消去
- 判断是否可以联网
- Hat’s Words 【字典树 判断字符串是否可以为字符串集中两个字符串组成】
- python计算输入的两个数字,try...except...判断是否输入的是数字,如果是则相加
- 编译期判断类型之间是否可以convert
- 在iphone上面执行外部命令的方式(以此可能可以判断iphone是否已经越狱)
- iOS 判断相机权限是否被限制,判断相机是否可以使用
- 基于MATLAB的线性代数 对角化是否可以进行的判断
- java判断字符串是否可以转为数字
- nyoj 修路方案 (判断最小生成树是否唯一)
- hrbustoj 1429:凸多边形(计算几何,判断点是否在多边形内,二分法)
- POJ 1308 Is It A Tree? 并查集 (判断任意2个点是否有且仅有一条路径可以相通)
- Codeforces Gym 100523K K - Cross Spider 计算几何,判断是否n点共面
- 如何判断是否有环,如何计算环的长度,如何判断环的入口点
- HashSet 重写类的equals方法可以根据类的关键字段判断是否相等
- 判断三个参数是否能构成一个三角形。如果不能则抛出异常IllegalArgumentException, * 显示异常信息:a,b,c “不能构成三角形”;如果可以构成则显示三角形三个边长。在主方
- poj 1679 判断最小生成树是否唯一