SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | ||
5 | typedef struct offsets{ | |
6 | short int horiz; | |
7 | short int vert; | |
8 | }offsets ; | |
9 | offsets move[8]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}}; | |
10 | ||
11 | int length,basic_col=0,basic_row=0,token=0; | |
12 | void set_basic(){ | |
13 | int ok=0; | |
14 | printf("Please set length side of a square:"); | |
15 | scanf("%d",&length); | |
16 | printf("Please set a beginning:"); | |
17 | do{ | |
18 | if(ok) printf("Out of range,please enter again!!!\n"); | |
19 | scanf("%d",&basic_col); | |
20 | scanf("%d",&basic_row); | |
21 | ok=1; | |
22 | }while(basic_col<0 || basic_col>=length ||basic_row<0||basic_row>=length); | |
23 | } | |
24 | ||
25 | int chess[100][100]={0}; | |
26 | ||
27 | void printBoard() { | |
28 | int col,row; | |
29 | for(row=0;row<length;row++) { | |
30 | for(col=0;col<length;col++) { | |
31 | printf("%3d ", chess[row][col]); | |
32 | } | |
33 | puts(""); | |
34 | } | |
35 | } | |
36 | ||
37 | void path(int row,int col,int step){ | |
38 | int nextcol,nextrow,dir; | |
39 | if (step == (length * length )&&!chess[row][col]&& !token++) { | |
40 | chess[row][col] = step; | |
41 | printf( "FOUND!\n"); | |
42 | printBoard(); | |
43 | printf( "=============================\n" ); | |
44 | chess[row][col] = 0; | |
45 | } | |
46 | if (token) return; | |
47 | - | if (!chess[row][col]&&step !=25) { |
47 | + | if (!chess[row][col]) { |
48 | chess[row][col] = step; | |
49 | for(dir=0;dir<8;dir++){ | |
50 | nextcol = col + move[dir].horiz; | |
51 | nextrow = row + move[dir].vert; | |
52 | if (nextrow < length && nextrow > -1 &&nextcol < length && nextcol > -1) { | |
53 | path(nextrow, nextcol, step + 1); | |
54 | } | |
55 | } | |
56 | chess[row][col] = 0; | |
57 | ||
58 | } | |
59 | } | |
60 | ||
61 | main(){ | |
62 | set_basic(); | |
63 | path(basic_row,basic_col,1); | |
64 | system("pause"); | |
65 | return 0; | |
66 | } |