p2739 Shuttle Puzzle

时间:2024-09-15 08:04:13

观察样例得知就是和离'_'左边最近的'w'交换位置,然后和离'_'右边最近的'b'交换位置,轮流进行。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF,mod=; void work(string str,string tmp,int n)
{
vector<int> res;
int pos=str.find('_');
for(int time=;;++time)
{
if(str==tmp)break;
for(int i=;i<min(time,n);++i)
{
if(str==tmp)break;
int dst;
if(time&)dst=str.find_last_of('w',pos);
else dst=str.find('b',pos);
if(dst==-)break;
//cout<<str<<endl;
//cout<<dst<<endl;
res.push_back(dst);
swap(str[pos],str[dst]);
pos=dst;
}
}
for(int i=;i<res.size();++i)
{
if(i%!=)cout<<" ";
cout<<res[i]+;
if((i+)%==)cout<<endl;
}cout<<endl;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
//cin>>casenum;
//for(lon time=1;time<=casenum;++time)
{
int n;
cin>>n;
string str(n,'w');
str+='_';
str+=string(n,'b');
string tmp(n,'b');
tmp+='_';
tmp+=string(n,'w');
work(str,tmp,n);
}
return ;
}