-
Notifications
You must be signed in to change notification settings - Fork 0
/
8puzzel.c
164 lines (153 loc) · 2.89 KB
/
8puzzel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
typedef long long num_t;
#define LEN 3
#define MAX_REC 51 // max record len
typedef struct {
num_t v[LEN][LEN]; // value
num_t row, col;
num_t cost;
num_t lv; // level
} rec_t; // record
num_t lim;
rec_t puz;
rec_t final = {
{
{1,2,3},
{8,0,4},
{7,6,5}
},
1, 1,
0
};
num_t
find_cost(rec_t * state) {
num_t cost = 0;
num_t i = 0;
for (; i < LEN; i++) {
num_t j = 0;
for (; j < LEN; j++) {
if (state->v[i][j] != final.v[i][j]) {
cost++;
}
}
}
return cost;
}
num_t
min_rec(rec_t * rec, num_t len) {
num_t min_cost = rec[0].cost;
num_t min_i = 0;
num_t i = 1;
for (; i < len; i++) {
if (rec[i].cost < min_cost) {
min_cost = rec[i].cost;
min_i = i;
}
}
return min_i;
}
void
swap_rec(rec_t * rec, num_t i, num_t j) {
rec_t tmp = rec[i];
rec[i] = rec[j];
rec[j] = tmp;
}
void
swap_cel(rec_t * rec, num_t i, num_t j) {
num_t v = rec->v[rec->row][rec->col];
rec->v[rec->row][rec->col] = rec->v[i][j];
rec->v[i][j] = v;
rec->row = i;
rec->col = j;
}
void
prec(rec_t * rec) {
printf(" --- %lld ---\n", rec->cost);
num_t i = 0;
for (; i < LEN; i++) {
num_t k = 0;
for (; k < rec->lv; k++) {
printf(" ");
}
num_t j = 0;
for (; j < LEN; j++) {
printf("%2lld ", rec->v[i][j]);
}
puts("");
}
}
void
can(void) {
num_t total = 0;
static rec_t recs[1500];
num_t rec_i = 0;
num_t rec_len = 0;
recs[rec_len++] = puz;
while (1) {
num_t i = min_rec(recs, rec_len);
swap_rec(recs, i, rec_len - 1);
rec_len--;
rec_t now = recs[rec_len];
prec(&now);
static num_t stop = 0;
if (stop++ == 21) break;
if (find_cost(&now) == 0) {
printf("YES\n");
break;
}
if (now.lv == lim) {
printf("NO\n");
break;
}
if (now.col < LEN - 1) {
rec_t tbl = now;
swap_cel(&tbl, tbl.row, tbl.col + 1);
tbl.cost = find_cost(&tbl);
tbl.lv++;
recs[rec_len++] = tbl;
}
if (now.row < LEN - 1) {
rec_t tbl = now;
swap_cel(&tbl, tbl.row + 1, tbl.col);
tbl.cost = find_cost(&tbl);
tbl.lv++;
recs[rec_len++] = tbl;
}
if (now.col > 0) {
rec_t tbl = now;
swap_cel(&tbl, tbl.row, tbl.col - 1);
tbl.cost = find_cost(&tbl);
tbl.lv++;
recs[rec_len++] = tbl;
}
if (now.row > 0) {
rec_t tbl = now;
swap_cel(&tbl, tbl.row - 1, tbl.col);
tbl.cost = find_cost(&tbl);
tbl.lv++;
recs[rec_len++] = tbl;
}
}
}
int
main(void) {
while (1) {
scanf("%lld", &lim);
if (lim == 0) break;
num_t i = 0;
for (; i < LEN; i++) {
num_t j = 0;
for (; j < LEN; j++) {
scanf("%lld", &puz.v[i][j]);
if (puz.v[i][j] == 0) {
puz.row = i;
puz.col = j;
}
}
}
puz.cost = find_cost(&puz);
puz.lv = 0;
can();
}
return 0;
}