#include<bits/stdc++.h>
using namespace std;
namespace wxh666{
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
#define sqrt(a) __builtin_sqrt(a)
#define f(a,b,c) for(int a=b;a<=c;a++)
#define ff(a,b,c) for(int a=b;a>=c;a--)
#define g(n) f(i,1,n)
#define df(n,m) g(n) f(j,1,m)
#define max max_by_wxh
template<typename T>
inline T max_by_wxh(T x,T y) {return ((x)>(y)?(x):y);}
template<typename T,typename ...Args>
inline T max_by_wxh(T x,Args ...xs) {return max_by_wxh(x,max_by_wxh(xs...));}
#define min min_by_wxh
template<typename T>
inline T min_by_wxh(T x,T y) {return ((x)<(y)?(x):y);}
template<typename T,typename ...Args>
inline T min_by_wxh(T x,Args ...xs) {return min_by_wxh(x,min_by_wxh(xs...));}
template<typename T>
inline void pu(T x)
{
ios::sync_with_stdio(false);
cout<<x;
ios::sync_with_stdio(true);
return;
}
template<typename T>
inline void pk(T x)
{
ios::sync_with_stdio(false);
cout<<x<<" ";
ios::sync_with_stdio(true);
return;
}
inline void pk(char x)
{
ios::sync_with_stdio(false);
cout<<x;
if(x!='\n') cout<<' ';
ios::sync_with_stdio(true);
return;
}
template<typename T,typename ...Args>
inline void pu(T x,Args ...xs){pu(x);pu(xs...);}
template<typename T,typename ...Args>
inline void ppu(T x,Args ...xs){pu(x,xs...,'\n');}
template<typename T,typename ...Args>
inline void pk(T x,Args ...xs){pk(x);pk(xs...);}
template<typename T,typename ...Args>
inline void ppk(T x,Args ...xs){pk(x,xs...,'\n');}
#define cs(dt,n,m) \
for(int i=1;i<=n;i++)\
{\
for(int j=1;j<=m;j++)\
printf("%d ",dt[i][j]);\
cout<<endl;\
}
};using namespace wxh666;
// pair first means step
// pair second means state
map<string,pair<int,int> >maps;
#define mp(x,y) make_pair(x,y)
#define in read()
string read()
{
string ins="";
cin>>ins;
return ins;
}
pair<int,int> one_to_two(int x)
{
return mp(x/5+1,x%5+1);
}
inline int two_to_one(int x,int y)
{
return (x-1)*5+y-1;
}
string dt="111110111100*110000100000";
string ins="";
int T;
void BFS()
{
// first is x and y
// second`s first is step(range 1...7)
// second`s second is state
// 1 means start in ins
// -1 means start in dt
queue<pair<string,pair<int,int> > >p;
int dx[]={0,-2,-2,-1,-1,1,1,2,2};
int dy[]={0,-1,1,-2,2,-2,2,-1,1};
p.push(mp(ins,mp(0,1)));
p.push(mp(dt,mp(0,-1)));
maps[ins]=mp(0,1);
maps[dt]=mp(0,-1);
while(!p.empty())
{
pair<string,pair<int,int> >now=p.front();p.pop();
if(now.second.first>8)
{
ppk(-1);
return;
}
string sp=now.first;
int _=sp.find('*');
pair<int,int>pos=one_to_two(_);
int x=pos.first,y=pos.second;
g(8)
{
sp=now.first;
int xx=x+dx[i],yy=y+dy[i];
if(xx<1||yy<1||xx>5||yy>5) continue;
swap(sp[two_to_one(x,y)],sp[two_to_one(xx,yy)]);
if(maps[sp].second==now.second.second) continue;
if(maps[sp].second==-now.second.second)
{
if(maps[sp].first+now.second.first+1<=15)
ppk(maps[sp].first+now.second.first+1);
else
ppk(-1);
return;
}
maps[sp]=mp(now.second.first+1,now.second.second);
p.push(mp(sp,mp(now.second.first+1,now.second.second)));
}
}
ppk("ERROR");
}
int main()
{
cin>>T;
while(T--)
{
maps.clear();
int x;
ins="";
ins=in;
ins+=in;
ins+=in;
ins+=in;
ins+=in;
f(i,0,ins.size()-1) if(ins[i]=='*') x=i;
if(ins==dt) {ppk(0);continue;}
BFS();
}
return 0;
}
story
我写完之后样例一直报段错误,多组数据输进来就会报错double free or corruption (out),我想着交一下代码,然后就过了