Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct offsets{
- short int horiz;
- short int vert;
- }offsets ;
- offsets move[8]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
- int length,basic_col=0,basic_row=0,token=0;
- void set_basic(){
- int ok=0;
- printf("Please set length side of a square:");
- scanf("%d",&length);
- printf("Please set a beginning:");
- do{
- if(ok) printf("Out of range,please enter again!!!\n");
- scanf("%d",&basic_col);
- scanf("%d",&basic_row);
- ok=1;
- }while(basic_col<0 || basic_col>=length ||basic_row<0||basic_row>=length);
- }
- int chess[100][100]={0};
- void printBoard() {
- int col,row;
- for(row=0;row<length;row++) {
- for(col=0;col<length;col++) {
- printf("%3d ", chess[row][col]);
- }
- puts("");
- }
- }
- void path(int row,int col,int step){
- int nextcol,nextrow,dir;
- if (step == (length * length )&&!chess[row][col]&& !token++) {
- chess[row][col] = step;
- printf( "FOUND!\n");
- printBoard();
- printf( "=============================\n" );
- chess[row][col] = 0;
- }
- if (token) return;
- if (!chess[row][col]&&step !=25) {
- chess[row][col] = step;
- for(dir=0;dir<8;dir++){
- nextcol = col + move[dir].horiz;
- nextrow = row + move[dir].vert;
- if (nextrow < length && nextrow > -1 &&nextcol < length && nextcol > -1) {
- path(nextrow, nextcol, step + 1);
- }
- }
- chess[row][col] = 0;
- }
- }
- main(){
- set_basic();
- path(basic_row,basic_col,1);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement