共有回帖数  0  个 
	 
	
	
	
     
          
          
               
                  - 我用codeblock编译网上一位大大的马踏棋盘,结果死机了,求解··
- 
													  只看楼主					  
                     
                       收藏
                      
                          
                           回复
                      
					  
					                     
					 
 
            
				   - 
						
						
							 
									#includestdio.h
 #includestring.h
 #includestdlib.h
 #define M 8
 //定义棋盘大小为8
 #define SIZE 100
 int board[M][M];
 typedef struct sirect
 {
 int r,c,pathnum;
 }
 dir;
 typedef struct nodetype
 {
 int r,c,pathnum;
 struct nodetype*next,*prior;
 }
 node;
 int pathnum(int row,int cn)
 {
 int a,b,count=0;
 a=row;
 b=cn;
 //
 if (a-2=0&&b-1=0&&board[a-2][b-1]==0)
 count++;
 if (a-2=0&&b+1=0&&board[a-2][b+1]==0)
 count++;
 if (a+2=0&&b-1=0&&board[a-2][b-1]==0)
 count++;
 if (a+2=0&&b+1=0&&board[a-2][b+1]==0)
 count++;
 if (a-1=0&&b+2=0&&board[a-2][b+2]==0)
 count++;
 if (a-1=0&&b-2=0&&board[a-2][b-2]==0)
 count++;
 if (a+1=0&&b+2=0&&board[a-2][b+2]==0)
 count++;
 if (a+1=0&&b-2=0&&board[a-2][b-2]==0)
 count++;
 return count;
 }
 //
 void findway(int m,int n)
 {
 dir f[8],path;
 int i,j,k,stepnum;
 stepnum=1;
 i=m;
 j=n;
 while (stepnumM*M&&iM&&jM&&i=0&&j=0)
 {
 board[j]=stepnum;
 printf("%d,%d,%d",i,j,board[j]);
 
 
 //
 path.pathnum=8;
 //
 for(k=0;k8;k++)
 {
 f[k].r=-1;
 f[k].c=-1;
 }
 //
 if(i-2=0&&j-1=0)
 {
 f[0].r=i-2;
 f[0].c=j-1;
 f[0].pathnum=pathnum(f[0].r,f[0].c);
 }
 if(i-2=0&&j+1M)
 {
 f[1].r=i-2;
 f[1].c=j+1;
 f[1].pathnum=pathnum(f[1].r,f[1].c);
 }
 if(i+2M&&j-1=0)
 {
 f[2].r=i+2;
 f[2].c=j-1;
 f[2].pathnum=pathnum(f[2].r,f[2].c);
 }
 if(i+2M&&j+1M)
 {
 f[3].r=i+2;
 f[3].c=j+1;
 f[3].pathnum=pathnum(f[3].r,f[3].c);
 }
 if(i-1=0&&j+2M)
 {
 f[4].r=i-1;
 f[4].c=j+2;
 f[4].pathnum=pathnum(f[4].r,f[4].c);
 }
 if(i-1=0&&j-2=0)
 {
 f[5].r=i-1;
 f[5].c=j-2;
 f[5].pathnum=pathnum(f[5].r,f[5].c);
 }
 if(i+1M&&j-2=0)
 {
 f[6].r=i+1;
 f[6].c=j-2;
 f[6].pathnum=pathnum(f[6].r,f[6].c);
 }
 if(i+1M&&j+2M)
 {
 f[7].r=i+1;
 f[7].c=j+2;
 f[7].pathnum=pathnum(f[7].r,f[7].c);
 }
 //
 for(k=0;k8;k++)
 if(f[k].r=0&&f[k].c=0&&f[k].pathnum=path.pathnum&&board[f[k].r][f[k].c]==0
 &&f[k].pathnum0)
 {
 path.pathnum=f[k].pathnum;
 path.r=f[k].r;
 path.c=f[k].c;
 }
 i=path.r;
 j=path.c;
 stepnum++;
 }
 board[j]=M*M-1;
 
 
 if(i-2=0&&j-1=0&&board[i-2][j-1]==0)
 {
 i=i-2;
 j=j-1;
 }
 else
 if(i-2=0&&j-1M&&board[i-2][j+1]==0)
 {
 i=i-2;
 j=j+1;
 }
 else
 if(i+1M&&j+2M&&board[i+1][j+2]==0)
 {
 i=i+1;
 j=j+2;
 }
 else //```````````````````````i和j```````````````````````````````````````````````````````````````````````````````````
 if(i+2M&&j-1=0&&board[i+2][j-1]==0)
 {
 i=i+2;
 j=j-1;
 }
 else
 if(i+1M&&j-2=0&&board[i+1][j-2]==0)
 {
 i=i+1;
 j=j-2;
 }
 else
 if(i-1=0&&j-2=0&&board[i-1][j-2]==0)
 {
 i=i-1;
 j=j-2;
 }
 board[j]=M*M;
 printf("%d,%d,%d",i,j,board[j]);
 }
 void main()
 {
 int r,c,i,j;
 printf("请输入马在棋盘的坐标(8*8)n");
 scanf("%d,%d",&r,&c);
 findway(r,c);
 printf("n");
 //
 for(i=0;iM;i++)
 {
 for(j=0;jM;i++)
 printf("%3d",board[j]);
 printf("n");
 
 
 }
 }
 楼主 2015-07-02 11:27 回复 
 
 
   
             
                  
                  
 
 
 
     
	 
  
	Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
	
	意见反馈 | 
	关于直线 | 
	版权声明 | 
	会员须知