Soft-shelled turtle data structure and algorithm-horse stepping board (Knight travel problem)

Source: Internet
Author: User

The code is as follows:

#include <stdio.h>#include<time.h>#defineX 5#defineY 5intChess[x][y];voidprintchess () {inti,j; printf ("This is horse chess:\n");  for(i=0; i<x;i++){             for(j=0; j<y;j++) {printf ("%2d\t", Chess[i][j]); } printf ("\ n"); }}intNextint*x,int*y,intStep) {    Switch(step) { Case 0:            if(*y+2<=y-1&& *x-1>=0&& chess[*x-1][*y+2]==0)            {                *y+=2; *x-=1; return 1; }             Break;  Case 1:            if(*y+2<=y-1&& *x+1<=x-1&& chess[*x+1][*y+2]==0)            {                *y+=2; *x+=1; return 1; }             Break;  Case 2:            if(*y+1<=y-1&& *x+2<=x-1&& chess[*x+2][*y+1]==0)            {                *y+=1; *x+=2; return 1; }             Break;  Case 3:            if(*y-1>=0&& *x+2<=x-1&& chess[*x+2][*y-1]==0)            {                *y-=1; *x+=2; return 1; }             Break;  Case 4:            if(*y-2>=0&& *x+1<=x-1&& chess[*x+1][*y-2]==0)            {                *y-=2; *x+=1; return 1; }             Break;  Case 5:            if(*y-2>=0&& *x-1>=0&& chess[*x-1][*y-2]==0)            {                *y-=2; *x-=1; return 1; }             Break;  Case 6:            if(*y-1>=0&& *x-2>=0&& chess[*x-2][*y-1]==0)            {                *y-=1; *x-=2; return 1; }             Break;  Case 7:            if(*y+1<=y-1&& *x-2>=0&& chess[*x-2][*y+1]==0)            {                *y+=1; *x-=2; return 1; }             Break; default:             Break; }    return 0;}intHorseintXintYinttag) {    intx_t=x,y_t=y; intflag=0, count=0; Chess[x][y]=tag; if(tag==x*Y)        {printchess (); return 1; } Flag=next (&x_t,&y_t,count);  while(!flag && count<=7) {Count++; Flag=next (&x_t,&y_t,count); }     while(flag) {if(Horse (x_t,y_t,tag+1))        return 1; x_t=x,y_t=y,count++; Flag=next (&x_t,&y_t,count);  while(!flag && count<=7) {Count++; Flag=next (&x_t,&y_t,count); }    }    if(!flag) chess[x][y]=0; return 0;}intMain () {inti,j;  for(i=0; i<x;i++){         for(j=0; j<y;j++) {Chess[i][j]=0;    }} clock_t Begin,end; Begin=clock (); if(!horse (2,0,1) {printf ("The horse Chess is unavailable!"); } End=clock (); printf ("This time used is%lf\n",(Double) (End-begin)/clocks_per_sec); return 0;}

Run:

Soft-shelled turtle data structure and algorithm-horse stepping board (Knight travel problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.