-
Notifications
You must be signed in to change notification settings - Fork 0
/
c35.cpp
155 lines (137 loc) · 2 KB
/
c35.cpp
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
#include <cstdio>
#include <queue>
#include <map>
enum colour
{
N,
Y,
R,
G
};
struct State
{
bool a[7][7];
int pi, pj, pk, r;
colour pre;
bool operator<(const State& y) const
{
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
if (a[i][j] < y.a[i][j]) return true;
else if (a[i][j] > y.a[i][j]) return false;
}
}
return pi < y.pi or (pi == y.pi and pj < y.pj) or (pi == y.pi and pj == y.pj and pk < y.pk);
}
};
State NewState()
{
State s;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
s.a[i][j] = false;
}
}
s.a[1][2] = true;
s.a[2][5] = true;
s.a[4][3] = true;
s.a[5][2] = true;
s.pi = 3;
s.pj = 3;
s.pk = 3;
s.r = 4;
s.pre = N;
return s;
}
bool GoalState(State s)
{
return 0 == s.r;
}
void PrintState(State s)
{
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
if (s.a[i][j]) printf("%3d%3d", i, j);
}
}
printf(" %c\n", s.pre < 2 ? (s.pre < 1 ? 'N' : 'Y') : (s.pre < 3 ? 'R' : 'G'));
}
//yellow
State YY(State s)
{
if (s.pk > 2) s.pk--;
else s.pk = 4;
s.pre = Y;
return s;
}
//red
State RR(State s)
{
s.pi -= 3;
s.pj -= s.pk;
int x = s.pj;
int y = -s.pi;
s.pi = x + 3;
s.pj = y + s.pk;
if (0 <= s.pi and 7 > s.pi and 0 <= s.pj and 7 > s.pj)
{
if (1 == s.pi and 3 == s.pj) s.pi = 2;
s.r -= s.a[s.pi][s.pj];
s.a[s.pi][s.pj] = false;
}
s.pre = R;
return s;
}
//green
State GG(State s)
{
if (s.pk < 4) s.pk++;
else s.pk = 2;
s.pre = G;
return s;
}
int main()
{
State (*func[3])(State) = {YY, RR, GG};
std::map<State, State> m;
std::queue<State> q;
State x = NewState();
q.push(x);
m[x] = x;
State tmp;
bool keep = true;
while (not q.empty() and keep)
{
State s = q.front();
q.pop();
for (int i = 0; i < 3; i++)
{
tmp = func[i](s);
if (0 == m.count(tmp))
{
m[tmp] = s;
if (GoalState(tmp))
{
keep = false;
break;
}
else
{
q.push(tmp);
}
}
}
}
while (x < tmp or tmp < x)
{
PrintState(tmp);
tmp = m[tmp];
}
return 0;
}