Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

source_code

asd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

source_code

asd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

package Taci_Algorithm;

/*

* Mục đích: Quản lý nghiệp vụ về đỉnh của bài toán Taci

* Người viết:

* Ngày viết:

* Version:

*/

import java.util.*;

public class Vertex {

private int[][] state, goal;

private int cost;

private int heuristic;

private Vertex parent;

private int row, col;

public Vertex(int[][] state, int goal[][], int cost) {

this.state = state;

this.goal = goal;

this.cost = cost;

this.heuristic = calculateHeuristic();

public void setRowCol(int row, int col) {

this.col = col;

this.row = row;

}
// public Vertex(int[][] state, int cost) {

// this.state = state;

// this.cost = cost;

// }

public int[][] getState() {

return state;

public void setGoal(int goal[][]) {

this.goal = goal;

public int getCost() {

return cost;

public int getHeuristic() {

return heuristic;

public Vertex getParent() {

return parent;

public List<Vertex> getNeighbors() {

List<Vertex> neighbors = new ArrayList<>();

// danh sach cac node ke

for (int[] move : getValidMoves()) {

int[][] newState = Arrays.stream(state).map(int[]::clone).toArray(int[][]::new);


swap(newState, row, col, move[0], move[1]);

Vertex neighbor = new Vertex(newState, goal, cost + 1);

neighbor.parent = this;

neighbor.row = move[0];

neighbor.col = move[1];

neighbors.add(neighbor);

return neighbors;

private List<int[]> getValidMoves() {

List<int[]> moves = new ArrayList<>();

for (int[] move : new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }) {

int newRow = row + move[0];

int newCol = col + move[1];

if (newRow >= 0 && newRow < 3 && newCol >= 0 && newCol < 3) {

moves.add(new int[] { newRow, newCol });

return moves;

public int calculateHeuristic() {

int distance = 0;

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)


if (i != 1 || j != 1)

if (state[i][j] != goal[i][j])

distance++;

return distance ;

private void swap(int[][] state, int i, int j, int x, int y) {

int temp = state[i][j];

state[i][j] = state[x][y];

state[x][y] = temp;

@Override

public boolean equals(Object o) {

if (this == o)

return true;

if (o == null || getClass() != o.getClass())

return false;

Vertex node = (Vertex) o;

return Arrays.deepEquals(state, node.state);

@Override

public int hashCode() {

return Arrays.deepHashCode(state);

}
package Taci_Algorithm;

import java.util.*;

public class Taci {


private PriorityQueue<Vertex> queue;
private Set<Vertex> visited;
private int[][] goal;

public Taci(int[][] start, int[][] goal, int row, int col) {


this.visited = new HashSet<>();
this.goal = goal;
Vertex startNode = new Vertex(start, goal, 0);
this.queue = new
PriorityQueue<>(Comparator.comparingInt(Vertex::getHeuristic));
startNode.setRowCol(row, col);
queue.offer(startNode);
}

public List<Vertex> solve() {


int i = 0;
while (!queue.isEmpty()) {
System.out.println("Loop " + (i++));
// In thông tin về đỉnh đang xét
Vertex current = queue.poll();
int[][] currentState = current.getState();
System.out.print("Đỉnh đang xét: ");
for (int[] row : currentState) {
System.out.print(Arrays.toString(row) + " ");
}
System.out.println("\ng: " + current.getCost() + "\th: "
+ current.getHeuristic() + "\tf: "
+ (current.getHeuristic() + current.getCost()));
// In thông tin về đỉnh kề
System.out.println("Đỉnh kề với v: ");

for (Vertex neighbor : current.getNeighbors()) {


int[][] neighborState = neighbor.getState();
for (int[] row : neighborState) {
System.out.print(Arrays.toString(row) + " ");
}
System.out.println("\ng: " + neighbor.getCost() +
"\th: " + neighbor.getHeuristic() + "\tf: "
+ (neighbor.getHeuristic() +
neighbor.getCost()) + " ");

}
System.out.println();

// In thông tin về hàng đợi Open


System.out.println("Open:");
for (Vertex node : queue) {
int[][] state = node.getState();
System.out.print(Arrays.deepToString(state) + " ");
System.out.println("g: " + node.getCost() + " h: "
+ node.getHeuristic() + " f: "
+ (node.getHeuristic() + node.getCost()));
}

// In thông tin về tập Closed


System.out.println("\nClosed:");
for (Vertex v : visited) {
int[][] state = v.getState();
System.out.print(Arrays.deepToString(state) + " ");
System.out.println("g: " + v.getCost() + " h: " +
v.getHeuristic() + " f: "
+ (v.getHeuristic() + v.getCost()));
}
System.out.println("---------------------\n\n");

visited.add(current);

if (Arrays.deepEquals(current.getState(), goal)) {
System.out.println("Goal reached!");
return getPath(current);
}

for (Vertex neighbor : current.getNeighbors())


if (!visited.contains(neighbor))
queue.offer(neighbor);
}
System.out.println("No solution found!");
return null;
}

private List<Vertex> getPath(Vertex end) {


List<Vertex> path = new ArrayList<>();
Vertex current = end;

while (current != null) {


path.add(current);
current = current.getParent();
}
Collections.reverse(path);
return path;
}

public static void main(String[] args) {


int[][] start = { { 2, 8, 3 }, { 1, 6, 4 }, { 7, 0, 5 } };
int[][] goal = { { 1, 2, 3 }, { 8, 0, 4 }, { 7, 6, 5 } };
int r = 2, c = 1;
Taci solver = new Taci(start, goal, r, c);
List<Vertex> solution = solver.solve();

if (solution == null) {
System.out.println("No Result!");
} else {
System.out.println("Result: ");
for (Vertex node : solution) {
int[][] state = node.getState();
System.out.println("----------");
for (int[] row : state) {
System.out.println(Arrays.toString(row));
}
}
System.out.println("----------");
}
}
}

You might also like