【例题 6-14 UVA-816】Abbott's Revenge
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
预处理出某个方向的左边、前边、右边是哪个方向就好了。
然后就是普通的bfs了。
hash存到某个点,走到这里的方向的最小距离。
dfs输出路径。
【代码】
#include <bits/stdc++.h>
using namespace std;
//[where in,point]minimum dis
//0 up 1 down 2 left 3 right
const int dx[4] = { -1,1,0,0 };
const int dy[4] = { 0,0,-1,1 };
const int N = 9;
const int INF = 0x3f3f3f3f;
struct abc {
int dir, x, y;
};
int bo[N + 5][N + 5][4], xx1, yy1, x2, y2;
int dire[300], newdire[4][300];
int h, t, pre[N*N * 5];
vector <pair<int, pair<int, int> > > v[N + 5][N + 5][4];
vector <pair<int, int> > ans;
abc dl[N*N * 5];
void wujie() {
puts("");
puts(" No Solution Possible");
}
bool bfs(int x, int y, int fx) {
h = 0, t = 1;
memset(bo, INF, sizeof bo);
memset(pre, 0, sizeof pre);
dl[1].dir = fx; dl[1].x = x; dl[1].y = y;
bo[x][y][fx] = 1;
if (x==x2 && y == y2) return true;
while (h < t) {
h++;
int tx = dl[h].x, ty = dl[h].y, before = dl[h].dir;
for (auto temp : v[tx][ty][before]) {
int xx = tx + temp.second.first, yy = ty + temp.second.second;
if (xx < 1 || xx > 9 || yy <1 || yy > 9) continue;
if (bo[xx][yy][temp.first]>bo[tx][ty][before] + 1) {
bo[xx][yy][temp.first] = bo[tx][ty][before] + 1;
t++;
dl[t].x = xx, dl[t].y = yy, dl[t].dir = temp.first;
pre[t] = h;
if (xx == x2 && yy == y2) return true;
}
}
}
return false;
}
void dfs(int now) {
if (now == 1) {
ans.push_back(make_pair(xx1, yy1));
ans.push_back(make_pair(dl[now].x, dl[now].y));
return;
}
dfs(pre[now]);
ans.push_back(make_pair(dl[now].x, dl[now].y));
}
int main() {
/* freopen("rush.txt","r",stdin);
freopen("rush_out.txt","w",stdout);
*/
dire['N'] = 0, dire['S'] = 1, dire['W'] = 2, dire['E'] = 3;
newdire[0]['L'] = 2, newdire[0]['F'] = 0, newdire[0]['R'] = 3;
newdire[1]['L'] = 3, newdire[1]['R'] = 2, newdire[1]['F'] = 1;
newdire[2]['L'] = 1, newdire[2]['R'] = 0, newdire[2]['F'] = 2;
newdire[3]['L'] = 0, newdire[3]['R'] = 1, newdire[3]['F'] = 3;
string T;
while (cin >> T && T != "END") {
for (int i = 1; i <= 9; i++)
for (int j = 1; j <= 9; j++)
for (int k = 0; k < 4; k++)
v[i][j][k].clear();
scanf("%d%d", &xx1, &yy1);
char s[5];
scanf("%s", s);
scanf("%d%d", &x2, &y2);
int x, y;
while (scanf("%d", &x) && x) {
scanf("%d", &y);
char temp[5];
while (~scanf("%s", temp) && temp[0] != '*') {
int from = dire[temp[0]];
int len = strlen(temp);
for (int i = 1; i < len; i++) {
int temp1 = newdire[from][temp[i]];
v[x][y][from].push_back(make_pair(temp1, make_pair(dx[temp1], dy[temp1])));
}
}
}
int dir1 = dire[s[0]];
int tx = xx1 + dx[dir1], ty = yy1 + dy[dir1];
cout << T;
if (tx < 1 || tx > 9 || ty < 1 || ty > 9 || !bfs(tx, ty, dir1)) {
wujie();
}
else {
ans.clear();
dfs(t);
bool first;
for (int i = 0; i < (int)ans.size(); i++) {
if (i % 10 == 0) {
puts(""); printf(" ");
first = true;
}
if (!first) putchar(' ');
first = false;
printf("(%d,%d)", ans[i].first, ans[i].second);
}
puts("");
}
}
return 0;
}
【例题 6-14 UVA-816】Abbott's Revenge的更多相关文章
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- uva 816 abbott's revenge ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r
- UVA 816 Abbott’s Revenge
bfs求最短路,递归打印最短路的具体路径: 难点: 当前状态和转弯方式很复杂,要仔细处理: 递归打印:用一个数组存储路径中结点的前一个节点,递归查找 (bfs无法确定下一个结点,但对于没一个结点,它的 ...
- Uva - 816 - Abbott's Revenge
这个迷宫问题还是挺好玩的,多加了一个转向的问题,有些路口不同的进入方式会有不同的转向限制,这个会比较麻烦一点,所以定义结点结构体的时候需要加一个朝向dir.总体来说是一道BFS求最短路的问题.最后打印 ...
- Uva 816 Abbott's Revenge(BFS)
#include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...
- UVA 816 Abbott's Revenge 紫书
紫书的这道题, 作者说是很重要. 但看着题解好长, 加上那段时间有别的事, 磨了几天没有动手. 最后,这道题我打了五遍以上 ,有两次被BUG卡了,找了很久才找到. 思路紫书上有,就缺少输入和边界判断两 ...
- UVA - 816 Abbott's Revenge(bfs)
题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...
- UVA 816 - Abbott's Revenge(BFS)
UVA 816 - Abbott's Revenge option=com_onlinejudge&Itemid=8&page=show_problem&category=59 ...
- UVa (一道比较复杂的广搜) 816 Abbott’s Revenge
题意: 给出一个迷宫,在迷宫的节点处,面向某个方向只能向给定的方向转弯.给出起点和终点输出迷宫的最短路径,这里指的是刚刚离开起点的时刻,所以即使起点和终点重合路径也非空. 分析: 用三个变量来表示状态 ...
- uva 816 - Abbott's Revenge(有点困难bfs迷宫称号)
是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...
随机推荐
- Qt源码编译
Qt源码编译 eryar@163.com Key words. Qt, 源码编译 1.Introduction 随着Qt版本升级,源码编译出来的库体积越来越大.如果只是用Qt来做GUI,Qt提供的预编 ...
- 比MD5 和HMAC还要安全的加密 - MD5 加时间戳
//1.给一个字符串进行MD5加密 NSString *passKey = @"myapp"; passKey = [passKey md5String]; //2.对第一步中得到 ...
- tomcat 分别在window 和 Linux上配置SSL-安全问题
公司项目收尾后.通过压力測试后的安全測试.安全測试后中,对于网络传输中数据加密问题存在安全隐患. 须要配置SSL. 简介下SSL协议:SSL或者Secure Socket Layer,是一种同意web ...
- ssh框架的总结
一.spring:是基础,可以管理对象,也可以通过关键对象管理另一个框架.但是首先应该明确spring并不是只能应用于web方面,而是可以应用在一般的java项目中.只是如果在web环境下使用需要在w ...
- 转 SQL集合函数中利用case when then 技巧
SQL集合函数中利用case when then 技巧 我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then '男' ...
- java中的九大隐藏变量.
javax.servlet.jsp.JspWriter类型,代表输出流的对象.作用域为page(页面执行期) request:javax.servlet.ServletRequest的子类 ...
- 用Vue+axios写一个实时搜索
刚刚在学vue,试着写了一个实时搜索文件. 思路:1.input 通过v-model绑定.2.通过watch检测输入结果变化.3根据结果变化从api调用不同的数据. 代码如下: <!DOCTYP ...
- pgrep---以名称为依据从运行进程队列中查找进程
pgrep命令以名称为依据从运行进程队列中查找进程,并显示查找到的进程id.每一个进程ID以一个十进制数表示,通过一个分割字符串和下一个ID分开,默认的分割字符串是一个新行.对于每个属性选项,用户可以 ...
- PatentTips - Transparent unification of virtual machines
BACKGROUND Virtualization technology enables a single host computer running a virtual machine monito ...
- HDU——T 1573 X问题
http://acm.hdu.edu.cn/showproblem.php?pid=1573 Time Limit: 1000/1000 MS (Java/Others) Memory Limi ...