题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1026

题目大意:就是说给你一个8数码,问你能够达到的距离最远的状态是什么。

刚开始以为只要顺着一边走就行了,后来发现这样走可能最远到达的状态也能通过其他方式到达。

在这里WA了好多次。

应该把所有可能出现的情况全都枚举出来,然后判重。。

UVA也真是慢。。4s的代码跑了快4分钟。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <string>
using namespace std;
typedef unsigned long long ull; const int B = ; struct Node{
int status[][];
int h;
int x,y;
string route;
}; int T;
int dir[][] = {{,},{-,},{,},{,-}}; int main(){
scanf("%d",&T);
for(int t=;t<=T;t++){
Node s;
s.h = ;
set<int> se;
for(int i=;i<;i++){
for(int j=;j<;j++){
scanf("%d",&s.status[i][j]);
s.h = s.h*+s.status[i][j];
if( s.status[i][j] == ){
s.x = i; s.y = j;
}
}
}
se.insert(s.h);
// printf("%d\n",s.h);
s.route = "";
queue<Node> q;
q.push(s);
Node ans = s;
bool fl = true;
while(!q.empty()){
Node tn = q.front(); q.pop();
if( fl || ans.route.size()<tn.route.size()) ans = tn;
for(int i=;i<;i++){
Node t = tn;
int dx = t.x+dir[i][], dy = t.y+dir[i][];
if( dx<=&&dx>=&&dy<=&&dy>= ){
swap(t.status[tn.x][tn.y],t.status[dx][dy]);
t.x = dx; t.y = dy;
int tt = ;
for(int i=;i<;i++){
for(int j=;j<;j++){
tt = tt*B+t.status[i][j];
}
}
t.h = tt; if( i== ) t.route = tn.route+"D";
else if( i== ) t.route = tn.route+"U";
else if( i== ) t.route = tn.route+"R";
else if( i== ) t.route = tn.route+"L";
if( se.find(tt)==se.end() ){
se.insert(tt);
q.push(t);
}
}
}
}
printf("Puzzle #%d\n",t);
for(int i=;i<;i++) for(int j=;j<;j++){
printf(j==?"%d\n":"%d ",ans.status[i][j]);
}
puts(ans.route.c_str());
puts("");
}
return ;
}

[Uva 10085] The most distant state (BFS)的更多相关文章

  1. UVa 439骑士的移动(BFS)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. UVa 816 Abbott的复仇(BFS)

    寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同, ...

  4. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

  5. UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)

    题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...

  6. UVa 1599 理想路径(反向BFS 求最短路径 )

    题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...

  7. UVA - 816 Abbott's Revenge(bfs)

    题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...

  8. UVA 1599 Ideal Path(双向bfs+字典序+非简单图的最短路+队列判重)

    https://vjudge.net/problem/UVA-1599 给一个n个点m条边(2<=n<=100000,1<=m<=200000)的无向图,每条边上都涂有一种颜色 ...

  9. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

随机推荐

  1. 百度ue富文本编辑器setContent方法报错初始化加载内容失败解决办法

    解决方案: 不能创建editor之后马上使用ueditor.setContent('文本内容');要等到创建完成之后才可以使用 ueditor.addListener("ready" ...

  2. 【java】 java 实现mysql备份

    使用java实现mysql的备份: public class MySQLBackUp { /** * Java代码实现MySQL数据库导出 * * @author GaoHuanjie * @para ...

  3. asp.net mvc 页面传值的方法总结

    转自:http://msprogrammer.serviciipeweb.ro/2012/01/15/usual-methods-to-transfer-data-from-page-to-page- ...

  4. css 默认

    先写全局的样式 body { margin:0 auto; font-size:12px; font-family:Verdana; line-height:1.5;} ul,dl,dd,h1,h2, ...

  5. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  6. android学习笔记19——对话框(DatePickerDialog、TimePickerDialog)

    DatePickerDialog.TimePickerDialog ==> DatePickerDialog.TimePickerDialog功能.用法都比较简单,操作步骤: 1.通过new关键 ...

  7. VBA标准模块与类模块

    大家通过之前的介绍,已知道怎么将一个空模块插入VBA的工程中.从插入模块中可以看到,模块有有两种——标准模块与类模块.类模块是含有类定义的特殊模块,包括其属性和方法的定义.在后面会有介绍与说明. 随着 ...

  8. 黄聪:MYSQL5.6缓存性能优化my.ini文件配置方案

    使用MYSQL版本:5.6 [client] …… default-character-set=gbk default-storage-engine=MYISAM max_connections=10 ...

  9. SDS查看部署在集成TOMCAT服务器中的项目目录结构

  10. linux命令(5)文件操作:ls命令、显示文件总个数

    一:ls命令是最常用的linux命令了:下面是ls --help里面的用法 在提示符下输入ls --help ,屏幕会显示该命令的使用格式及参数信息: 先介绍一下ls命令的主要参数: -a 列出目录下 ...