uva-10085-搜索-最远状态的八数码
直接bfs即可,取最后一个状态
#include <iostream>
#include <stdio.h>
#include <string>
#include <map>
#include <queue> using namespace std; struct Dir
{
int x, y;
}; struct Node
{
int x, y;
string str = "";
string steps = "";
};
char st[] = { 'U', 'D', 'L', 'R' };
Dir dir[] = { { , - }, { , }, { -, }, { , } }; queue<Node> q;
const int N = ;
string ans = "";
string ansStep = ""; int index(int x, int y)
{
return y * + x;
} void bfs(map<string, int> repeatMaps)
{
while (!q.empty())
{
Node node = q.front();
q.pop();
int curZero = index(node.x, node.y);
string curStr = node.str;
for(int i = ; i < ; i++)
{
int nx = node.x + dir[i].x;
int ny = node.y + dir[i].y;
if(nx < || ny < || nx > || ny > )
continue;
//nextstr
string nextStr(curStr);
swap(nextStr[index(nx, ny)], nextStr[curZero]);
if(repeatMaps.find(nextStr) == repeatMaps.end())
{
//未重复
Node n;
n.str = nextStr;
n.x = nx;
n.y = ny;
n.steps = node.steps + st[i];
q.push(n);
ans = nextStr;
ansStep = n.steps;
repeatMaps[nextStr] = ;
}
}
}
} int main()
{
freopen("d://1.text", "r", stdin);
//3x3图
//目标图
int n;
cin >> n;
int t =;
while (n--)
{
if(t!=)
cout<<endl;
string str = "";
int k;
int x, y;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
{
cin >> k;
str += k+'';
if(k == )
{
y = i;
x = j;
}
}
Node init;
init.steps = "";
init.str = str;
init.x = x;
init.y = y;
q.push(init);
map<string, int> repeatMaps;
repeatMaps[str] = ;
bfs(repeatMaps);
cout<<"Puzzle #"<<t<<endl;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(j!=)
{
cout<<" ";
}
cout<<ans[i*+j];
}
cout<<endl;
}
cout << ansStep << endl;
++t;
}
return ;
}
uva-10085-搜索-最远状态的八数码的更多相关文章
- HDU 1043 Eight 八数码问题 A*算法(经典问题)
HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...
- 八数码问题:C++广度搜索实现
毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...
- 关于八数码问题中的状态判重的三种解决方法(编码、hash、<set>)
八数码问题搜索有非常多高效方法:如A*算法.双向广搜等 但在搜索过程中都会遇到同一个问题.那就是判重操作(假设反复就剪枝),怎样高效的判重是8数码问题中效率的关键 以下关于几种判重方法进行比較:编码. ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
- HDU 1043 八数码(A*搜索)
在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...
- [luogu]P1379 八数码难题[广度优先搜索]
八数码难题 ——!x^n+y^n=z^n 我在此只说明此题的一种用BFS的方法,因为本人也是初学,勉勉强强写了一个单向的BFS,据说最快的是IDA*(然而蒟蒻我不会…) 各位如果想用IDA*的可以看看 ...
- codevs1225八数码难题(搜索·)
1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description Yours和zero在研究A*启 ...
- A*算法 -- 八数码问题和传教士过河问题的代码实现
前段时间人工智能的课介绍到A*算法,于是便去了解了一下,然后试着用这个算法去解决经典的八数码问题,一开始写用了挺久时间的,后来试着把算法的框架抽离出来,编写成一个通用的算法模板,这样子如果以后需要用到 ...
- ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...
随机推荐
- 编译小米mini openwrt
编译小米mini openwrt 1.下载openwrt源码包:进入要下载保存的文件目录,输入 git clone git://git.openwrt.org/15.05/openwrt.git 2. ...
- 阅读<<HDMI 1.4/2.0 Transmitter Subsystem V2.0>>笔记
阅读<<HDMI 1.4/2.0 Transmitter Subsystem V2.0>>笔记 1.Subsystem Block Diagram 2.HDMI TX Subs ...
- golang GBK与UTF-8互转的例子
package main import ( "code.google.com/p/mahonia" "fmt" ) func main() { //" ...
- JS一行代码,生成一个16进制随机颜色,简单粗暴。
var color = '#'+ Math.random().toString(16).substr(-6); document.body.style.backgroundColor = color; ...
- ALGO-14_蓝桥杯_算法训练_回文数
问题描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...
- gulp学习总结
一.gulp使用-博客推荐: http://www.sheyilin.com/2016/02/gulp_introduce/ 二.gulp的作用 gulp是一个前端构建工具,它是一个工具框架,可以通过 ...
- P2430严酷的训练
传送 这个题的题干很长,长到令人恶心 这个题的p乍一看好像没有卵用,但其实他很有用(废话).这里的“费用”不再是tw[i](wky做第i道题的时间),而是tw[p[i]](wky做第i道题所对应的知识 ...
- hive命令的3种调用方式
方式1:hive –f /root/shell/hive-script.sql(适合多语句) hive-script.sql类似于script一样,直接写查询命令就行 例如: [root@cloud ...
- [转][C#]降级.net 源码4.5
来自:https://www.cnblogs.com/had37210/p/8057042.html 主要是 Task 的降级: 1.net4.5.2 引入了 async/await 关键字. 这个其 ...
- 找出N个无序数中第K大的数
使用类似快速排序,执行一次快速排序后,每次只选择一部分继续执行快速排序,直到找到第K个大元素为止,此时这个元素在数组位置后面的元素即所求 时间复杂度: 1.若随机选取枢纽,线性期望时间O(N) 2.若 ...