Description

 

Venture MFG Company, Inc. has made a game board. This game board has 15 holes and these holes are filled with pegs except one hole. A peg can jump over one or more consecutive peg s to the nearest empty hole along the straight line. As a peg jump over the pegs you remove them from the board. In the following figure, the peg at the hole number 12 or the peg at the hole number 14 can jump to the empty hole number 5. If the peg at the hole number 12 is moved then the peg at the hole number 8 is removed. Instead, if the peg at the hole number 14 is moved then the peg at the hole number 9 is removed.

                    

 

Write a program which find a shortest sequence of moving pegs to leave the last peg in the hole that was initially empty. If such a sequence does not exist the program should write a message ``IMPOSSIBLE".

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case is a single integer which means an empty hole number.

Output

For each test case, the first line of the output file contains an integer which is the number of jumps in a shortest sequence of moving pegs. In the second line of the output file, print a sequence of peg movements. A peg movement consists of a pair o f integers separated by a space. The first integer of the pair denotes the hole number of the peg that is moving, and the second integer denotes a destination (empty) hole number.

If there are multiple solutions, output the lexicographically smallest one.

Sample Input

1
5

Sample Output

10
12 5 3 8 15 12 6 13 7 9 1 7 10 8 7 9 11 14 14 5 题目大意:在如图中的棋盘(固定5行)上,每个棋子的走法类似于象棋中“炮”的走法,只能隔着棋子沿直线走,每走一步造成的效果是该棋子落到第一个空白处,并且沿途经过的棋子全部消失。求使最后一个棋子恰好落在第n个点上的最短、字典序最小的路径。
题目分析:这道题说白了有15个位置,每个位置上可能有棋子也可能没有棋子,棋子的状况总共有2^15种。起点是(2^15)-1,终点是1<<(n-1),BFS即可,状态转移也不难,但比较复杂。 代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<map>
# include<cmath>
# include<string>
# include<cstring>
# include<algorithm> using namespace std; const int tot=(1<<15)-1; struct node
{
int s,t;
string step;
node(int _s,int _t,string _step):s(_s),t(_t),step(_step){}
bool operator < (const node &a) const {
if(t==a.t)
return step>a.step;
return t>a.t;
}
}; int mark[1<<15];
map<int,char>mp;
int d[6][2]={{-1,-1},{-1,0},{0,-1},{0,1},{1,0},{1,1}}; int get_pos(int x,int y)
{
return x*(x-1)/2+y;
} void get_XY(int n,int &x,int &y)
{
x=1;
for(int i=1;i<=5&&n-i>0;++i)
++x,n-=i;
y=n;
} bool ok(int x,int y)
{
if(x>=1&&x<=5&&y>=1&&y<=x)
return true;
return false;
} void print(string p)
{
for(int i=0;i<p.size();++i)
printf("%d%c",p[i]-'A'+1,(i==p.size()-1)?'\n':' ');
} void bfs(int goal)
{
priority_queue<node>q;
memset(mark,0,sizeof(mark));
mark[tot^goal]=1;
q.push(node(tot^goal,0,""));
while(!q.empty())
{
node u=q.top();
q.pop();
//cout<<u.t<<' '<<u.s<<' '<<u.step<<endl;
if(u.s==goal){
printf("%d\n",u.t);
print(u.step);
return ;
}
int x,y;
for(int i=1;i<=15;++i){
if(u.s&(1<<(i-1))){
get_XY(i,x,y);
for(int j=0;j<6;++j){
int nx=x+d[j][0],ny=y+d[j][1];
if(!ok(nx,ny))
continue;
int pos=get_pos(nx,ny);
if(!(u.s&(1<<(pos-1))))
continue;
int s=u.s^(1<<(i-1));
while(u.s&(1<<(pos-1)))
{
s^=(1<<(pos-1));
nx=nx+d[j][0],ny=ny+d[j][1];
if(!ok(nx,ny))
break;
pos=get_pos(nx,ny);
}
s^=(1<<(pos-1));
string step=u.step+mp[i];
step+=mp[get_pos(nx,ny)];
if(ok(nx,ny)&&!mark[s]){
mark[s]=1;
q.push(node(s,u.t+1,step));
}
}
}
}
}
printf("IMPOSSIBLE\n");
} int main()
{
for(int i=1;i<=15;++i)
mp[i]=i+'A'-1;
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
bfs(1<<(n-1));
}
return 0;
}

  

UVA-1533 Moving Pegs (路径寻找问题)的更多相关文章

  1. 八数码问题+路径寻找问题+bfs(隐式图的判重操作)

    Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...

  2. 基于JavaFX图形界面演示的迷宫创建与路径寻找

    事情的起因是收到了一位网友的请求,他的java课设需要设计实现迷宫相关的程序--如标题概括. 我这边不方便透露相关信息,就只把任务要求写出来. 演示视频指路: 视频过审后就更新链接 完整代码链接: 网 ...

  3. 【路径寻找问题】UVa 10603 - Fill

    如家大神书上的例题.第一次接触也是按代码敲得.敲的过程感觉很直观.但自己写估计会写的乱七八糟.以后不能砍得难就不愿意做这种题.否则只能做一些水题了.(PS:48) 紫书 #include<ios ...

  4. Uva 10054 欧拉回路 打印路径

    看是否有欧拉回路 有的话打印路径 欧拉回路存在的条件: 如果是有向图的话 1.底图必须是连通图 2.最多有两个点的入度不等于出度 且一个点的入度=出度+1 一个点的入度=出度-1 如果是无向图的话 1 ...

  5. Python解释器路径寻找规则

    Python编辑器路径寻址总结 Python编程优化 这场表演邀请了三位角色:run.sh.main.py.path.sh,拍摄场地选在了 Windows -> Git Bash 群演1号 ru ...

  6. Fill-倒水问题(Uva-10603-隐式图路径寻找问题)

    原题:https://uva.onlinejudge.org/external/106/10603.pdf 有三个没有刻度的杯子,它们的容量分别是a, b, c, 最初只有c中的杯子装满水,其他的被子 ...

  7. What Goes Up UVA - 481 LIS+打印路径 【模板】

    打印严格上升子序列: #include<iostream> #include<cstdio> #include<algorithm> #include<cst ...

  8. 【DP】UVA 624 CD 记录路径

    开一个数组p 若dp[i-1][j]<dp[i-1][j-a[i]]+a[i]时就记录下p[j]=a[i];表示此时放进一个轨道 递归输出p #include <stdio.h> # ...

  9. UVA 356 - Square Pegs And Round Holes

    题目:在一个2n*2n的网格中间画一个直径为2n-1的圆,问圆内部的格子以及和圆相交的格子个数. 思路:只要考虑1 / 4圆的点就行,用点到原点距离与半径比较,当格子左下方和右上方都在格子里时,格子在 ...

随机推荐

  1. C/C++之标准库和标准模板库

    C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完 成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花 ...

  2. (四)github之Git的初始设置

    设置姓名与邮箱地址 这里的姓名和邮箱地址会用在git的提交日志之中,在github上公开git仓库时会随着提交日志一起公开. 有两种方式, 第一种,在git bash下设置 第二种, 通过直接编辑.g ...

  3. Python Web笔记之高性能网络编程

    请参考博客: https://blog.csdn.net/russell_tao/article/details/9111769

  4. 来了解一下Ajax是什么?Ajax的原理?Ajax与传统Web比较?Ajax的优缺点?Ajax的Post与Get比较

    一.什么是Ajax Ajax(Asynchronous Java and XML的缩写)是一种异步请求数据的web开发技术,对于改善用户的体验和页面性能很有帮助.简单地说,在不需要重新刷新页面的情况下 ...

  5. C_Learning(3)

    / 结构体 / 声明结构体类型的一般形式: struct 结构体名[--表示的是这个结构体的类型] {    成员列表 }; [不要漏掉这个";"] / 声明结构可以放在main函 ...

  6. 20135234mqy-——信息安全系统设计基础第十一周学习总结

    第八章 异常控制流 8.1异常 异常是异常控制流的一种形式,它一部分是由硬件实现的,一部分是由操作系统实现的. 异常就是控制流中的突变,用来响应处理器状态中的某些变化. 8.1.1异常处理 异常号是到 ...

  7. 高通平台读写nv总结【转】

    本文转载自:https://blog.csdn.net/suofeng12345/article/details/52713993 一,引言      1. 什么是NV       高通平台的NV,保 ...

  8. luogu3935 Calculating

    标题也许叫整除分块吧 求\(1\)到\(n\)因数的个数\(\sum_{i=1}^n(\sum_{d|n}1)\) 范围\(1e14\)时限3s \(n\sqrt{n}\)的暴力铁定gg 分开考虑 \ ...

  9. sql:临时表和表变量

    在SQL Server的性能调优中,有一个不可比拟的问题:那就是如何在一段需要长时间的代码或被频繁调用的代码中处理临时数据集,表变量和临时表是两种选择. 临时表: 临时对象都以#或##为前缀,临时表是 ...

  10. MOOC_Java进阶_翁恺讲_第三周题

    package mooc_java进阶_d3周题; /** * 没有使用HashMap */ import java.util.ArrayList; import java.util.Scanner; ...