用递归处理 汉诺塔,迷宫和N皇后问题
汉诺塔
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int hanoi(int dishs,int peg1,int peg2,int peg3) 5 { 6 if(dishs == 1) 7 printf("盘子从 %d 移到 %d\n",peg1,peg3); 8 else 9 { 10 hanoi(dishs - 1,peg1,peg3,peg2); 11 printf("盘子从 %d 移到 %d\n",peg1,peg3); 12 hanoi(dishs - 1,peg2,peg1,peg3); 13 } 14 } 15 16 int main() 17 { 18 hanoi(3,1,2,3); 19 return 0; 20 }
$gcc -o main *.c
$main
盘子从 1 移到 3 盘子从 1 移到 2 盘子从 3 移到 2 盘子从 1 移到 3 盘子从 2 移到 1 盘子从 2 移到 3 盘子从 1 移到 3
迷宫
1 //数字0:表示是可走的路径 2 //数字1:表示是墙壁,不可走的路径 3 //数字2:表示是走过的路径 4 #include <stdio.h> 5 6 int maze[7][10] = { 7 1,1,1,1,1,1,1,1,1,1, 8 1,0,1,0,1,0,0,0,0,1, 9 1,0,1,0,1,0,1,1,0,1, 10 1,0,1,0,1,1,1,0,0,1, 11 1,0,1,0,0,0,0,0,1,1, 12 1,0,0,0,1,1,1,0,0,1, 13 1,1,1,1,1,1,1,1,1,1 14 }; 15 16 int find_path(int x,int y) 17 { 18 if(x == 1 && y == 1) 19 { 20 maze[x][y] = 2; 21 return 1; 22 } 23 else 24 if(maze[x][y] == 0) 25 { 26 maze[x][y] = 2; 27 if((find_path(x - 1,y) + 28 find_path(x + 1,y) + 29 find_path(x,y - 1) + 30 find_path(x,y + 1)) > 0) 31 return 1; 32 else 33 { 34 maze[x][y] = 0; 35 return 0; 36 } 37 } 38 else 39 return 0; 40 } 41 42 int main() 43 { 44 int i,j; 45 46 find_path(5,8); 47 printf("迷宫的路径如下图所示:\n"); 48 for(i = 1;i < 6;i++) 49 { 50 for(j = 1;j < 9;j++) 51 printf("%d",maze[i][j]); 52 printf("\n"); 53 } 54 return 0; 55 }
$gcc -o main *.c
$main
迷宫的路径如下图所示: 21010000 21010110 21011100 21222221 22211122
N皇后问题
1 //数字1:表示是放置皇后 2 //数字0:表示没有放置 3 4 #include <stdio.h> 5 #define MAXQUEEN 8 6 7 int pad[MAXQUEEN][MAXQUEEN] = { 8 0,0,0,0,0,0,0,0, 9 0,0,0,0,0,0,0,0, 10 0,0,0,0,0,0,0,0, 11 0,0,0,0,0,0,0,0, 12 0,0,0,0,0,0,0,0, 13 0,0,0,0,0,0,0,0, 14 0,0,0,0,0,0,0,0, 15 0,0,0,0,0,0,0,0 16 }; 17 18 int place(int x,int y); 19 20 int put_queen(int x,int y,int times) 21 { 22 int i,j,result = 0; 23 24 if(times > MAXQUEEN) 25 return 1; 26 else 27 if(place(x,y)) 28 { 29 pad[x][y] = 1; 30 for(i = 0; i < MAXQUEEN; i++) 31 for(j = 0; j < MAXQUEEN; j++) 32 { 33 result += put_queen(i,j,times+1); 34 if(result > 0) 35 break; 36 } 37 if(result > 0) 38 return 1; 39 else 40 { 41 pad[x][y] = 0; 42 return 0; 43 } 44 } 45 else 46 return 0; 47 } 48 49 int place(int x,int y) 50 { 51 int x1,y1; 52 53 if(pad[x][y] != 0) 54 return 0; 55 x1 = x - 1; 56 y1 = y - 1; 57 while(x1 >= 0 && y1 >= 0) 58 if(pad[x1--][y1--] != 0) 59 return 0; 60 x1 = x + 1; 61 y1 = y + 1; 62 while(x1 < MAXQUEEN && y1 < MAXQUEEN) 63 if(pad[x1++][y1++] != 0) 64 return 0; 65 x1 = x + 1; 66 y1 = y - 1; 67 while(x1 < MAXQUEEN && y1 >= 0) 68 if(pad[x1++][y1--] != 0) 69 return 0; 70 x1 = x - 1; 71 y1 = y + 1; 72 while(x1 >= 0 && y1 < MAXQUEEN) 73 if(pad[x1--][y1++] != 0) 74 return 0; 75 x1 = x; 76 y1 = y + 1; 77 while(y1 < MAXQUEEN) 78 if(pad[x1][y1++] != 0) 79 return 0; 80 x1 = x; 81 y1 = y - 1; 82 while(y1 >= 0) 83 if(pad[x1][y1--] != 0) 84 return 0; 85 x1 = x + 1; 86 y1 = y; 87 while(x1 < MAXQUEEN) 88 if(pad[x1++][y1] != 0) 89 return 0; 90 x1 = x - 1; 91 y1 = y; 92 while(x1 >= 0) 93 if(pad[x1--][y1] != 0) 94 return 0; 95 return 1; 96 } 97 98 int main() 99 { 100 int i,j; 101 102 put_queen(0,0,1); 103 printf("放置八皇后的棋盘图形:\n"); 104 for(i = 0; i < MAXQUEEN; i++) 105 { 106 for( j = 0; j < MAXQUEEN; j++) 107 printf("%d ",pad[i][j]); 108 printf("\n"); 109 } 110 return 0; 111 }
$gcc -o main *.c
$main
放置八皇后的棋盘图形: 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0