Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ma di tuan voi n ban co
- // Khai bao cac ham thu vien neu co
- #include <iostream>
- #include <iomanip>
- using namespace std;
- // Khoi tao cac gia tri ban dau
- int p[8] = { -2, -2, -1, -1, 1, 1, 2, 2 };
- int q[8] = { -1, 1, -2, 2, -2, 2, -1, 1 };
- int d = 0, x, y, n, a[50][50];
- void inkq();
- void tim(int x, int y, int k);
- // Ham chinh chuong trinh
- int main()
- {
- cout << "Nhap KT: ";// nhap kich thuoc
- cin >> n;
- x = 0;
- y = 0;
- for (int i = 0; i<n; i++)
- for (int j = 0; j<n; j++)
- a[i][j] = 0;
- a[x][y] = 1;
- tim(x, y, 2);
- if (d == 0)
- cout << " K giai duoc!" << endl;
- else
- cout << " Co tat ca " << d << " cach" << endl;
- return 0;
- }
- // In ket qua tim duoc cac buoc di cua chuong trinh
- void inkq()
- {
- cout << endl << " #" << ++d << "" << endl;
- for (int i = 0; i<n; i++)
- {
- for (int j = 0; j < n; j++)
- cout << setw(3) << a[i][j];
- cout << endl;
- }
- cout << endl;
- }
- // Thuat toan tim duong di
- void tim(int x, int y, int k)
- {
- if (k>n*n)
- inkq();
- else
- for (int i = 0; i<8; i++)
- {
- int x1 = x + p[i], y1 = y + q[i];
- if (x1 >= 0 && x1<n && y1 >= 0 && y1<n)
- if (a[x1][y1] == 0)
- {
- a[x1][y1] = k;
- tim(x1, y1, k + 1);
- a[x1][y1] = 0;
- }
- }
- }
- // Nhan Ctrl + F7 de bien dich chuong trinh
- // Nhan Ctrl + F5 de chay chuong trinh
- // no se liet ke cac truong hop ma chuong trinh se chay
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement