UVA-1533 Moving Pegs (路径寻找问题)
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 (路径寻找问题)的更多相关文章
- 八数码问题+路径寻找问题+bfs(隐式图的判重操作)
Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...
- 基于JavaFX图形界面演示的迷宫创建与路径寻找
事情的起因是收到了一位网友的请求,他的java课设需要设计实现迷宫相关的程序--如标题概括. 我这边不方便透露相关信息,就只把任务要求写出来. 演示视频指路: 视频过审后就更新链接 完整代码链接: 网 ...
- 【路径寻找问题】UVa 10603 - Fill
如家大神书上的例题.第一次接触也是按代码敲得.敲的过程感觉很直观.但自己写估计会写的乱七八糟.以后不能砍得难就不愿意做这种题.否则只能做一些水题了.(PS:48) 紫书 #include<ios ...
- Uva 10054 欧拉回路 打印路径
看是否有欧拉回路 有的话打印路径 欧拉回路存在的条件: 如果是有向图的话 1.底图必须是连通图 2.最多有两个点的入度不等于出度 且一个点的入度=出度+1 一个点的入度=出度-1 如果是无向图的话 1 ...
- Python解释器路径寻找规则
Python编辑器路径寻址总结 Python编程优化 这场表演邀请了三位角色:run.sh.main.py.path.sh,拍摄场地选在了 Windows -> Git Bash 群演1号 ru ...
- Fill-倒水问题(Uva-10603-隐式图路径寻找问题)
原题:https://uva.onlinejudge.org/external/106/10603.pdf 有三个没有刻度的杯子,它们的容量分别是a, b, c, 最初只有c中的杯子装满水,其他的被子 ...
- What Goes Up UVA - 481 LIS+打印路径 【模板】
打印严格上升子序列: #include<iostream> #include<cstdio> #include<algorithm> #include<cst ...
- 【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> # ...
- UVA 356 - Square Pegs And Round Holes
题目:在一个2n*2n的网格中间画一个直径为2n-1的圆,问圆内部的格子以及和圆相交的格子个数. 思路:只要考虑1 / 4圆的点就行,用点到原点距离与半径比较,当格子左下方和右上方都在格子里时,格子在 ...
随机推荐
- x86,x64,Any CPU区别
https://blog.csdn.net/zuguangboy/article/details/51509670 1,即主程序(编译出来是exe文件的)是x86平台下编译的,而它所依赖的一个项目(或 ...
- SQL学习之Can't connect to MySQL server on localhost (10061)
最近升级 了系统,开机后连接MySQL报错,Can't connect to MySQL server on localhost (10061): 估计是升级系统清除了以前的缓存设置,网上很多方法是命 ...
- c++编程和c在思想上最大的差别
从正规的血统上来说,c++跟java一样是面向对象编程语言,而c是面向结构的编程语言.但是,在现实中,有大量的系统虽然用cpp编写,但是100%跟java一样,使用面向对象的几乎没有,绝大部分都是结合 ...
- 20145310《网络对抗》Exp9 Web安全基础实践
基础问题回答 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,通过执行SQL语句进执行攻击者所要的操作. 如何防御?首先严格区分 ...
- 《将博客搬至51CTO》
想把你的博客搬家到51CTO吗?想拥有一键式搬家的体验吗? 就算家大业大不好搬也没关系,我们帮你! 51CTO推出专业的搬家工具,用最短的时间.最快的速度,为你在这儿搭建一个温馨的家. 在这儿,你可以 ...
- 使用Oracle执行计划分析SQL性能
执行计划:一条查询语句在ORACLE中的执行过程或访问路径的描述.即就是对一个查询任务,做出一份怎样去完成任务的详细方案. 如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQL的 ...
- Python3基础 str lstrip 去掉字符串左边的空格
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- DDMS files not found
在eclipse中启动新建的android项目的时候,控制台提示如图: 方法1.cmd中adb kill-server,然后adb -startserver 方法2.方法1不管用,那么在任务管理器中杀 ...
- vi如何修改注释颜色
答:往~/.vimrc或/etc/vimrc的最后添加以下行: hi comment ctermfg=6
- react 为元素添加自定义事件监听器
https://zhenyong.github.io/react/tips/dom-event-listeners.html class MovieItem extends React.Compone ...