p2739 Shuttle Puzzle

來源:互聯網
上載者:User

標籤:ack   ring   long   names   cin   cassert   with   int   pre   

觀察範例得知就是和離‘_‘左邊最近的‘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_pairusing namespace std;const double EPS=1e-8;typedef long long lon;const lon SZ=1030,INF=0x7FFFFFFF,mod=9901;void work(string str,string tmp,int n){    vector<int> res;    int pos=str.find(‘_‘);    for(int time=1;;++time)    {        if(str==tmp)break;        for(int i=0;i<min(time,n);++i)        {            if(str==tmp)break;            int dst;            if(time&1)dst=str.find_last_of(‘w‘,pos);            else dst=str.find(‘b‘,pos);            if(dst==-1)break;            //cout<<str<<endl;            //cout<<dst<<endl;            res.push_back(dst);            swap(str[pos],str[dst]);            pos=dst;        }    }    for(int i=0;i<res.size();++i)    {        if(i%20!=0)cout<<" ";        cout<<res[i]+1;        if((i+1)%20==0)cout<<endl;    }cout<<endl;}int main(){    std::ios::sync_with_stdio(0);    //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 0;}

 

p2739 Shuttle Puzzle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.