#include #include #include #include void left(int ); void right(int); void up(int ); void down(int ); void print_state(int [][]); void get_state(int [][]); // code for actions: 0 - right, 1 - left, 2 - down, 3 - up typedef struct board { int state[3][3]; int moves[100]; int NUMBER_OF_MOVES; int h2; int done; } BOARD; BOARD rt[200000]; int curMax = 0; int goal_state[3][3]; void add_one(BOARD, int); int h2(int [][]); void expand(); int main() { int i, j; int initial_state[3][3]; BOARD b; printf("Input the initial state\n"); get_state(initial_state); printf("Input the goal state\n"); get_state(goal_state); print_state(initial_state); printf("\n"); print_state(goal_state); for(i=0 ; i<3 ; i++) { for(j=0 ; j<3 ; j++) b.state[i][j] = initial_state[i][j]; } b.NUMBER_OF_MOVES = 0; b.h2 = h2(b.state); //printf("Heuristic h2 for the initial state %d\n", h2(initial_state)); //printf("Heuristic h2 for the goal state %d\n", h2(goal_state)); add_one(b, -1); //printf("\n"); // print_state(rt[0].state); //printf("Heuristic h2 for the initial state %d\n", rt[0].h2); expand(); /* for(i = 0; i < curMax; i++){ printf("\n"); print_state(rt[i].state); printf("Heuristic h2 for this state %d - done expanding %d\n", rt[i].h2,rt[i].done); } */ return 0; } void print_solution(int cnt) { BOARD b = rt[cnt]; int i; print_state(b.state); for(i=0; i %d -- No of moves %d\n",rt[bToExpand].h2,rt[bToExpand].NUMBER_OF_MOVES); left(bToExpand); right(bToExpand); down(bToExpand); up(bToExpand); rt[bToExpand].done = 1; } } void left(int cnt) { int i,j,found = 0; BOARD b = rt[cnt]; if (b.state[0][0]==0 || b.state[1][0]==0 || b.state[2][0]==0) return; for (i=0; i<3 ; i++){ for(j=0; j<3; j++){ if (b.state[i][j] == 0){ b.state[i][j] = b.state[i][j-1]; b.state[i][j-1] = 0; found = 1; break; } } if (found == 1) break; } b.h2 = b.NUMBER_OF_MOVES + h2(b.state); add_one(b, 1); } void right(int cnt) { int i,j,found = 0; BOARD b = rt[cnt]; if (b.state[0][2]==0 || b.state[1][2]==0 || b.state[2][2]==0) return; for (i=0; i<3 ; i++){ for(j=0; j<3; j++){ if (b.state[i][j] == 0){ b.state[i][j] = b.state[i][j+1]; b.state[i][j+1] = 0; found = 1; break; } } if (found == 1) break; } b.h2 = b.NUMBER_OF_MOVES + h2(b.state); add_one(b, 0); } void down(int cnt) { int i,j,found = 0; BOARD b = rt[cnt]; if (b.state[2][0]==0 || b.state[2][1]==0 || b.state[2][2]==0) return; for (i=0; i<3 ; i++){ for(j=0; j<3; j++){ if (b.state[i][j] == 0){ b.state[i][j] = b.state[i+1][j]; b.state[i+1][j] = 0; found = 1; break; } } if (found == 1) break; } b.h2 = b.NUMBER_OF_MOVES + h2(b.state); add_one(b, 2); } void up(int cnt) { int i,j,found = 0; BOARD b = rt[cnt]; if (b.state[0][0]==0 || b.state[0][1]==0 || b.state[0][2]==0) return; for (i=0; i<3 ; i++){ for(j=0; j<3; j++){ if (b.state[i][j] == 0){ b.state[i][j] = b.state[i-1][j]; b.state[i-1][j] = 0; found = 1; break; } } if (found == 1) break; } b.h2 = b.NUMBER_OF_MOVES + h2(b.state); add_one(b, 3); } int h2(int state[][3]) { int i, j, k, l; int current, found; int counter = 0; for(i=0; i<3; i++){ for(j=0; j<3; j++){ current = state[i][j]; found = 0; // printf("%d ", current); for(k=0; k<3; k++){ for(l=0; l<3; l++){ if (current == goal_state[k][l]){ found = 1; break; } } if (found==1) break; } counter = counter + abs(i-k)+abs(j-l); // printf("%d \n", counter); } } // add h1 to h2 return counter; } void add_one(BOARD b, int code) { int i,j; int k,l; int found; found = 0; for(i = 0; i