问题:
我自己写了一个代码,但是编译一直有问题,不知道咋改,需求大神们的帮助TAT
题目介绍:
代码如下
我是用c++写的
#include<iostream>
#include<queue>
#include<map>
using namespace std;
int mp[3][3],emp[3][3];
int minn = INT_MAX;//表示当前的最小操作次数
map<int[3][3], bool> vis;//表示是否访问过这种序列
int swapno(int mp[3][3], int i, int j,int x,int y) {
int temp = mp[i][j];
mp[i][j] = mp[x][y];
mp[x][y] = temp;
}
struct node {
int m[3][3];//存放当前魔方的序列
int step;//存放到当前序列所完成的操作次数
node() {};
node(int _m[3][3], int _step) {
m[3][3] = _m[3][3];
step = _step;
}
}startnode,resultnode;
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> mp[i][j];
emp[i][j] = i * 3 + (j + 1);
startnode.m[i][j] = mp[i][j];
}
}
startnode.step = 0;
queue<node> q;
q.push(startnode);
vis[startnode.m] = 1;
while (!q.empty()) {
node top = q.front();
q.pop();
if (top.m == emp) { //序列与初始序列相等
if (top.step < minn) {
minn = top.step;
}
}
for (int m = 0; m < 3; m++) {
for (int i = 0; i < 4; i++) {
node no = top;
if (!vis[no.m]) {
if (i == 0) { //行左移
swapno(no.m, m, 1, m, 2);
swapno(no.m, m, 0, m, 1);
no.step++;
vis[no.m] = 1;
q.push(no);
}
else if (i == 1) { //行右移
swapno(no.m, m, 0, m, 1);
swapno(no.m, m, 1, m, 2);
no.step++;
vis[no.m] = 1;
q.push(no);
}
else if (i == 2) { // 列上移
swapno(no.m, 0, m, 1, m);
swapno(no.m, 1, m, 2, m);
no.step++;
vis[no.m] = 1;
q.push(no);
}
else { // 列下移
swapno(no.m, 1, m, 2, m);
swapno(no.m, 0, m, 1, m);
no.step++;
vis[no.m] = 1;
q.push(no);
}
}
}
}
}
cout << minn;
return 0;
}