Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int dx[8] = { -1, 1, 2, 2, -1, 1, -2, -2 };
- int dy[8] = { -2, -2, -1, 1, 2, 2, -1, 1 };
- int Px = 0, Py = 0;
- int A[5][5];
- int acceptable(int k){
- if ((Px + dx[k] >= 0) && (Py + dy[k]>=0 ))
- if ((Px + dx[k]<=4) && (Py+dy[k]<=4 ))
- if (A[Px + dx[k]][Py + dy[k]] == 0)
- return 1;
- return 0;
- }
- int solution(int k, int i){
- return (i==24);
- }
- void printsolution(){
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- printf(" %d ", A[i][j]);
- printf("\n");
- }
- }
- void back(int i){
- for (int k = 0; k < 8; k++)
- if (acceptable(k)){
- A[Px + dx[k]][Py + dy[k]] = i;
- Px = Px + dx[k];
- Py = Py + dy[k];
- if (solution(k, i)){
- printsolution();
- printf("\n");
- }
- else
- back(i + 1);
- A[Px][Py] = 0;
- Px = Px - dx[k];
- Py = Py - dy[k];
- }
- }
- int main(void){
- A[0][0] = 1;
- back(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement