[Uva 10085] The most distant state (BFS)
题目链接: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)的更多相关文章
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 816 Abbott的复仇(BFS)
寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同, ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
- UVa 1599 理想路径(反向BFS 求最短路径 )
题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...
- UVA - 816 Abbott's Revenge(bfs)
题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...
- UVA 1599 Ideal Path(双向bfs+字典序+非简单图的最短路+队列判重)
https://vjudge.net/problem/UVA-1599 给一个n个点m条边(2<=n<=100000,1<=m<=200000)的无向图,每条边上都涂有一种颜色 ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
随机推荐
- JavaScript:改变li前缀图片和样式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- winrar 5.21去广告
http://www.rarlab.com/ 把下面的数据复制到“记事本”中,用文件名“rarreg.key搜索”命名该文件,保存到winrar安装文件夹即完成注册 RAR registration ...
- HTTP API 设计指南(中文版) restfull
http://www.css88.com/archives/5121 目录 基础 总是使用TLS 在Accepts头中带上版本号 通过Etags支持缓存 用Request-Ids追踪请求 用Range ...
- Spring 注解总结
声明:这是转载的.内容根据网上资料整理.相关链接:http://www.360doc.com/content/10/1118/16/2371584_70449913.shtmlhttp://www.i ...
- 多线程编程之Linux环境下的多线程(一)
一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...
- 程序员书单_HeadFirst系列
Head First Jquery(中文版),完整扫描版 http://download.csdn.net/detail/shenzhq1980/9103615 Head First Python(完 ...
- winform 承载 WCF 注意,可能不是工作在多线程模式下
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMo ...
- (C#) Parse xml 时, 返回的node值总是null。
网上查了一下,原因在于要parse的Xml文件本身包含了一些namespace,这些需要被添加进去. http://msdn.microsoft.com/zh-cn/library/system.xm ...
- Spark运行流程概述
Application 指用户编写的Spark应用程序,其中包含了一个Driver功能的代码和分布在集群中多个节点上运行的Executor代码. Driver Spark中的Driver即运行上述Ap ...
- PLSQL_闪回操作2_Fashback Version Query
2014-12-09 Created By BaoXinjian