#include
void
main()
{
int
i,j;
int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:
\\tc");
setbkcolor(WHITE);
setcolor(RED);
for(i=0;i<10;i++)
{
line(20,20+i*40,340,20+i*40);
}
for(i=0;i<9;i++)
{
line(20+i*40,20,20+i*40,180);
}
for(i=0;i<9;i++)
{
line(20+i*40,220,20+i*40,380);
}
setlinestyle(0,0,3);
line(20,20,340,20);
line(340,20,340,380);
line(340,380,20,380);
line(20,380,20,20);
line(140,20,220,100);
line(220,20,140,100);
line(140,380,220,300);
line(220,380,140,300);
circle(60,100,5);
circle(300,100,5);
circle(60,300,5);
circle(300,300,5);
setcolor(BLUE);
moveto(60,380);
lineto(100,300);
lineto(140,380);
lineto(180,300);
lineto(220,380);
lineto(260,300);
lineto(300,380);
lineto(340,300);
lineto(300,220);
lineto(260,300);
lineto(220,220);
lineto(180,300);
lineto(140,220);
lineto(100,300);
lineto(60,220);
lineto(100,140);
lineto(140,220);
lineto(180,140);
lineto(220,220);
lineto(260,140);
lineto(300,220);
lineto(340,140);
lineto(260,100);
lineto(340,60);
lineto(260,20);
lineto(180,60);
lineto(260,100);
lineto(180,140);
lineto(100,100);
lineto(180,60);
lineto(100,20);
lineto(20,60);
lineto(100,100);
lineto(20,140);
lineto(60,220);
lineto(20,300);
lineto(60,380);
getch();
}
咱是校友啊
给你试试吧
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define M 5
#define N 5
static int chessboard[M][N];
void jump(int i,int j, int prei,int prej) {
if(i < 0 || i >= M || j < 0 || j >= N)return;
if(chessboard[prei][prej] + 1 > chessboard[i][j] && chessboard[i][j] > 0)return;
chessboard[i][j] = chessboard[prei][prej] + 1;
jump(i+1, j+2, i, j);
jump(i+1, j-2, i, j);
jump(i-1, j+2, i, j);
jump(i-1, j-2, i, j);
jump(i+2, j+1, i, j);
jump(i+2, j-1, i, j);
jump(i-2, j+1, i, j);
jump(i-2, j-1, i, j);
}
int main() {
int starti, startj;
printf("enter i,j:");
scanf("%d %d", &starti, &startj);
if(starti < 0 || starti >= M || startj < 0 || startj >= N) {
printf("enter error\n");
getchar();
return 0;
}
fflush(stdin);
jump(starti ,startj ,starti ,startj);
for(int i=0; i
}
printf("\n");
}
printf("\n");
getchar();
return 0;
}
/*
这是一次走遍棋盘的的程序。如果找到路径就立即跳出。
用递归,通过改变栈中的数据寻找路径
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "setjmp.h"
#define M 10
#define N 9
jmp_buf my_jmp;
struct step_t {
int i;
int j;
int val;
};
struct step_t steps[M*N];
void jump(int i, int j, int step) {
if(i < 0 || i >= M || j < 0 || j >= N)return;
for(int k=0; k
}
steps[step].i = i;
steps[step].j = j;
steps[step].val = step+1;
if(step == M*N-1) {
printf("done\n");
longjmp(my_jmp, 1);
}
jump(i+1, j+2, step+1);
jump(i+1, j-2, step+1);
jump(i-1, j+2, step+1);
jump(i-1, j-2, step+1);
jump(i+2, j+1, step+1);
jump(i+2, j-1, step+1);
jump(i-2, j+1, step+1);
jump(i-2, j-1, step+1);
}
int main() {
int result = setjmp(my_jmp);
if(result == 0) {
jump(2, 2, 0);
} else if(result == 1) {
printf("\nresult...\n");
for(int k=0; k
}
} else {
printf("no result exit\n");
}
getchar();
return 0;
}
这个我劝你用Java写。C的确能写出来,但是运行的时间太长了,半个小时也出不来结果。用Java可以用多线程同步运算,这样会快一点。
这个的C程序我是写出来的,但是运行太慢了,因为要尝试超过1万亿种路线,普通的电脑估计一个月也算不完。如果你知道有人会用C的多线程我可以把它发给你,你让人把它改成多线程运行就行了。
哥们 你是沈工的吧?
#include
void main()
{
int i,j;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c: \\tc");
setbkcolor(WHITE);
setcolor(RED);
for(i=0;i<10;i++)
{
line(20,20+i*40,340,20+i*40);
}
for(i=0;i<9;i++)
{
line(20+i*40,20,20+i*40,180);
}
for(i=0;i<9;i++)
{
line(20+i*40,220,20+i*40,380);
}
setlinestyle(0,0,3);
line(20,20,340,20);
line(340,20,340,380);
line(340,380,20,380);
line(20,380,20,20);
line(140,20,220,100);
line(220,20,140,100);
line(140,380,220,300);
line(220,380,140,300);
circle(60,100,5);
circle(300,100,5);
circle(60,300,5);
circle(300,300,5);
setcolor(BLUE);
moveto(60,380);
lineto(100,300);
lineto(140,380);
lineto(180,300);
lineto(220,380);
lineto(260,300);
lineto(300,380);
lineto(340,300);
lineto(300,220);
lineto(260,300);
lineto(220,220);
lineto(180,300);
lineto(140,220);
lineto(100,300);
lineto(60,220);
lineto(100,140);
lineto(140,220);
lineto(180,140);
lineto(220,220);
lineto(260,140);
lineto(300,220);
lineto(340,140);
lineto(260,100);
lineto(340,60);
lineto(260,20);
lineto(180,60);
lineto(260,100);
lineto(180,140);
lineto(100,100);
lineto(180,60);
lineto(100,20);
lineto(20,60);
lineto(100,100);
lineto(20,140);
lineto(60,220);
lineto(20,300);
lineto(60,380);
getch();
}