c语音实现马踏棋盘,非递归程序
有没有大佬发一下代码 踏一步的我会写踏两步 就可能有64种变化踏三步就有可能有512种变化踏四步就有可能有4096种变化
太复杂了 #include<stdio.h>
int chess={0};
int cnt = 0;
int countChess = 0;
int move={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
int initChess();
void printChess();
void horse(int x, int y);
struct Node{
int x;
int y;
int i;
}chessStep;
int top = -1;
int main(void){
initChess();
int i,j;
for(i=2; i<10; i++){
for(j=2; j<10; j++){
cnt = 1;
chess = cnt;
horse(i,j);
}
}
}
void horse(int x, int y){
int i = 0;
while(cnt < 64){
int row;
int col;
for(i; i<8; i++){
row = x + move;
col = y + move;
if(chess == 0){
break;
}
}
if(i != 8){
chess = ++cnt;
chessStep[++top].x = x;
chessStep.y = y;
chessStep.i = i;
if(cnt == 64){
printChess();
chess = 0;
i = chessStep.i + 1;
x = chessStep.x;
y = chessStep.y;
top--;
cnt--;
}else{
x = row;
y = col;
i = 0;
}
}else{
chess = 0;
i = chessStep.i + 1;
x = chessStep.x;
y = chessStep.y;
top--;
cnt--;
}
if(top == -1){
break;
}
}
}
int initChess(){
int i,j;
for(i=0; i<12; ++i){
for(j=0; j<12; ++j){
if(i<2 || i>9 || j<2 || j>9){
chess=-1;
}
}
}
}
void printChess(){
++countChess;
printf("第%d个解是:\n", countChess);
int i,j;
for(i=2; i<10; ++i){
for(j=2; j<10; ++j){
printf("%2d ",chess);
}
printf("\n");
}
}
页:
[1]