离散数学第6版25页41题
本文章来自于博客园,其他地方出处可能是本人的日志同步
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define ROWS 17
5 #define COLS 17
6
7 char flag = 'a';
8 char array[ROWS][COLS];
9 int c_row, c_col;
10 int solution(int B_row, int B_col,
11 int row, int col,
12 int i_row, int i_col);
13
14 int main(int argc, char *argv[])
15 {
16 int row, col;
17
18 for(row = 1; row < ROWS; row++)
19 for(col = 1; col < COLS; col++)
20 array[row][col] = '0';
21
22 scanf("%d,%d", &c_row, &c_col);
23 array[c_row][c_col] = '*';
24
25 solution(1, 1, ROWS-1, COLS-1, c_row, c_col);
26
27 for(row = 1; row < ROWS; row++){
28 for(col = 1; col < COLS; col++){
29 printf("%5c", array[row][col]);
30 }
31 printf("\n");
32 }
33 return 0;
34 }
35
36 int solution(int B_row, int B_col,
37 int row, int col,
38 int i_row, int i_col)
39 {
40 if (B_row+1 == row && B_col+1 == col){
41 int i,j;
42 for(i = B_row; i <= row; i++ )
43 for(j = B_col; j <= col; j++ )
44 if(array[i][j] == '0'){
45 array[i][j] = flag;
46 }
47 }else{
48 if(i_row <= (B_row+row)/2){
49 array[(B_row+row)/2+1][(B_col+col)/2] = flag;
50 array[(B_row+row)/2+1][(B_col+col)/2+1] = flag;
51 if(i_col<=(B_col+col)/2){
52 array[(B_row+row)/2][(B_col+col)/2+1] = flag; /* 左上 */
53 flag++;
54 solution(B_row, B_col,
55 (B_row+row)/2, (B_col+col)/2,
56 c_row, c_col); /* 左上 */
57 flag++;
58 solution((B_row+row)/2+1, (B_col+col)/2+1,
59 row, col,
60 (B_row+row)/2+1, (B_col+col)/2+1); /* 右下 */
61 flag++;
62 solution(B_row, (B_col+col)/2+1,
63 (B_row+row)/2, col,
64 (B_row+row)/2, (B_col+col)/2+1); /* 右上 */
65 flag++;
66 solution((B_row+row)/2+1, B_col,
67 row, (B_col+col)/2,
68 (B_row+row)/2+1, col/2); /* 左下 */
69 }else{
70 array[(B_row+row)/2][(B_col+col)/2] = flag; /* 右上 */
71 flag++;
72 solution(B_row, (B_col+col)/2+1,
73 (B_row+row)/2, col,
74 c_row, c_col); /* 右上 */
75
76 flag++;
77 solution((B_row+row)/2+1, (B_col+col)/2+1,
78 row, col,
79 (B_row+row)/2+1, (B_col+col)/2+1); /* 右下 */
80 flag++;
81 solution((B_row+row)/2+1, B_col,
82 row, (B_col+col)/2,
83 (B_row+row)/2+1, col/2); /* 左下 */
84 flag++;
85 solution(B_row, B_col,
86 (B_row+row)/2, (B_col+col)/2,
87 row, col); /* 左上 */
88 }
89 }else{
90 array[(B_row+row)/2][(B_col+col)/2] = flag;
91 array[(B_row+row)/2][(B_col+col)/2+1] = flag;
92 if(i_col <= (B_col+col)/2){ /* 左下 */
93 array[(B_row+row)/2+1][(B_col+col)/2+1] = flag;
94 flag++;
95 solution((B_row+row)/2+1, B_col,
96 row, (B_col+col)/2,
97 c_row, c_col); /* 左下 */
98
99 flag++;
100 solution((B_row+row)/2+1, (B_col+col)/2+1,
101 row, col,
102 (B_row+row)/2+1, (B_col+col)/2+1); /* 右下 */
103 flag++;
104 solution(B_row, B_col,
105 (B_row+row)/2, (B_col+col)/2,
106 row, col); /* 左上 */
107 flag++;
108 solution(B_row, (B_col+col)/2+1,
109 (B_row+row)/2, col,
110 (B_row+row)/2, (B_col+col)/2+1); /* 右上 */
111 } else {
112 array[(B_row+row)/2+1][(B_col+col)/2] = flag; /* 右下 */
113 flag++;
114 solution((B_row+row)/2+1, (B_col+col)/2+1,
115 row, col,
116 c_row, c_col); /* 右下 注意:是原始的缺口 */
117
118 flag++;
119 solution(B_row, B_col,
120 (B_row+row)/2, (B_col+col)/2,
121 row, col); /* 左上 */
122 flag++;
123 solution(B_row, (B_col+col)/2+1,
124 (B_row+row)/2, col,
125 (B_row+row)/2, (B_col+col)/2+1); /* 右上 */
126 flag++;
127 solution((B_row+row)/2+1, B_col,
128 row, (B_col+col)/2,
129 (B_row+row)/2+1, col/2); /* 左下 */
130 }
131 }
132 }
133 return 0;
134 }