-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tile_Heuristic.java
210 lines (185 loc) · 4.98 KB
/
Tile_Heuristic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.util.LinkedList;
import java.util.Queue;
//import BFSmatrix.QItem;
/**
*
* @author Boaz Sharabi
*
*This class represent heuristic functions for tile game
*/
public class Tile_Heuristic {
/**
*
* @param t the tile for calculate the estimate cost until the goal state
* @return the estimate cost until the goal state
*/
public static int Manethen_Distance(tile t) {
int sum = 0;
int right_row;
int right_col;
int move=1;
Board board= t.getBoard();
for(int i=0;i<board.mat.length;i++) {
for(int j=0;j<board.mat[0].length;j++) {
if(board.mat[i][j]!=-1) {
move=1;
if(board.color_cell.get(board.mat[i][j])==Color.RED)move+=29;
right_row=(board.mat[i][j]-1)/(board.mat[0].length);
right_col= (board.mat[i][j]-1)%(board.mat[0].length);
sum+=Manethen_Distance(i,j,right_row,right_col)*move;
}
}
}
return sum;
}
/**
*
* @param t the tile for calculate the estimate cost until the goal state
* @return the estimate cost until the goal state
*/
public static int BFS_Distance(tile t) {
int sum = 0;
int move=1;
Board board= t.getBoard();
BFSmatrix bfs = new BFSmatrix(t);
for(int i=0;i<board.mat.length;i++) {
for(int j=0;j<board.mat[0].length;j++) {
if(board.mat[i][j]!=-1) {
move=1;
if(board.color_cell.get(board.mat[i][j])==Color.RED)move+=29;;
int dist= bfs.minDistance(i, j);
if(dist<0) return -1; // no possible solution!
sum+=dist*move;
}
}
}
return sum;
}
/**
*
* @param row
* @param col colum
* @param right_row
* @param right_col
* @return Manethen Distance between (row,col) to (right row , right col)
*/
private static int Manethen_Distance(int row, int col, int right_row, int right_col) {
return Math.abs(row-right_row)+Math.abs(col-right_col);
}
/**
*
* @author Boaz Sharabi
*
*
*
* based on https://www.geeksforgeeks.org/shortest-distance-two-cells-matrix-grid/
*/
public static class BFSmatrix {
tile s;
int m;
int n;
char[][] grid;
QItem source;
public BFSmatrix(tile s ) {
// TODO Auto-generated constructor stub
n= s.getBoard().mat.length;
m= s.getBoard().mat[0].length;
this.s=s;
}
/**
*
* @param board
*/
private void initGrid(Board board) {
grid= new char[n][m];
// TODO Auto-generated method stub
for(int i=0;i<board.mat.length;i++) {
for(int j=0;j<board.mat[0].length;j++) {
if((board.mat[i][j]!=-1)&&(i!=source.row||j!=source.col) ){
if(board.color_cell.get(board.mat[i][j])==Color.BLACK) {
if(i!=(board.mat[i][j]-1)/(board.mat[0].length)||j!=(board.mat[i][j]-1)%(board.mat[0].length)) {
grid=null;
return;
}
else grid[i][j]= '*';
}
else grid[i][j]= '0';
}
else if(board.mat[i][j]==-1) grid[i][j]= '0';
else grid[i][j]= 's';
}
}
grid[(board.mat[source.row][source.col]-1)/(board.mat[0].length)][(board.mat[source.row][source.col]-1)%(board.mat[0].length)]= 'd';
}
// QItem for current location and distance
// from source location
/**
* @author User
*
*/
class QItem {
public int row;
public int col;
public int dist;
QItem(int x, int y, int w)
{
row=x;
col=y;
dist=w;
}
};
int minDistance(int i,int j)
{
source= new QItem(i,j,0);
initGrid(s.getBoard());
if (grid==null)return -2;
// To keep track of visited QItems. Marking
// blocked cells as visited.
Boolean[][] visited = new Boolean[n][m];
for (int i1 = 0; i1 < n; i1++) {
for (int j1 = 0; j1 < m; j1++)
{
if (grid[i1][j1] == '0'||grid[i1][j1] == 'd')
visited[i1][j1] = false;
else
visited[i1][j1] = true;
}
}
// applying BFS on matrix cells starting from source
Queue<QItem> q = new LinkedList<>();;
q.add(source);
visited[source.row][source.col] = true;
while (!q.isEmpty()) {
QItem p = q.poll();
// Destination found;
if (grid[p.row][p.col] == 'd')
return p.dist;
// moving up
if (p.row - 1 >= 0 &&
visited[p.row - 1][p.col] == false) {
q.add(new QItem(p.row - 1, p.col, p.dist + 1));
visited[p.row - 1][p.col] = true;
}
// moving down
if (p.row + 1 < n &&
visited[p.row + 1][p.col] == false) {
q.add(new QItem(p.row + 1, p.col, p.dist + 1));
visited[p.row + 1][p.col] = true;
}
// moving left
if (p.col - 1 >= 0 &&
visited[p.row][p.col - 1] == false) {
q.add(new QItem(p.row, p.col - 1, p.dist + 1));
visited[p.row][p.col - 1] = true;
}
// moving right
if (p.col + 1 < m &&
visited[p.row][p.col + 1] == false) {
q.add(new QItem(p.row, p.col + 1, p.dist + 1));
visited[p.row][p.col + 1] = true;
}
}
return -1;
}
}
}