質問編集履歴

2

簡潔に

2017/06/29 16:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -17,241 +17,3 @@
17
17
 
18
18
 
19
19
  長々すみません…ぜひご回答お願いします!
20
-
21
-
22
-
23
- 以下は、8パズルのプログラミングです。参考になるかわかりませんが、一応のせておきます。```
24
-
25
- #include <stdio.h>
26
-
27
- #include <string.h>
28
-
29
- #include <time.h>
30
-
31
-
32
-
33
- #define TRUE 1
34
-
35
- #define FALSE 0
36
-
37
- #define GOAL 2
38
-
39
- #define SIZE 9
40
-
41
-
42
-
43
- #define MAX_STATE 181440
44
-
45
-
46
-
47
-
48
-
49
- const char adjacent[SIZE][5] = {
50
-
51
- 1, 3,-1,-1,-1,
52
-
53
- 0, 4, 2,-1,-1,
54
-
55
- 1, 5,-1,-1,-1,
56
-
57
- 0, 4, 6,-1,-1,
58
-
59
- 1, 3, 5, 7,-1,
60
-
61
- 2, 4, 8,-1,-1,
62
-
63
- 3, 7,-1,-1,-1,
64
-
65
- 4, 6, 8,-1,-1,
66
-
67
- 5, 7,-1,-1,-1,
68
-
69
- };
70
-
71
-
72
-
73
- char state[MAX_STATE + 1][SIZE];
74
-
75
- char space_postion[MAX_STATE];
76
-
77
- int prev_state[MAX_STATE];
78
-
79
-
80
-
81
- char check_table[MAX_STATE * 2];
82
-
83
-
84
-
85
- char init_state[SIZE] = {
86
-
87
- 8, 6, 7, 2, 5, 4, 3, 0, 1
88
-
89
- };
90
-
91
-
92
-
93
- char final_state[SIZE] = {
94
-
95
- 1, 2, 3, 4, 5, 6, 7, 8, 0
96
-
97
- };
98
-
99
-
100
-
101
- void print_answer( int n )
102
-
103
- {
104
-
105
- int i, j;
106
-
107
- if( n != 0 ) print_answer( prev_state[n] );
108
-
109
- for( i = 0; i < 3; i++ ){
110
-
111
- for( j = 0; j < 3; j++ ){
112
-
113
- printf("%d ", state[n][i * 3 + j] );
114
-
115
- }
116
-
117
- printf("\n");
118
-
119
- }
120
-
121
- printf("\n");
122
-
123
- }
124
-
125
-
126
-
127
- int change_number( char *board )
128
-
129
- {
130
-
131
- char work[SIZE];
132
-
133
- static int fact_table[SIZE] = {
134
-
135
- 40320, 5040, 720, 120, 24, 6, 2, 1, 0,
136
-
137
- };
138
-
139
- int j, k, value = 0;
140
-
141
- memcpy( work, board, SIZE );
142
-
143
- for( j = 0; j < SIZE - 1; j++ ){
144
-
145
- value += fact_table[j] * work[j];
146
-
147
- for( k = j + 1; k < SIZE; k++ ){
148
-
149
- if( work[j] < work[k] ) work[k]--;
150
-
151
- }
152
-
153
- }
154
-
155
- return value;
156
-
157
- }
158
-
159
-
160
-
161
- void search( void )
162
-
163
- {
164
-
165
- int front = 0, rear = 1;
166
-
167
- memcpy( state[0], init_state, SIZE );
168
-
169
- space_postion[0] = 7;
170
-
171
- prev_state[0] = -1;
172
-
173
- check_table[ change_number( init_state ) ] = TRUE;
174
-
175
- check_table[ change_number( final_state ) ] = GOAL;
176
-
177
-
178
-
179
- while( front < rear ){
180
-
181
- int s = space_postion[front];
182
-
183
- int i, k, n;
184
-
185
- for( i = 0; (n = adjacent[s][i]) != -1; i++ ){
186
-
187
-
188
-
189
- memcpy( state[rear], state[front], SIZE );
190
-
191
-
192
-
193
- state[rear][s] = state[rear][n];
194
-
195
- state[rear][n] = 0;
196
-
197
- space_postion[rear] = n;
198
-
199
- prev_state[rear] = front;
200
-
201
- k = change_number( state[rear] );
202
-
203
- if( check_table[k] == GOAL ){
204
-
205
-
206
-
207
- print_answer( rear );
208
-
209
- printf("状態数 %d 個\n", rear );
210
-
211
- return;
212
-
213
- } else if( !check_table[k] ){
214
-
215
-
216
-
217
- check_table[k] = TRUE;
218
-
219
- rear++;
220
-
221
- }
222
-
223
- }
224
-
225
- front++;
226
-
227
- }
228
-
229
- }
230
-
231
-
232
-
233
- int main()
234
-
235
- {
236
-
237
- int start, end;
238
-
239
-
240
-
241
- memset( check_table, 0 , MAX_STATE * 2 );
242
-
243
- start = clock();
244
-
245
- search();
246
-
247
- end = clock();
248
-
249
- printf("時間 %d \n", end - start );
250
-
251
- return 0;
252
-
253
- }
254
-
255
-
256
-
257
- ```

1

詳しくしました

2017/06/29 16:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,241 @@
17
17
 
18
18
 
19
19
  長々すみません…ぜひご回答お願いします!
20
+
21
+
22
+
23
+ 以下は、8パズルのプログラミングです。参考になるかわかりませんが、一応のせておきます。```
24
+
25
+ #include <stdio.h>
26
+
27
+ #include <string.h>
28
+
29
+ #include <time.h>
30
+
31
+
32
+
33
+ #define TRUE 1
34
+
35
+ #define FALSE 0
36
+
37
+ #define GOAL 2
38
+
39
+ #define SIZE 9
40
+
41
+
42
+
43
+ #define MAX_STATE 181440
44
+
45
+
46
+
47
+
48
+
49
+ const char adjacent[SIZE][5] = {
50
+
51
+ 1, 3,-1,-1,-1,
52
+
53
+ 0, 4, 2,-1,-1,
54
+
55
+ 1, 5,-1,-1,-1,
56
+
57
+ 0, 4, 6,-1,-1,
58
+
59
+ 1, 3, 5, 7,-1,
60
+
61
+ 2, 4, 8,-1,-1,
62
+
63
+ 3, 7,-1,-1,-1,
64
+
65
+ 4, 6, 8,-1,-1,
66
+
67
+ 5, 7,-1,-1,-1,
68
+
69
+ };
70
+
71
+
72
+
73
+ char state[MAX_STATE + 1][SIZE];
74
+
75
+ char space_postion[MAX_STATE];
76
+
77
+ int prev_state[MAX_STATE];
78
+
79
+
80
+
81
+ char check_table[MAX_STATE * 2];
82
+
83
+
84
+
85
+ char init_state[SIZE] = {
86
+
87
+ 8, 6, 7, 2, 5, 4, 3, 0, 1
88
+
89
+ };
90
+
91
+
92
+
93
+ char final_state[SIZE] = {
94
+
95
+ 1, 2, 3, 4, 5, 6, 7, 8, 0
96
+
97
+ };
98
+
99
+
100
+
101
+ void print_answer( int n )
102
+
103
+ {
104
+
105
+ int i, j;
106
+
107
+ if( n != 0 ) print_answer( prev_state[n] );
108
+
109
+ for( i = 0; i < 3; i++ ){
110
+
111
+ for( j = 0; j < 3; j++ ){
112
+
113
+ printf("%d ", state[n][i * 3 + j] );
114
+
115
+ }
116
+
117
+ printf("\n");
118
+
119
+ }
120
+
121
+ printf("\n");
122
+
123
+ }
124
+
125
+
126
+
127
+ int change_number( char *board )
128
+
129
+ {
130
+
131
+ char work[SIZE];
132
+
133
+ static int fact_table[SIZE] = {
134
+
135
+ 40320, 5040, 720, 120, 24, 6, 2, 1, 0,
136
+
137
+ };
138
+
139
+ int j, k, value = 0;
140
+
141
+ memcpy( work, board, SIZE );
142
+
143
+ for( j = 0; j < SIZE - 1; j++ ){
144
+
145
+ value += fact_table[j] * work[j];
146
+
147
+ for( k = j + 1; k < SIZE; k++ ){
148
+
149
+ if( work[j] < work[k] ) work[k]--;
150
+
151
+ }
152
+
153
+ }
154
+
155
+ return value;
156
+
157
+ }
158
+
159
+
160
+
161
+ void search( void )
162
+
163
+ {
164
+
165
+ int front = 0, rear = 1;
166
+
167
+ memcpy( state[0], init_state, SIZE );
168
+
169
+ space_postion[0] = 7;
170
+
171
+ prev_state[0] = -1;
172
+
173
+ check_table[ change_number( init_state ) ] = TRUE;
174
+
175
+ check_table[ change_number( final_state ) ] = GOAL;
176
+
177
+
178
+
179
+ while( front < rear ){
180
+
181
+ int s = space_postion[front];
182
+
183
+ int i, k, n;
184
+
185
+ for( i = 0; (n = adjacent[s][i]) != -1; i++ ){
186
+
187
+
188
+
189
+ memcpy( state[rear], state[front], SIZE );
190
+
191
+
192
+
193
+ state[rear][s] = state[rear][n];
194
+
195
+ state[rear][n] = 0;
196
+
197
+ space_postion[rear] = n;
198
+
199
+ prev_state[rear] = front;
200
+
201
+ k = change_number( state[rear] );
202
+
203
+ if( check_table[k] == GOAL ){
204
+
205
+
206
+
207
+ print_answer( rear );
208
+
209
+ printf("状態数 %d 個\n", rear );
210
+
211
+ return;
212
+
213
+ } else if( !check_table[k] ){
214
+
215
+
216
+
217
+ check_table[k] = TRUE;
218
+
219
+ rear++;
220
+
221
+ }
222
+
223
+ }
224
+
225
+ front++;
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+ int main()
234
+
235
+ {
236
+
237
+ int start, end;
238
+
239
+
240
+
241
+ memset( check_table, 0 , MAX_STATE * 2 );
242
+
243
+ start = clock();
244
+
245
+ search();
246
+
247
+ end = clock();
248
+
249
+ printf("時間 %d \n", end - start );
250
+
251
+ return 0;
252
+
253
+ }
254
+
255
+
256
+
257
+ ```