Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <queue>
- #include <sstream>
- #include <set>
- #include <algorithm>
- #define MAXN 8
- #define reset_game(G,M) FOR(INT i = 0; i < N;i++)G[i].clear();
- #define to_s(N) static_cast<ostringstream*>( &(ostringstream() << N) )->str()
- USING namespace std;
- struct state{
- vector<int>g[MAXN];
- INT d;
- string id;
- };
- INT N,ai;
- vector<int>game[MAXN];
- set< string >past_game;
- string to_id(vector<int>g[MAXN]){
- string id;
- FOR(INT i = 0; i < MAXN;i++){
- string t;
- FOR(INT j = 0; j < g[i].SIZE();j++){
- t+= to_s(g[i][j]);
- }
- t+="-";
- id+=t;
- }
- RETURN id;
- }
- bool game_done(vector<int>g[MAXN]){
- FOR(INT i = 0; i < N;i++)
- IF(g[i].empty())RETURN FALSE;
- FOR(INT i = 0; i < N;i++)
- IF(g[i].SIZE()!= 1 || g[i][0] != i+1)RETURN FALSE;
- RETURN TRUE;
- }
- void print_game(vector<int>g[MAXN],INT d,string id){
- printf("D:%d ;",d);cout << id << endl;
- FOR(INT i = 0; i < MAXN;i++){
- printf("%d:",i+1);
- FOR(INT j = 0; j < g[i].SIZE();j++)printf("%d",g[i][j]);
- printf("\n");
- }
- printf("\n");
- }
- INT bfs(){
- queue<state> q;
- q.push( (state){game[0],game[1],game[2],game[3],game[4],game[5],game[6],game[7],0,to_id(game)});
- while(!q.empty()){
- vector<int>g[MAXN] = q.front().g;
- INT d = q.front().d;
- string id = q.front().id;
- q.pop();
- IF(past_game.find(id) != past_game.END())
- continue;
- ELSE
- past_game.INSERT(id);
- //print_game(g,d,id);
- IF(game_done(g)){
- RETURN d;
- }
- FOR(INT i = 0; i < N;i++){
- IF(g[i].empty())continue;
- IF(i-1>=0){
- INT left_end = g[i-1].SIZE()-1;
- INT curr_end = g[i].SIZE()-1;
- IF(g[i-1].empty()){
- g[i-1].push_back(g[i][curr_end]);
- g[i].erase(g[i].BEGIN() + curr_end);
- q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
- left_end++;curr_end--;
- g[i].push_back(g[i-1][left_end]);
- g[i-1].erase(g[i-1].BEGIN()+left_end);
- }
- ELSE IF(g[i-1][left_end] > g[i][curr_end]){
- g[i-1].push_back(g[i][curr_end]);
- g[i].erase(g[i].BEGIN() + curr_end);
- q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
- left_end++;curr_end--;
- g[i].push_back(g[i-1][left_end]);
- g[i-1].erase(g[i-1].BEGIN()+left_end);
- }
- }
- IF(i+1<N){
- INT rght_end = g[i+1].SIZE()-1;
- INT curr_end = g[i].SIZE()-1;
- IF(g[i+1].empty()){
- g[i+1].push_back(g[i][curr_end]);
- g[i].erase(g[i].BEGIN() + curr_end);
- q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
- rght_end++;curr_end--;
- g[i].push_back(g[i+1][rght_end]);
- g[i+1].erase(g[i+1].BEGIN()+rght_end);
- }
- ELSE IF(g[i+1][rght_end] > g[i][curr_end]){
- g[i+1].push_back(g[i][curr_end]);
- g[i].erase(g[i].BEGIN() + curr_end);
- q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
- rght_end++;curr_end--;
- g[i].push_back(g[i+1][rght_end]);
- g[i+1].erase(g[i+1].BEGIN()+rght_end);
- }
- }
- }
- }
- RETURN -1;
- }
- INT main(){
- //freopen("input.txt","r",stdin);
- while(scanf("%d",&N)&& N!= 0){
- FOR(INT i = 0; i < N;i++){
- scanf("%d",&ai);
- game[i].push_back(ai);
- }
- //print_game(game,0,to_id(game));
- INT ans = bfs();
- IF(ans == -1)printf("IMPOSSIBLE\n");
- ELSE printf("%d\n",ans);
- past_game.clear();
- reset_game(game,MAXN);
- }
- RETURN 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement