Advertisement
wgma

Untitled

Sep 7th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <queue>
  5. #include <sstream>
  6. #include <set>
  7. #include <algorithm>
  8. #define MAXN 8
  9. #define reset_game(G,M) FOR(INT i = 0; i < N;i++)G[i].clear();
  10. #define to_s(N) static_cast<ostringstream*>( &(ostringstream() << N) )->str()
  11. USING namespace std;
  12.  
  13. struct state{
  14. vector<int>g[MAXN];
  15. INT d;
  16. string id;
  17. };
  18.  
  19. INT N,ai;
  20. vector<int>game[MAXN];
  21. set< string >past_game;
  22.  
  23. string to_id(vector<int>g[MAXN]){
  24. string id;
  25. FOR(INT i = 0; i < MAXN;i++){
  26. string t;
  27. FOR(INT j = 0; j < g[i].SIZE();j++){
  28. t+= to_s(g[i][j]);
  29. }
  30. t+="-";
  31. id+=t;
  32. }
  33. RETURN id;
  34. }
  35. bool game_done(vector<int>g[MAXN]){
  36. FOR(INT i = 0; i < N;i++)
  37. IF(g[i].empty())RETURN FALSE;
  38. FOR(INT i = 0; i < N;i++)
  39. IF(g[i].SIZE()!= 1 || g[i][0] != i+1)RETURN FALSE;
  40. RETURN TRUE;
  41. }
  42. void print_game(vector<int>g[MAXN],INT d,string id){
  43. printf("D:%d ;",d);cout << id << endl;
  44. FOR(INT i = 0; i < MAXN;i++){
  45. printf("%d:",i+1);
  46. FOR(INT j = 0; j < g[i].SIZE();j++)printf("%d",g[i][j]);
  47. printf("\n");
  48. }
  49. printf("\n");
  50. }
  51. INT bfs(){
  52. queue<state> q;
  53. q.push( (state){game[0],game[1],game[2],game[3],game[4],game[5],game[6],game[7],0,to_id(game)});
  54. while(!q.empty()){
  55. vector<int>g[MAXN] = q.front().g;
  56. INT d = q.front().d;
  57. string id = q.front().id;
  58. q.pop();
  59. IF(past_game.find(id) != past_game.END())
  60. continue;
  61. ELSE
  62. past_game.INSERT(id);
  63. //print_game(g,d,id);
  64. IF(game_done(g)){
  65. RETURN d;
  66. }
  67. FOR(INT i = 0; i < N;i++){
  68. IF(g[i].empty())continue;
  69. IF(i-1>=0){
  70. INT left_end = g[i-1].SIZE()-1;
  71. INT curr_end = g[i].SIZE()-1;
  72. IF(g[i-1].empty()){
  73. g[i-1].push_back(g[i][curr_end]);
  74. g[i].erase(g[i].BEGIN() + curr_end);
  75. q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
  76. left_end++;curr_end--;
  77. g[i].push_back(g[i-1][left_end]);
  78. g[i-1].erase(g[i-1].BEGIN()+left_end);
  79. }
  80. ELSE IF(g[i-1][left_end] > g[i][curr_end]){
  81. g[i-1].push_back(g[i][curr_end]);
  82. g[i].erase(g[i].BEGIN() + curr_end);
  83. q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
  84. left_end++;curr_end--;
  85. g[i].push_back(g[i-1][left_end]);
  86. g[i-1].erase(g[i-1].BEGIN()+left_end);
  87. }
  88. }
  89. IF(i+1<N){
  90. INT rght_end = g[i+1].SIZE()-1;
  91. INT curr_end = g[i].SIZE()-1;
  92. IF(g[i+1].empty()){
  93. g[i+1].push_back(g[i][curr_end]);
  94. g[i].erase(g[i].BEGIN() + curr_end);
  95. q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
  96. rght_end++;curr_end--;
  97. g[i].push_back(g[i+1][rght_end]);
  98. g[i+1].erase(g[i+1].BEGIN()+rght_end);
  99. }
  100. ELSE IF(g[i+1][rght_end] > g[i][curr_end]){
  101. g[i+1].push_back(g[i][curr_end]);
  102. g[i].erase(g[i].BEGIN() + curr_end);
  103. q.push( (state){g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],d+1,to_id(g)});
  104. rght_end++;curr_end--;
  105. g[i].push_back(g[i+1][rght_end]);
  106. g[i+1].erase(g[i+1].BEGIN()+rght_end);
  107. }
  108. }
  109. }
  110. }
  111. RETURN -1;
  112. }
  113. INT main(){
  114. //freopen("input.txt","r",stdin);
  115. while(scanf("%d",&N)&& N!= 0){
  116. FOR(INT i = 0; i < N;i++){
  117. scanf("%d",&ai);
  118. game[i].push_back(ai);
  119. }
  120. //print_game(game,0,to_id(game));
  121. INT ans = bfs();
  122. IF(ans == -1)printf("IMPOSSIBLE\n");
  123. ELSE         printf("%d\n",ans);
  124. past_game.clear();
  125. reset_game(game,MAXN);
  126. }
  127. RETURN 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement