开始我的代码生涯吧
sicily1511 慢慢学 用map优化
我是菜鸟 求各位大神指导啊
#include<iostream>
#include<math.h>
#include <vector>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef pair<int,int> pii;
struct state
{
int m;
int n;
char op;
int depth;
state(){}
state(int x,int y,char ch,int d)
{
m = x;
n = y;
op = ch;
depth = d;
}
/*
void print() const
{
printf("%d\n%d\n",m,n);
}
*/
};
stack<char> st;
state OPA(state x)
{
state temp;
temp.m = x.n;
temp.n = x.m;
temp.op = 'A';
temp.depth = x.depth + 1;
return temp;
}
state OPB(state x)
{
state temp;
temp.m = (x.m % 10) * 1000 + (x.m / 10);
temp.n = (x.n % 10) * 1000 + (x.n / 10);
temp.op = 'B';
temp.depth = x.depth + 1;
return temp;
}
state OPC(state xx)
{
state temp;
int x[4],y[4];
int tm1=xx.m,tm2=xx.n;
for(int i=0;i<4;++i)
{
x[i]=tm1%10;
tm1/=10;
y[i]=tm2%10;
tm2/=10;
}
int a = x[1];
int b = x[2];
int c = y[1];
int d = y[2];
x[1] = b;
x[2] = d;
y[1] = a;
y[2] = c;
tm1=0,tm2=0;
for(int i=3;i>=0;--i)
tm1=10*tm1+x[i],tm2=10*tm2+y[i];
temp.m=tm1;
temp.n=tm2;
temp.op = 'C';
temp.depth = xx.depth + 1;
return temp;
}
state inv_OPA(state xx)
{
state temp;
temp.m = xx.n;
temp.n = xx.m;
temp.depth = xx.depth - 1;
return temp;
}
state inv_OPB(state xx)
{
state temp;
temp.m = (xx.m % 1000) * 10 + (xx.m / 1000);
temp.n = (xx.n % 1000) * 10 + (xx.n / 1000);
temp.depth = xx.depth - 1;
return temp;
}
state inv_OPC(state xx)
{
state temp;
int x[4],y[4];
int tm1=xx.m,tm2=xx.n;
for(int i=0;i<4;++i)
{
x[i]=tm1%10;
tm1/=10;
y[i]=tm2%10;
tm2/=10;
}
int a = x[1];
int b = x[2];
int c = y[1];
int d = y[2];
x[1] = c;
x[2] = a;
y[1] = d;
y[2] = b;
tm1=0,tm2=0;
for(int i=3;i>=0;--i)
tm1=10*tm1+x[i],tm2=10*tm2+y[i];
temp.m=tm1;
temp.n=tm2;
temp.depth = xx.depth - 1;
return temp;
}
int main()
{
int maxstep, m, n;
while (cin >> maxstep && maxstep != -1)
{
bool flag = false;
queue<state> qu;
map<pii,char> map1;
int a1,b1,c1,d1;
int a2,b2,c2,d2;
cin >> a1 >> b1 >> c1 >> d1;
cin >> a2 >> b2 >> c2 >> d2;
m = a1 *1000 + b1 * 100 + c1 * 10 + d1;
n = a2 *1000 + b2 * 100 + c2 * 10 + d2;
state result = state(m,n,' ',0);
state original = state(1234,8765,' ',0);;
if (original.m == result.m && original.n == result.n)
flag = true;
qu.push(original);
while(qu.front().depth < maxstep)
{
state temp;
temp = qu.front();
qu.pop();
state a = OPA(temp);
if(map1.find(make_pair(a.m,a.n)) == map1.end()){
qu.push(a);
map1[make_pair(a.m,a.n)] = 'A';
}
if (a.m == result.m && a.n == result.n){
result.op = 'A';
result.depth = a.depth;
break;
}
state b = OPB(temp);
if(map1.find(make_pair(b.m,b.n)) == map1.end()){
qu.push(b);
map1[make_pair(b.m,b.n)] = 'B';
}
if (b.m == result.m && b.n == result.n){
result.op = 'B';
result.depth = b.depth;
break;
}
state c = OPC(temp);
if (map1.find(make_pair(c.m,c.n)) == map1.end())
{
qu.push(c);
map1[make_pair(c.m,c.n)] = 'C';
}
if (c.m == result.m && c.n == result.n){
result.op = 'C';
result.depth = c.depth;
break;
}
}
while(result.depth != 0)
{
//result.print();
char ch = map1[make_pair(result.m,result.n)];
st.push(ch);
if(ch == 'A')
result = inv_OPA(result);
if(ch == 'B')
result = inv_OPB(result);
if(ch == 'C')
result = inv_OPC(result);
}
if (flag)
cout << "0";
else if(!st.empty())
{
cout << st.size() << " ";
char temp;
while(!st.empty())
{
temp = st.top();
st.pop();
cout << temp ;
}
}
else
cout << "-1";
cout << endl;
}
return 0;
}