设为首页 加入收藏

TOP

UVA - 1533 Moving Pegs
2015-07-24 05:41:25 来源: 作者: 【 】 浏览:5
Tags:UVA 1533 Moving Pegs

题意:首先给你空闲的位置,可以跳过几个来吃掉几个,求最短的吃完所有的,且最后一个回到开始指定的位置

思路:BFS+HASH判重,对于每个位置有六个方向,当然有的是不能走的,加上map的判重就可以了

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         using namespace std; const int MAXN = 16; int next[15][6] = { {-1, -1, -1, -1, 2, 3}, {-1, 1, -1, 3, 4, 5}, {1, -1, 2, -1, 5, 6}, {-1, 2, -1, 5, 7, 8}, {2, 3, 4, 6, 8, 9}, {3, -1, 5, -1, 9, 10}, {-1, 4, -1, 8, 11, 12}, {4, 5, 7, 9, 12, 13}, {5, 6, 8, 10, 13, 14}, {6, -1, 9, -1, 14, 15}, {-1, 7, -1, 12, -1, -1}, {7, 8, 11, 13, -1, -1}, {8, 9, 12, 14, -1, -1}, {9, 10, 13, 15, -1, -1}, {10, -1, 14, -1, -1, -1} }; struct Statue { int st; int num; int way[2][MAXN]; int cnt; Statue(int _st = (1<<16) - 1, int _num = 15) { st = _st; num = _num; cnt = 0; } }; queue
        
          q; set
         
           s; int n; int bfs() { s.clear(); while (!q.empty()) q.pop(); q.push(Statue(((1<<16)-1)^(1<<(n-1)), 14)); while (!q.empty()) { Statue tmp = q.front(); q.pop(); for (int i = 0; i < 15; i++) { if ((tmp.st>>i)&1) { for (int j = 0; j < 6; j++) { if (next[i][j] != -1 && ((tmp.st>>(next[i][j]-1))&1)) { int tt = i; Statue cur = tmp; while (tt >= 0 && (cur.st>>tt)&1) { cur.st -= (1<
          
           >(n-1))&1) { printf("%d\n%d %d", cur.cnt, cur.way[0][0], cur.way[1][0]); for (int i = 1; i < cur.cnt; i++) { printf(" %d %d", cur.way[0][i], cur.way[1][i]); } puts(""); return 0; } q.push(cur); } } } } } } printf("IMPOSSIBLE\n"); return 0; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); bfs(); } return 0; }
          
         
        
       
      
     
    
   
  



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉树前序、中序、后序遍历非递.. 下一篇UVA - 669 Defragment

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: