问题背景
这是一个幼儿锻炼智力的玩具,但是有一个“小可爱”在哄孩子玩的时候,解不出来,遂来求助。
书归正文,对于上面的局面,需要通过不断移动通过其他汽车,让橙色汽车向下移动到出口,即为胜利。
在这里,不允许把小车拿起来,同时对于横着的小汽车只能横着移动,竖着的小车同样道理。
问题求解
这是一个十分经典的计算机领域的问题,可以使用宽度优先遍历的方式求解最少的移动步数。
抽象一下问题,主要是棋盘里面小车的摆放方式,如何更好的表示:
首先, 6 * 6 的棋盘,是一个二维数组。
数组的内容表示如下:
0: 空
1:作为小车的其他部分填充,小车只有最左边/最上边的是2/3/4/5
2:1 * 2
3:1 * 3
4:2 * 1
5:3 * 1
所以,上面图片的例子数组表示就是:
VII a = {
{0,2,1,0,0,4},
{4,4,0,0,0,1},
{1,1,0,3,1,1},
{3,1,1,4,0,0},
{0,0,4,1,2,1},
{0,0,1,3,1,1}
};
所以,只要修改main函数里面的二维数组,就可以求解其他的情况了。
另外,因为使用BFS,所以输出的执行步骤需要倒着看。
下面贴出来代码:(c++版本)
#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>
#include<iomanip>
#include<queue>
using namespace std;
// 6 * 6
// 0: 空
// 1:填充
// 2:1 * 2
// 3:1 * 3
// 4:2 * 1
// 5:3 * 1
typedef vector<vector<int>> VII;
unordered_map<string, bool> st;
unordered_map<string, string> pre;
string vec2str(VII a) {
string res;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
res += to_string(a[i][j]);
}
}
return res;
}
VII str2vec(string s) {
VII a(6, vector<int>(6, 0));
int k = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
a[i][j] = s[k++] - '0';
}
}
return a;
}
void print(VII a) {
vector<vector<char>> b(6, vector<char>(6, '0'));
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (b[i][j] != '0') continue;
switch (a[i][j]) {
case 0: {
b[i][j] = '-'; break;
}
case 2: {
b[i][j] = b[i][j + 1] = '@';
break;
}
case 3: {
b[i][j] = b[i][j + 1] = b[i][j + 2] = '#';
break;
}
case 4: {
b[i][j] = b[i+1][j] = '$';
break;
}
case 5: {
b[i][j] = b[i+1][j] = b[i+2][j] = '%';
}
}
}
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
cout << b[i][j];
}
cout << endl;
}
cout << endl;
}
bool is_success(VII a) {
return a[4][3] == 4;
}
void bfs(VII& b) {
queue<VII> q;
q.push(b);
st[vec2str(b)] = true;
pre[vec2str(b)] = "";
while (q.size()) {
VII a = q.front();
if (is_success(a)) {
string s = vec2str(a);
while (s != "") {
print(str2vec(s));
s = pre[s];
}
return;
}
string s = vec2str(a);
q.pop();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (a[i][j] == 0 || a[i][j] == 1) continue;
if (a[i][j] == 2) {
// 1*2 左右移动 k = [0, 4]
// left
if (j - 1 >= 0 && a[i][j - 1] == 0) {
a[i][j] = a[i][j + 1] = 0;
a[i][j - 1] = 2;
a[i][j] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i][j - 1] = 0;
a[i][j] = 2;
a[i][j + 1] = 1;
}
// right
if (j + 2 < 6 && a[i][j + 2] == 0) {
a[i][j] = a[i][j + 1] = 0;
a[i][j + 1] = 2;
a[i][j + 2] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i][j + 1] = a[i][j + 2] = 0;
a[i][j] = 2;
a[i][j + 1] = 1;
}
}
else if (a[i][j] == 3) {
// left
if (j - 1 >= 0 && a[i][j - 1] == 0) {
a[i][j] = a[i][j + 1] = a[i][j + 2] = 0;
a[i][j - 1] = 3;
a[i][j] = 1;
a[i][j + 1] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i][j - 1] = a[i][j] = a[i][j + 1] = 0;
a[i][j] = 3;
a[i][j + 1] = 1;
a[i][j + 2] = 1;
}
// right
if (j + 3 < 6 && a[i][j + 3] == 0) {
a[i][j] = a[i][j + 1] = a[i][j + 2] = 0;
a[i][j + 1] = 3;
a[i][j + 2] = 1;
a[i][j + 3] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i][j + 1] = a[i][j + 2] = a[i][j + 3] = 0;
a[i][j] = 3;
a[i][j + 1] = 1;
a[i][j + 2] = 1;
}
}
else if (a[i][j] == 4) {
// up
if (i - 1 >= 0 && a[i - 1][j] == 0) {
a[i][j] = a[i + 1][j] = 0;
a[i - 1][j] = 4;
a[i][j] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i - 1][j] = 0;
a[i][j] = 4;
a[i + 1][j] = 1;
}
// down
if (i + 2 < 6 && a[i + 2][j] == 0) {
a[i][j] = a[i + 1][j] = 0;
a[i + 1][j] = 4;
a[i + 2][j] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i + 1][j] = a[i + 2][j] = 0;
a[i][j] = 4;
a[i + 1][j] = 1;
}
}
else if (a[i][j] == 5) {
// up
if (i - 1 >= 0 && a[i - 1][j] == 0) {
a[i][j] = a[i + 1][j] = a[i + 2][j] = 0;
a[i - 1][j] = 5;
a[i][j] = 1;
a[i + 1][j] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i - 1][j] = a[i][j] = a[i + 1][j] = 0;
a[i][j] = 5;
a[i + 1][j] = 1;
a[i + 2][j] = 1;
}
// down
if (i + 3 < 6 && a[i + 3][j] == 0) {
a[i][j] = a[i + 1][j] = a[i + 2][j] = 0;
a[i + 1][j] = 5;
a[i + 2][j] = 1;
a[i + 3][j] = 1;
if (!st.count(vec2str(a))) {
st[vec2str(a)] = true;
pre[vec2str(a)] = s;
q.push(a);
}
a[i + 1][j] = a[i + 2][j] = a[i + 3][j] = 0;
a[i][j] = 5;
a[i + 1][j] = 1;
a[i + 2][j] = 1;
}
}
}
}
}
}
int main() {
//freopen("out.txt", "w", stdout);
/* VII a = {
{0,2,1,0,0,4},
{4,4,0,0,0,1},
{1,1,0,3,1,1},
{3,1,1,4,0,0},
{0,0,4,1,2,1},
{0,0,1,3,1,1}
};*/
VII a = {
{0,5,0,4,0,0},
{0,1,0,1,5,0},
{0,1,2,1,1,0},
{0,3,1,1,1,0},
{0,0,4,0,0,0},
{2,1,1,2,1,0}
};
vector<string> path;
bfs(a);
return 0;
}