作业帮 > 综合 > 作业

C语言 16*16棋盘 马数小于4的马拦过河卒问题

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/07/15 02:00:57
C语言 16*16棋盘 马数小于4的马拦过河卒问题
#include
struct node{
int right;
int down;
int x,y;
}stack[40];
struct horse{
int x,y;
}horse[8];
int test(int x,int y,struct horse *h,int k){
int i;
for(i = 0; i < k; i++)
if(h[i].x == x && h[i].y == y)
return 1;
return 0;
}
int main(){
int xd,yd,xh,yh,i,k,m;
int b[] = {-2,-1,1,2,2,1,-1,-2};
int c[] = {1,2,2,1,-1,-2,-2,-1};
while(scanf("%d%d%d%d",&xd,&yd,&xh,&yh)!=EOF){
for(k = i = 0; i < 8; i++){
if(xh+c[i]xd || yh+b[i]yd)
continue;
horse[k].x = xh + c[i];
horse[k].y = yh + b[i];
k++;
}
stack[0].x=stack[0].y=stack[0].down=stack[0].right=0;
for(m = i = 0; i >= 0;){
if(stack[i].x == xd && stack[i].y == yd){
i--;
m++;
}
if(stack[i].down == 0){
stack[i+1].y = stack[i].y + 1;
stack[i+1].x = stack[i].x;
stack[i].down = 1;
i++;
stack[i].down = stack[i].right = 0;
}
else if(stack[i].right == 0){
stack[i+1].x = stack[i].x + 1;
stack[i+1].y = stack[i].y;
stack[i].right = 1;
i++;
stack[i].down = stack[i].right = 0;
}
else{
i--;
continue;
}
if( stack[i].x > xd || stack[i].y > yd ||
test(stack[i].x,stack[i].y,horse,k) ||
(stack[i].x == xh && stack[i].y == yh) ){
i--;
continue;
}
}
printf("%d\n",m);
}
return 0;
}