⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 status.java

📁 八数码问题和舰科技符合时刻记得和反馈记得和反馈及时反馈记得是
💻 JAVA
字号:
package zdg;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream; //status类用来存储某一个状态元素
//import java.util.ArrayList;
import java.io.ObjectOutputStream;

public class Status implements Cloneable {
	private int status[][];
	// 用数组存储当前的状态(依次编号为123456789个位置,其中值为零的位置表示空)
	private int depth;
	// 当前状态的深度值,以便求解cost
	private int now_cost;
	// 有当前状态到目的状态的代价(),用来求cost
	private int cost;

	// 当前状态到目的状态的估计代价,用来决定下次搜索的状态

	public int getCost(Status finish) {
		int cost = 0;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (this.status[i][j] != finish.getStatus()[i][j])
					cost++;
			}

		}
		return cost;
	}

	public void setCost(int cost) {
		this.cost = cost;
	}

	public void setStatus(int[][] array) {
		this.status = array;

	}

	public int[][] getStatus() {
		return this.status;
	}

	public int[] getLoc() {// 获得0的坐标,以判断移动规则
		int loc[] = { 0, 0 };
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (this.status[i][j] == 0) {
					loc[0] = i;
					loc[1] = j;
				}
			}
		}
		return loc;
	}

	public int getValueByLoc(int row, int col) {// 根据位置获得需要交换的非零数据的值
		return this.status[row][col];
	}

	public void setValueByLoc(int row, int col, int value) {
		this.status[row][col] = value;
	}

	private Object cloneObject(Object obj) throws Exception {
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(byteOut);
		out.writeObject(obj);

		ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut
				.toByteArray());
		ObjectInputStream in = new ObjectInputStream(byteIn);

		return in.readObject();
	}

	public Status clone() {
		Status result = null;
		try {
			result = (Status) super.clone();
			result.status = this.status.clone();
			for (int i = 0; i < this.status.length; i++) {
				result.status[i] = this.status[i].clone();
			}

		} catch (CloneNotSupportedException e) {
			System.out.println("不能复制");
		}

		return result;
	}

	public void printStatus() {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				System.out.print(this.status[i][j] + "  ");
			}
			System.out.println();
		}
	}

	public boolean isFinish(Status finish) {// 判断当前转台是不是目的状态
		if (this.status.equals(finish.getStatus()))
			return true;
		else
			return false;

	}

	public boolean isUp() {// 获得当前状态的行位置,并判断是否能够上移
		int row = 0;
		row = this.getLoc()[0];
		if (row == 0)
			return false;
		else
			return true;
	}

	public boolean isDown() {// 获得当前状态的行位置,并判断是否能够下移
		int row = 0;
		row = this.getLoc()[0];
		if (row == 2)
			return false;
		else
			return true;

	}

	public boolean isLeft() {// 当前状态是否能够左移
		int col = 0;
		col = this.getLoc()[1];
		if (col == 0)
			return false;
		else
			return true;
	}

	public boolean isRight() {// 当前状态是否能够右移
		int col = 0;
		col = this.getLoc()[1];

		System.out.println(col);
		if (col == 2)
			return false;
		else
			return true;
	}

	public Status upMove() {// 当前状态空格上移,返回另一个状态
		int row = 0;
		int col = 0;
		int value = -1;// 定义临时变量存储要移动位置的非零值
		int temp[] = new int[2];// 定义临时数组存放空格位置的坐标
		Status after_move = new Status();

		temp = this.getLoc();
		row = temp[0];// 获得空格的行位置
		col = temp[1];
		// 数据的移动
		value = this.getValueByLoc(row - 1, col);
		this.setValueByLoc(row, col, value);
		this.setValueByLoc(row - 1, col, 0);
		after_move.setStatus(this.status);
		return after_move;
	}

	public Status downMove() {
		// 当前状态空格下移,返回另一个状态
		int row = 0;
		int col = 0;
		int value = -1;// 定义临时变量存储要移动的非零值
		int temp[] = new int[2];// 定义临时数组存放空格位置的坐标
		Status after_move = new Status();
		int[][] temp_status;
		temp_status = this.status;
		temp = this.getLoc();
		row = temp[0];// 获得空格的行位置
		col = temp[1];
		// 数据的移动
		value = this.getValueByLoc(row + 1, col);
		this.setValueByLoc(row, col, value);
		this.setValueByLoc(row + 1, col, 0);
		after_move.setStatus(this.status);
		return after_move;
	}

	public Status leftMove() {// 当前状态空格左移,返回另一个状态
		int row = 0;
		int col = 0;
		int value = -1;// 定义临时变量存储要移动的非零值
		int temp[] = new int[2];// 定义临时数组存放空格位置的坐标
		int[][] temp_status;
		temp_status = this.status;
		Status after_move = new Status();
		temp = this.getLoc();
		row = temp[0];// 获得空格的行位置
		col = temp[1];
		// 数据的移动
		value = this.getValueByLoc(row, col - 1);
		this.setValueByLoc(row, col, value);
		this.setValueByLoc(row, col - 1, 0);
		after_move.setStatus(this.status);
		return after_move;
	}

	public Status rightMove() {// 当前状态空格右移,返回另一个状态

		int row = 0;
		int col = 0;
		int value = -1;// 定义临时变量存储要移动的非零值
		int temp[] = new int[2];// 定义临时数组存放空格位置的坐标
		int[][] temp_status;
		temp_status = this.status;
		Status after_move = new Status();
		temp = this.getLoc();
		row = temp[0];// 获得空格的行位置
		col = temp[1];
		// 数据的移动
		value = this.getValueByLoc(row, col + 1);
		this.setValueByLoc(row, col, value);
		this.setValueByLoc(row, col + 1, 0);
		after_move.setStatus(this.status);
		return after_move;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -