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圆的点就行,用点到原点距离与半径比较,当格子左下方和右上方都在格子里时,格子在 ...
随机推荐
- Maven(一)如何用Eclipse创建一个Maven项目
1.什么是Maven Apache Maven 是一个项目管理和整合工具.基于工程对象模型(POM)的概念,通过一个中央信息管理模块,Maven 能够管理项目的构建.报告和文档. Maven工程结构和 ...
- MongoDB ----基于分布式文件存储的数据库
参考: http://www.cnblogs.com/huangxincheng/category/355399.html http://www.cnblogs.com/daizhj/category ...
- INNODB引擎概述
INNODB存储引擎的历史概述: INNODB存储引擎是OLTP应用中核心表的首选存储引擎. INNODB存储引擎包含在所有mysql数据库的二进制发行版本中.早期其版本随着mysql数据库的更新而更 ...
- SQL学习之SQL注入学习总结
所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 测试数据库 我们本文就以如下数据库作为测试数据库,完成我们的注入分析 ...
- Linux 安装JDK(jdk-8u121-linux-x64.tar.gz)
Linux 安装JDK(jdk-8u121-linux-x64.tar.gz) 一.JDK准备 1.1 文件名称 jdk-8u121-linux-x64.tar.gz 1.2 下载地址 http:// ...
- troubleshooting-执行导数shell脚本抛异常error=2, No such file or directory
Cannot run program "order_log.sh" (in directory "/data/yarn/nm/usercache/chenweidong/ ...
- MemcacheQ安装
一.memcacheq介绍 特性: 1.简单易用 2.处理速度快 3.多条队列 4.并发性能好 5.与memcache的协议兼容 6.在zend framework中使用方便 memcacheq依赖于 ...
- 20145302张薇 《网络对抗技术》 web基础
20145302张薇 <网络对抗> web基础 实验问题回答 1.什么是表单 表单在网页中主要负责数据采集功能:一般网页上需要用户输入.选择的地方都会用到表单 表单标签:即,用于确定表单所 ...
- Code First技术介绍
地址:https://wenku.baidu.com/view/5620b862eefdc8d376ee3258.html 仅供参考
- 史丰收速算|2014年蓝桥杯B组题解析第四题-fishers
史丰收速算 史丰收速算法的革命性贡献是:从高位算起,预测进位.不需要九九表,彻底颠覆了传统手算! 速算的核心基础是:1位数乘以多位数的乘法. 其中,乘以7是最复杂的,就以它为例. 因为,1/7 是个循 ...