我修改了一下无心人的代码,添加了一个next2的数组,这个数组用来查找跳跃关系,
例如next2[1] = 3,2,3,5,9,4,7,0,0,0,
表示对于1,有3种跳跃可能,当2被mask了,就可以跳到3,当5被mask了,就可以跳到9,当4被mask了,就可以跳到7.
代码如下:-
- #include <stdio.h>
-
- int mask[10] = {1,0,0,0,0,0,0,0,0,0};
- int next[10][10] =
- {0,0,0,0,0,0,0,0,0,0,
- 5,2,4,5,6,8,0,0,0,0,
- 7,1,3,4,5,6,7,9,0,0,
- 5,2,4,5,6,8,0,0,0,0,
- 7,1,2,3,5,7,8,9,0,0,
- 8,1,2,3,4,6,7,8,9,0,
- 7,1,2,3,5,7,8,9,0,0,
- 5,2,4,5,6,8,0,0,0,0,
- 7,1,3,4,5,6,7,9,0,0,
- 5,2,4,5,6,8,0,0,0,0,
- };
- int next2[10][10] = {
- 0,0,0,0,0,0,0,0,0,0,
- 3,2,3,5,9,4,7,0,0,0,
- 1,5,8,0,0,0,0,0,0,0,
- 3,2,1,5,7,6,9,0,0,0,
- 1,5,6,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,
- 1,5,4,0,0,0,0,0,0,0,
- 3,4,1,5,3,8,9,0,0,0,
- 1,5,2,0,0,0,0,0,0,0,
- 3,6,3,5,1,8,7,0,0,0,
- };
-
-
- int counter[10] = {0,0,0,0,0,0,0,0,0,0};
- int graph[10] = {0,0,0,0,0,0,0,0,0,0};
- int nextNumber = 0;
- int graphPostion = 0;
-
- void circle( int graphPostion )
- {
- int i, k;
- int noPostion = 1;
- int n = graph[graphPostion - 1];
- for (i = 1; i <= next[n][0]; i ++)
- {
- if (mask[next[n][i]] == 0)
- {
- noPostion = 0;
- mask[next[n][i]] = 1;
- graph[graphPostion] = next[n][i];
- counter[graphPostion + 1] ++;
- printf( "find%d: ", graphPostion + 1 );
- for( int z = 0; z < graphPostion + 1; ++z ) printf( "%d ", graph[z] );
- puts( "" );
- circle( graphPostion + 1 );
- mask[next[n][i]] = 0;
- }
- }
-
- for (i = 1, k = 1; i <= next2[n][0]; i ++, k += 2) {
- if (mask[next2[n][k]] == 1 && mask[next2[n][k+1]] == 0 ) {
- mask[next2[n][k+1]] = 1;
- graph[graphPostion] = next2[n][k+1];
- counter[graphPostion + 1] ++;
- printf( "2find%d: ", graphPostion + 1 );
- for( int z = 0; z < graphPostion + 1; ++z ) printf( "%d ", graph[z] );
- puts( "" );
- circle( graphPostion + 1 );
- mask[next2[n][k+1]] = 0;
- }
- }
-
- }
-
- void first( void )
- {
- int i;
- for (i = 1; i <= 9 ; i ++)
- {
- mask[i] = 1;
- graph[graphPostion] = i;
- counter[graphPostion+1] ++;
- circle( graphPostion + 1 );
- mask[i] = 0;
- }
- }
-
- int main ( void )
- {
- int i;
- first( );
- for (i = 1; i <= 9; i ++)
- printf("%d: %d\n", i, counter[i]);
- return 0;
- }
复制代码 最后运行结果:
1: 9
2: 56
3: 320
4: 1624
5: 7152
6: 26016
7: 72912
8: 140704
9: 140704
和你最开始给的答案一致。 |