Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- void swap(int&, int&);
- int left(int[][4], int[][4], int, int);
- int right(int[][4], int[][4], int, int);
- int up(int[][4], int[][4], int, int);
- int down(int[][4], int[][4], int, int);
- int calc(int[][4], int[][4]);
- int lowest(int[][4], int[][4], int, int, int, int, int, int);
- int zero(int [][4], int[][4], int[4], int, int, int, int, int, int);
- int count(int[][4], int[][4], int[4], int, int, int, int, int, int);
- void print(int[][4], int, string);
- int main()
- {
- int cur[4][4]; // used to track the initial and current states
- int goal[4][4] = // Goal State
- {
- {1, 2, 3, 4}, // Row 0
- {5, 6, 7, 8}, // Row 1
- {9, 10, 11, 12}, // Row 2
- {13, 14, 15, 0} // Row 3
- };
- int i, j; // looping variables
- int row, col; // used for the location of the blank
- int h; // value obtained with the heuristic function
- int L, R, U, D; // operators
- string op; // stores last operator used
- int op_used; // represents an operator
- //int n=0; // number of nodes generated
- int len=1; // length of solution path
- cout << "Please input your initial state. Input the numbers from left to right with a space between each one, representing the blank space with the number 0." << endl;
- cout << endl;
- cin >> cur[0][0] >> cur[0][1] >> cur[0][2] >> cur[0][3];
- cin >> cur[1][0] >> cur[1][1] >> cur[1][2] >> cur[1][3];
- cin >> cur[2][0] >> cur[2][1] >> cur[2][2] >> cur[2][3];
- cin >> cur[3][0] >> cur[3][1] >> cur[3][2] >> cur[3][3];
- cout << endl;
- int cc[16] = {cur[0][0], cur[0][1], cur[0][2], cur[0][3], cur[1][0], cur[1][1], cur[1][2], cur[1][3], cur[2][0], cur[2][1], cur[2][2], cur[2][3], cur[3][0], cur[3][1], cur[3][2], cur[3][3]};
- int gg[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
- int final=0;
- for (i=0; i<=3; i++)
- {
- if (cc[i] == gg[i])
- final++;
- }
- // Obtain the current location of the blank space.
- for (i=0; i<=4; i++)
- {
- for (j=0; j<=4; j++)
- {
- if (cur[i][j] == 0)
- {
- row = i;
- col = j;
- }
- }
- }
- // Calculate h(IS)
- h = calc(cur, goal);
- // Print out the initial state.
- //cout << right << setw(2) << cur[0][0] << " " << setw(2) << cur[0][1] << " " << setw(2) << cur[0][2] << " " << setw(2) << cur[0][3] << endl;
- //cout << setw(2) << cur[1][0] << " " << setw(2) << cur[1][1] << " " << setw(2) << cur[1][2] << " " << setw(2) << cur[1][3] << endl;
- //cout << setw(2) << cur[2][0] << " " << setw(2) << cur[2][1] << " " << setw(2) << cur[2][2] << " " << setw(2) << cur[2][3] << endl;
- //cout << setw(2) << cur[3][0] << " " << setw(2) << cur[3][1] << " " << setw(2) << cur[3][2] << " " << setw(2) << cur[3][3] << endl;
- //cout << endl;
- //cout << "h(IS) = " << h << endl << endl;
- do
- {
- cout << "made it in the loop";
- L = left(cur, goal, row, col);
- R = right(cur, goal, row, col);
- U = up(cur, goal, row, col);
- D = down(cur, goal, row, col);
- cout << "went through first functions";
- if (L < R && L < U && L < D)
- {
- swap(cur[row][col], cur[row][col-1]);
- col = col - 1;
- op = "Left";
- print(cur, L, op);
- }
- else if (R < L && R < U && R < D)
- {
- swap(cur[row][col], cur[row][col+1]);
- col = col + 1;
- op = "Right";
- print(cur, R, op);
- }
- else if (U < L && U < R && U < D)
- {
- swap(cur[row][col], cur[row-1][col]);
- row = row - 1;
- op = "Up";
- print(cur, U, op);
- }
- else if (D < L && D < R && D < U)
- {
- swap(cur[row][col], cur[row+1][col]);
- row = row + 1;
- op = "Down";
- print(cur, D, op);
- }
- // If there is no lowest operator, find the lowest matching ones.
- // From there, we will find the number of rows and columns the blank is away from the right space
- // If this isn't right, then just count the TOTAL NUMBER.
- // this can be very difficult, because we would need to recreate these.....UGH......
- else
- {
- op_used = lowest(goal, cur, L, R, U, D, row, col);
- if (op_used == 1)
- {
- op = "Left";
- swap(cur[row][col], cur[row][col-1]);
- col = col - 1;
- print(cur, L, op);
- }
- else if (op_used == 2)
- {
- op = "Right";
- swap(cur[row][col], cur[row][col+1]);
- col = col + 1;
- print(cur, R, op);
- }
- else if (op_used == 3)
- {
- op = "Up";
- swap(cur[row][col], cur[row-1][col]);
- row = row - 1;
- print(cur, U, op);
- }
- else
- {
- op = "Down";
- swap(cur[row][col], cur[row+1][col]);
- row = row + 1;
- print(cur, D, op);
- }
- }
- int cc[16] = {cur[0][0], cur[0][1], cur[0][2], cur[0][3], cur[1][0], cur[1][1], cur[1][2], cur[1][3], cur[2][0], cur[2][1], cur[2][2], cur[2][3], cur[3][0], cur[3][1], cur[3][2], cur[3][3]};
- final = 0;
- for (i=0; i<=3; i++)
- {
- if (cc[i] == gg[i])
- final++;
- }
- } while (final < 16);
- // We can now assume that the goal state has been reached, and print it out.
- cout << "Operator: " << op << endl << endl;
- cout << right << setw(2) << cur[0][0] << " " << setw(2) << cur[0][1] << " " << setw(2) << cur[0][2] << " " << setw(2) << cur[0][3] << endl;
- cout << setw(2) << cur[1][0] << " " << setw(2) << cur[1][1] << " " << setw(2) << cur[1][2] << " " << setw(2) << cur[1][3] << endl;
- cout << setw(2) << cur[2][0] << " " << setw(2) << cur[2][1] << " " << setw(2) << cur[2][2] << " " << setw(2) << cur[2][3] << endl;
- cout << setw(2) << cur[3][0] << " " << setw(2) << cur[3][1] << " " << setw(2) << cur[3][2] << " " << setw(2) << cur[3][3];
- cout << endl << endl;
- // print out all the additional information
- //cout << "The total number of nodes generated is: " << n << endl;
- //cout << "The length of the solution path is: " << len;
- return 0;
- }
- void swap(int& a, int& b)
- {
- int temp; // used to hold value
- temp = a;
- a = b;
- b = temp;
- }
- // LEFT FUNCTION
- int left (int cur[][4], int g[][4], int row, int col)
- {
- int h;
- int c[4][4] = // copy of current state
- {
- {cur[0][0], cur[0][1], cur[0][2], cur[0][3]}, // Row 0
- {cur[1][0], cur[1][1], cur[1][2], cur[1][3]}, // Row 1
- {cur[2][0], cur[2][1], cur[2][2], cur[2][3]}, // Row 2
- {cur[3][0], cur[3][1], cur[3][2], cur[3][3]} // Row 3
- };
- if (col == 0)
- return 100;
- else
- swap(c[row][col], c[row][col-1]);
- // n++;
- h = calc(c, g);
- return h;
- }
- // RIGHT FUNCTION
- int right (int cur[][4], int g[][4], int row, int col)
- {
- int h;
- int c[4][4] = // copy of current state
- {
- {cur[0][0], cur[0][1], cur[0][2], cur[0][3]}, // Row 0
- {cur[1][0], cur[1][1], cur[1][2], cur[1][3]}, // Row 1
- {cur[2][0], cur[2][1], cur[2][2], cur[2][3]}, // Row 2
- {cur[3][0], cur[3][1], cur[3][2], cur[3][3]} // Row 3
- };
- if (col == 3)
- return 100;
- else
- swap(c[row][col], c[row][col+1]);
- //n++;
- h = calc(c, g);
- return h;
- }
- // UP FUNCTION
- int up (int cur[][4], int g[][4], int row, int col)
- {
- int h;
- int c[4][4] = // copy of current state
- {
- {cur[0][0], cur[0][1], cur[0][2], cur[0][3]}, // Row 0
- {cur[1][0], cur[1][1], cur[1][2], cur[1][3]}, // Row 1
- {cur[2][0], cur[2][1], cur[2][2], cur[2][3]}, // Row 2
- {cur[3][0], cur[3][1], cur[3][2], cur[3][3]} // Row 3
- };
- if (row == 0)
- return 100;
- else
- swap(c[row][col], c[row-1][col]);
- // n++;
- h = calc(c, g);
- return h;
- }
- // DOWN FUNCTION
- int down (int cur[][4], int g[][4], int row, int col)
- {
- int h;
- int c[4][4] = // copy of current state
- {
- {cur[0][0], cur[0][1], cur[0][2], cur[0][3]}, // Row 0
- {cur[1][0], cur[1][1], cur[1][2], cur[1][3]}, // Row 1
- {cur[2][0], cur[2][1], cur[2][2], cur[2][3]}, // Row 2
- {cur[3][0], cur[3][1], cur[3][2], cur[3][3]} // Row 3
- };
- if (row == 3)
- return 100;
- else
- swap(c[row][col], c[row+1][col]);
- // n++;
- h = calc(c, g);
- return h;
- }
- // CALCULATION FUNCTION
- int calc(int c[][4], int g[][4])
- {
- int i, j; // looping variables
- int h = 0; // value for heuristic function
- for (i=0; i<=3; i++)
- {
- for (j=0; j<=3; j++)
- {
- if (c[i][j] != g[i][0] && c[i][j] != g[i][1] && c[i][j] != g[i][2] && c[i][j] != g[i][3])
- h++;
- }
- }
- for (j=0; j<=3; j++)
- {
- for (i=0; i<=3; i++)
- {
- if (c[i][j] != g[0][j] && c[i][j] != g[1][j] && c[i][j] != g[2][j] && c[i][j] != g[3][j])
- h++;
- }
- }
- return h;
- }
- // LOWEST FUNCTION
- int lowest(int g[][4], int cur[][4], int L, int R, int U, int D, int row, int col)
- {
- int a[4] = {L, R, U, D}; // keeps track of the lowest values
- int op; // operator used
- if (L < R) // if left is less than right - we must TOSS right.
- {
- int a[4] = {L, U, D, 200};
- if (L < U) // if left is less than up, we must TOSS up.
- {
- int a[4] = {L, D, 200, 200};
- }
- else if (L > U) // must toss l
- {
- int a[4] = {U, D, 200, 200};
- }
- else
- {
- // assume that L = U
- if (L < D)
- int a[4] = {L, U, 200, 200};
- else
- int a[4] = {L, U, D, 200};
- }
- }
- else if (L > R) // but if left is greater, then we must toss left!
- {
- int a[4] = {R, U, D, 200};
- if (R < U) // if left is less than up, we must TOSS up.
- {
- int a[4] = {R, D, 200, 200};
- }
- else if (R > U) // must toss l
- {
- int a[4] = {U, D, 200, 200};
- }
- else
- {
- // assume that R = U
- if (R < D)
- int a[4] = {R, U, 200, 200};
- else
- int a[4] = {R, U, D, 200};
- }
- }
- op = zero(g, cur, a, L, R, U, D, row, col);
- return op;
- }
- int zero(int g[][4], int cur[][4], int a[], int L, int R, int U, int D, int row, int col)
- {
- int hL, hR, hU, hD;
- int i, temp;
- for (i=0; i<=3; i++)
- {
- if (a[i] == L)
- {
- col = col - 1;
- hL = (3 - row) + (3 - col);
- }
- else if (a[i] == R)
- {
- col = col + 1;
- hR = (3 - row) + (3 - col);
- }
- else if (a[i] == U)
- {
- row = row - 1;
- hR = (3 - row) + (3 - col);
- }
- else if (a[i] == D)
- {
- row = row + 1;
- hR = (3 - row) + (3 - col);
- }
- }
- if (a[0] != L && a[1] != L && a[2] != L && a[3] != L)
- {
- hL = 100;
- }
- else if (a[0] != R && a[1] != R && a[2] != R && a[3] != R)
- {
- hR = 100;
- }
- else if (a[0] != U && a[1] != U && a[2] != U && a[3] != U)
- {
- hU = 100;
- }
- else if (a[0] != D && a[1] != D && a[2] != D && a[3] != D)
- {
- hD = 100;
- }
- // now that all the proper values should be assigned, do proper checking
- if (hL < hR && hL < hU && hL < hD)
- {
- return 1;
- }
- else if (hR < hL && hR < hU && hR < hD)
- {
- return 2;
- }
- else if (hU < hL && hU < hR && hU < hD)
- {
- return 3;
- }
- else if (hD < hL && hD < hR && hD < hU)
- {
- return 4;
- }
- else
- {
- temp = count(g, cur, a, L, R, U, D, row, col);
- return temp;
- }
- }
- int count(int g[][4], int cur[][4], int a[], int L, int R, int U, int D, int row, int col)
- {
- int i, j, k; // looping variables
- int wL = 0;
- int wR = 0;
- int wU = 0;
- int wD = 0;
- int c[4][4] = // copy of current state
- {
- {cur[0][0], cur[0][1], cur[0][2], cur[0][3]}, // Row 0
- {cur[1][0], cur[1][1], cur[1][2], cur[1][3]}, // Row 1
- {cur[2][0], cur[2][1], cur[2][2], cur[2][3]}, // Row 2
- {cur[3][0], cur[3][1], cur[3][2], cur[3][3]} // Row 3
- };
- // loop through both the current and goal. save the number! the lowest one will get worked on.
- for (i=0; i<=3; i++)
- {
- if (a[i] == L)
- {
- swap(c[row][col], c[row][col-1]);
- for (j=0; j<=3; j++)
- {
- for (k=0; k<=3; k++)
- {
- if (c[j][k] != g[j][k])
- wL++;
- }
- }
- }
- else if (a[i] == R)
- {
- swap(c[row][col], c[row][col+1]);
- for (j=0; j<=3; j++)
- {
- for (k=0; k<=3; k++)
- {
- if (c[j][k] != g[j][k])
- wR++;
- }
- }
- }
- else if (a[i] == U)
- {
- swap(c[row][col], c[row-1][col]);
- for (j=0; j<=3; j++)
- {
- for (k=0; k<=3; k++)
- {
- if (c[j][k] != g[j][k])
- wU++;
- }
- }
- }
- else if (a[i] == D)
- {
- swap(c[row][col], c[row+1][col]);
- for (j=0; j<=3; j++)
- {
- for (k=0; k<=3; k++)
- {
- if (c[j][k] != g[j][k])
- wD++;
- }
- }
- }
- }
- if (a[0] != L && a[1] != L && a[2] != L && a[3] != L)
- {
- wL = 100;
- }
- else if (a[0] != R && a[1] != R && a[2] != R && a[3] != R)
- {
- wR = 100;
- }
- else if (a[0] != U && a[1] != U && a[2] != U && a[3] != U)
- {
- wU = 100;
- }
- else if (a[0] != D && a[1] != D && a[2] != D && a[3] != D)
- {
- wD = 100;
- }
- if (wL < wR && wL < wU && wL < wD)
- {
- return 1;
- }
- else if (wR < wL && wR < wU && wR < wD)
- {
- return 2;
- }
- else if (wU < wL && wU < wR && wU < wD)
- {
- return 3;
- }
- else if (wD < wL && wD < wR && wD < wU)
- {
- return 4;
- }
- }
- // PRINT FUNCTION
- void print(int c[][4], int h, string op)
- {
- cout << "Operator: " << op << endl << endl;
- cout << right << setw(2) << c[0][0] << " " << setw(2) << c[0][1] << " " << setw(2) << c[0][2] << " " << setw(2) << c[0][3] << endl;
- cout << setw(2) << c[1][0] << " " << setw(2) << c[1][1] << " " << setw(2) << c[1][2] << " " << setw(2) << c[1][3] << endl;
- cout << setw(2) << c[2][0] << " " << setw(2) << c[2][1] << " " << setw(2) << c[2][2] << " " << setw(2) << c[2][3] << endl;
- cout << setw(2) << c[3][0] << " " << setw(2) << c[3][1] << " " << setw(2) << c[3][2] << " " << setw(2) << c[3][3];
- cout << endl << endl << "h(S) = " << h << endl;
- //len++;
- }
Advertisement
Add Comment
Please, Sign In to add comment