http://acm.timus.ru/problem.aspx?space=1&num=1474
找规律 然后模拟
规律: 假设隔一个跳为 jump,相邻跳为 mov (mov 分为从左向右 movl_r 和从右向左 movr_l)
jump 的优先级高于 mov
对于 mov 当空格在 左面 movl_r 优先级高 当空格在 右面 movr_l 优先级高 当空格在中间 优先级任意 但要始终不变。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<cmath>
using namespace std;
//#pragma comment(linker,"/STACK:1000000000,1000000000")
#define LL long long
const int INF=0x3f3f3f3f;
const int N=1005;
const int M=1000000;
int frogs[N];
int space;
int ans[M+5],num;
int n;
bool jump()
{
if(space-2>=0&&frogs[space-2]==-1&&frogs[space-1]==1)
{swap(frogs[space-2],frogs[space]);ans[num++]=space=space-2;return true;}
if(space+2<=2*n&&frogs[space+2]==1&&frogs[space+1]==-1)
{swap(frogs[space+2],frogs[space]);ans[num++]=space=space+2;return true;}
return false;
}
bool movr_l()
{
if(space+1<=2*n&&frogs[space+1]==1)
{swap(frogs[space+1],frogs[space]);ans[num++]=space=space+1;return true;}
return false;
}
bool movl_r()
{
if(space-1>=0&&frogs[space-1]==-1)
{swap(frogs[space-1],frogs[space]);ans[num++]=space=space-1;return true;}
return false;
}
bool mov()
{
if(space>=n)
{
if(movr_l())
return true;
if(movl_r())
return true;
}else
{
if(movl_r())
return true;
if(movr_l())
return true;
}
return false;
}
bool action()
{
if(jump())
return true;
if(mov())
return true;
return false;
}
int main()
{
//freopen("data.txt","r",stdin);
while(cin>>n)
{
for(int i=0;i<n;++i)
{frogs[i]=-1;frogs[i+n+1]=1;}
frogs[n]=0;
space=n;
num=0;
while(num<M&&action())
;
if(space!=n)
{cout<<"-1"<<endl;continue;}
int l;
for(l=0;l<n;++l)
if(frogs[l]!=1)
break;
if(l<n)
cout<<"-1"<<endl;
else
{
cout<<num<<endl;
cout<<ans[0];
for(int i=1;i<num;++i)
cout<<" "<<ans[i];
cout<<endl;
}
}
return 0;
}
浙公网安备 33010602011771号