接上面中国象棋代码:
if( check_turn == 12) //相的走法规范
{ if((x == check_x &&y == check_y))
{temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500)
}
else if( x >= 15 &&(abs(y - check_y) == 8 &&abs(x - check_x) == 4))
{if((x == 22 &&(y == 11 || y == 27))||(x == 18 &&
( y == 3 || y == 19 || y == 35)) ||(x == 14 &&(y == 11|| y ==27)))
{ if( map[(x+check_x)/2][(y+check_y)/2] == '+') check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else {printf("棋子卡住,不可执行")Sleep(500)} }
else {printf("不合法的下法\n")Sleep(500)}
}
else {printf("不合法的下法\n")Sleep(500)}
}
if( check_turn == 13) //士的走法规范
{ if((x == check_x &&y == check_y))
{temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( abs(x - check_x)== 2 &&abs( y - check_y) == 4 &&((x==22 &&(y == 15
|| y == 23)) || ( x == 20 &&y == 19) || ( x == 18 &&( y == 15 || y == 23)))) {check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}
else { printf("不合法的下法\n")Sleep(500)} }
if( check_turn == 14) //将的走法规范
{ if((x == check_x &&y == check_y))
{ temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( ((abs(x - check_x)== 2 &&abs( y - check_y) == 0 )|| (abs(x - check_x)== 0
&&abs( y - check_y) == 4)) &&x >= 18 &&x <= 22 &&y >= 15 &&y <= 23 )
{ check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}
else { printf("不合法的下法\n")Sleep(500)} }
if( check_turn == 15) //炮的走法规范
{ if((x == check_x &&y == check_y))
{ temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( y == check_y )
{ int check_pao = 0
if( x >check_x)
{ for(j = check_x + 2j<= x j = j+ 2)
{ if(map[j][y] == '+' ) else check_pao++}
if(check_pao == 1&&temp == '+') // 直线行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2 &&temp != '+') //跳跃吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
else { for(j = check_x - 2j>= xj = j - 2)
{ if(map[j][y] == '+' ) else { check_pao++} }
if(check_pao == 1&&temp == '+') //直线行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2 &&temp != '+') //跳跃吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
}
else if( x == check_x )
{ int check_pao = 0
if( y >check_y)
{ for(j = check_y + 4j<= y j = j+4)
{ if(map[x][j] == '+' ) else check_pao++}
if(check_pao == 1&&temp == '+') //直线行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2 &&temp != '+') //跳跃吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
else {for(j = check_y - 4j>= yj = j - 4)
{if(map[x][j] == '+' ) else check_pao++}
if(check_pao == 1&&temp == '+') //直线行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2 &&temp != '+') //跳跃吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
}
else { printf("不合法的下法\n")Sleep(500)}
}
if( check_turn == 16) //卒的走法规范
{ if ( x >= 14)
{ if((x == check_x &&y == check_y))
{ temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合") Sleep(500) }
else if( x == check_x - 2 &&y == check_y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)}
}
else{ if((x == check_x &&y == check_y))
{ temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if((x - check_x == 0 &&abs(y-check_y) ==4) ||( x - check_x == -2
&&abs(y-check_y) == 0)) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
}
}
else { if( check_turn == 20) //车的走法规范 (帅方)
{ if((x == check_x &&y == check_y))
{ temp = turn temp1 = turn1turn = 'O'turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( y == check_y )
{ if( x >check_x)
{ for(j = check_x + 2j <xj = j + 2)
{ if(map[j][y] == '+')else {printf("不合法的下法\n")Sleep(500)break} }
if( j >= x) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
}
if( x <check_x)
{ for(j = check_x - 2j >xj = j - 2)
{ if(map[j][y] == '+')else { printf("不合法的下法\n")Sleep(500)break} }
if( j <= x) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
}
}
else if( x == check_x )
{ if( y >check_y)
{ for(j = check_y + 4j <yj = j + 4)
{ if(map[x][j] == '+')else { printf("不合法的下法\n")Sleep(500)break} }
if( j >= y) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
}
if( y <check_y)
{ for(j = check_y - 4j >yj = j - 4)
{ if(map[x][j] == '+')else { printf("不合法的下法\n")Sleep(500)break} }
if( j <= y) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
}
}
else { printf("不合法的下法\n")Sleep(500)}
}
if( check_turn == 21) //马的走法规范
{ if((x == check_x &&y == check_y))
{ temp = turntemp1 = turn1turn = 'O'turn1 = 'N'num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500)}
else if( (abs( x - check_x) == 2&&abs( y - check_y) == 8)&&
map[check_x][(y+check_y)/2] =='+')
{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}
else if( (abs( x - check_x) == 4&&abs( y - check_y) == 4)&&
map[(x + check_x)/2][check_y] == '+' )
{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}
else { printf("不合法的下法\n")Sleep(500)} }
if( check_turn == 22) //相的走法规范
{ if((x == check_x &&y == check_y))
{ temp = turntemp1 = turn1turn = 'O'turn1 = 'N'num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500)}
else if( x <= 12 &&(abs(y - check_y) == 8 &&abs(x - check_x) == 4))
{ if((x == 4 &&(y == 11 || y == 27))||(x == 8 &&( y == 3 || y == 19 || y == 35))
||(x == 12 &&(y == 11|| y ==27)))
{ if( map[(x+check_x)/2][(y+check_y)/2] == '+') check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("棋子卡住,不可执行")Sleep(500)} }
else {printf("不合法的下法\n")Sleep(500)}
}
else { printf("不合法的下法\n")Sleep(500)} }
if( check_turn == 23) //士的走法规范
{ if((x == check_x &&y == check_y))
{ temp = turntemp1 = turn1turn = 'O'turn1 = 'N'num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( abs(x - check_x)== 2 &&abs( y - check_y) == 4 &&((x==4 &&
(y == 15 || y == 23)) || ( x == 6 &&y == 19) || ( x == 8 &&( y == 15 || y == 23))))
{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}
else { printf("不合法的下法\n")Sleep(500)} }
if( check_turn == 24) //将的走法规范
{ if((x == check_x &&y == check_y))
{ temp = turntemp1 = turn1turn = 'O'turn1 = 'N'num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( ((abs(x - check_x)== 2 &&abs( y - check_y) == 0 )|| (abs(x - check_x)== 0 &&abs( y - check_y) == 4)) &&x >= 4 &&x <= 8 &&y >= 15 &&y <= 23 )
{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}
else {printf("不合法的下法\n")Sleep(500)} }
if( check_turn == 25) //炮的走法规范
{ if((x == check_x &&y == check_y))
{ temp = turn temp1 = turn1turn = 'O'turn1 = 'N'num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( y == check_y )
{ int check_pao = 0
if( x >check_x)
{ for(j = check_x + 2j<= x j = j+ 2)
{ if(map[j][y] == '+' ) else check_pao++}
if(check_pao == 1&&temp == '+') //直线行走但不可吃棋子 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2 &&temp != '+') //跳跃吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
else { for(j = check_x - 2j>= xj = j - 2)
{ if(map[j][y] == '+' ) else { check_pao++} }
if(check_pao == 1&&temp== '+') //直线行走但不可吃棋子 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2 &&temp != '+') //跳跃吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
}
else if( x == check_x )
{ int check_pao = 0
if( y >check_y)
{ for(j = check_y + 4j<= y j = j+4)
{ if(map[x][j] == '+' ) else check_pao++}
if(check_pao == 1&&temp == '+') //直线行走但不可吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2 &&temp != '+') //跳跃吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
else { for(j = check_y - 4 j>= yj = j - 4)
{ if(map[x][j] == '+' ) else check_pao++}
if(check_pao ==1&&temp == '+') //直线行走但不可吃棋子 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else if( check_pao == 2&&temp != '+') //跳跃吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
}
else { printf("不合法的下法\n")Sleep(500)}
}
if( check_turn == 26) //卒的走法规范
{ if( x <= 12)
{ if((x == check_x &&y == check_y))
{ temp = turntemp1 = turn1turn = 'O'turn1 = 'N'num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500)}
else if( x == check_x + 2 &&y == check_y) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
else{ if((x == check_x &&y == check_y))
{ temp = turntemp1 = turn1turn = 'O'turn1 = 'N'num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if((x - check_x == 0 &&abs(y-check_y) ==4) ||( x - check_x == 2
&&abs(y-check_y) == 0)) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map)
else { printf("不合法的下法\n")Sleep(500)} }
}
}
}
}
system("cls")
if( if_return) return
for(i = 0i <27i++)puts(map[i])
}
Sleep(5000)
}
int main( )
{ while(1)
{ xiangqi( )
printf("\n 重来,请按键.\n")
getch( )
}
return 0
}
#include<stdio.h>/*
问题描述:在n*m的棋盘上,马只能走日字。马从(x,y)出发,把棋盘的每一个点都走一遍,且只走一次,
找出所有路径。
*/
void find(int x,int y,int dep)
int check(int x,int y)
void output()
int n=5,m=4
int fx[8]={1,2,2,1,-1,-2,-2,-1}
int fy[8]={2,1,-1,-2,-2,-1,1,2}
int a[5][4]
int dep,x,y,count
int main()
{
int i,j
dep=1////记录递归深度,即走过的点的个数,当dep=n*m,找到一组解
count=0
printf("Please input the original point:\n")
scanf("%d%d",&x,&y)
if(x<0 || x>4 || y<0 || y>3)
{
printf("wrong data!\n")
return 1
}
for(i=0i<ni++)
{
for(j=0j<mj++)
{
a[i][j]=0
}
}
a[x][y]=1
find(x,y,2)
if(count==0)
{
printf("NO solution!\n")
}
else
{
printf("Total count=%d\n",count)
}
return 0
}
void find(int x,int y,int dep)
{
int i
int xx,yy
for(i=0i<8i++)//加上方向增量,形成新的坐标
{
xx=x+fx[i]
yy=y+fy[i]
if(check(xx,yy)==1)//判断坐标是否出界,或者已经走过
{
a[xx][yy]=dep//走向新的坐标
if(dep==n*m)
{
output()
}
else
{
find(xx,yy,dep+1)//从新的坐标出发,递归下一层
}
a[xx][yy]=0//回溯,恢复未走标志
}
}
}
int check(int x,int y)
{
int flag = 1
if(x<0 || x>4 || y<0 || y>3)
{
flag = 0
}
if(a[x][y]!=0)
{
flag=0
}
return flag
}
void output()
{
count++
printf("\ncount=%d\n",count)
int i,j
for(i=0i<ni++)
{
for(j=0j<mj++)
{
printf("%4d",a[i][j])
}
printf("\n")
}
}
//原始文件请参考:CSDN马的遍历问题
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)