【例题 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> ...
随机推荐
- 漫漫人生路-学点Jakarta基础-Java8新特性 Stream/Lambda
背景 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利.高效的聚合操作(aggregate operation),或者大批量数据操作 (bulk dat ...
- Ajax的跨域问题分析
一.Ajax的跨域问题 Ajax是利用javascript内置XMLHttpRequest对象来进行传输的,所以它依赖于XMLHttpRequest对象,而XMLHttpRequest对象却有很多的限 ...
- java高质量缩放图片
可按照比例缩放,也可以指定宽高 import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JP ...
- hash_set和hash_map
1.hash_set集合容器 hash_set利用链式哈希表,进行数据的插入.删除和搜索.与set容器同样,不同意插入反复键值的元素.SGIC++哈希表是一个链式的结构,由表头和一系列单链组成.表头是 ...
- bug 7715339 登录失败触发 ‘row cache lock’ 等待
Bug 7715339 - Logon failures causes "row cache lock" waits - Allow disable of logon delay ...
- CSS3:元素的边框、背景和大小
边框 和边框相关的属性例如以下. border-width 用于设置边框的宽度,可选择包含: 1)<长度值>:将边框宽度设为以CSS度量单位(如em.px.cm)表达的长度值. 2)< ...
- 重建一些被PHP7废弃的函数,
<?php if(!function_exists('ereg')) { function ereg($pattern, $subject, &$matches = []) { retu ...
- 11lession-class 类
python既然也是面向对象编程的语言,自然也就跟java相似,它也有类的概念.今天就简单学习下.看如下代码 #!/usr/bin/python class cl_test: test = 0 def ...
- 如果输入的不是英文字母或者数字或者汉字,则返回false
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_C ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...