-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path狼羊菜过河.cpp
More file actions
88 lines (82 loc) · 2.18 KB
/
Copy path狼羊菜过河.cpp
File metadata and controls
88 lines (82 loc) · 2.18 KB
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
#include <bits/stdc++.h>
using namespace std;
bool check(unsigned s)
{
unsigned W = 0b1000, S = 0b0100, V = 0b0010, P = 0b0001;
// 如果狼和羊在同一侧且人不在该侧 -> 不合法
bool wolf_side = (s & W) != 0;
bool sheep_side = (s & S) != 0;
bool people_side = (s & P) != 0;
if ((wolf_side == sheep_side) && (people_side != wolf_side))
return false;
// 如果羊和菜在同一侧且人不在该侧 -> 不合法
bool veg_side = (s & V) != 0;
if ((sheep_side == veg_side) && (people_side != sheep_side))
return false;
return true;
}
int main()
{
unsigned final = 0b1111;
unsigned wolf = 0b1000;
unsigned sheep = 0b0100;
unsigned vegetable = 0b0010;
unsigned people = 0b0001;
queue<unsigned> q;
unordered_map<unsigned, unsigned> prev;
vector<char> visited(16, 0);
const unsigned start = 0;
visited[start] = 1;
q.push(start);
bool found = false;
while (!q.empty())
{
unsigned cur = q.front();
q.pop();
if (cur == final)
{
found = true;
break;
}
// 生成四种可能的移动:不带/带菜/带羊/带狼
unsigned candidates[4] = {
cur ^ people,
cur ^ people ^ vegetable,
cur ^ people ^ sheep,
cur ^ people ^ wolf};
for (unsigned nx : candidates)
{
if (visited[nx])
continue;
if (!check(nx))
continue;
visited[nx] = 1; // 标记为已发现,避免重复入队
prev[nx] = cur; // 记录前驱(首次发现)
q.push(nx);
if (nx == final)
{
found = true;
break;
}
}
if (found)
break;
}
if (!found)
{
cout << "No solution" << endl;
return 0;
}
// 重建路径并正序输出
vector<unsigned> path;
unsigned x = final;
path.push_back(x);
while (x != start)
{
x = prev[x];
path.push_back(x);
}
reverse(path.begin(), path.end());
for (unsigned p : path)
cout << bitset<4>(p) << '\n';
}