【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

预处理出某个方向的左边、前边、右边是哪个方向就好了。
然后就是普通的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的更多相关文章

  1. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

  2. uva 816 abbott's revenge ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r

  3. UVA 816 Abbott’s Revenge

    bfs求最短路,递归打印最短路的具体路径: 难点: 当前状态和转弯方式很复杂,要仔细处理: 递归打印:用一个数组存储路径中结点的前一个节点,递归查找 (bfs无法确定下一个结点,但对于没一个结点,它的 ...

  4. Uva - 816 - Abbott's Revenge

    这个迷宫问题还是挺好玩的,多加了一个转向的问题,有些路口不同的进入方式会有不同的转向限制,这个会比较麻烦一点,所以定义结点结构体的时候需要加一个朝向dir.总体来说是一道BFS求最短路的问题.最后打印 ...

  5. Uva 816 Abbott's Revenge(BFS)

    #include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...

  6. UVA 816 Abbott's Revenge 紫书

    紫书的这道题, 作者说是很重要. 但看着题解好长, 加上那段时间有别的事, 磨了几天没有动手. 最后,这道题我打了五遍以上 ,有两次被BUG卡了,找了很久才找到. 思路紫书上有,就缺少输入和边界判断两 ...

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

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

  8. UVA 816 - Abbott&#39;s Revenge(BFS)

    UVA 816 - Abbott's Revenge option=com_onlinejudge&Itemid=8&page=show_problem&category=59 ...

  9. UVa (一道比较复杂的广搜) 816 Abbott’s Revenge

    题意: 给出一个迷宫,在迷宫的节点处,面向某个方向只能向给定的方向转弯.给出起点和终点输出迷宫的最短路径,这里指的是刚刚离开起点的时刻,所以即使起点和终点重合路径也非空. 分析: 用三个变量来表示状态 ...

  10. uva 816 - Abbott&#39;s Revenge(有点困难bfs迷宫称号)

    是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...

随机推荐

  1. Floyd-Warshall 算法-- 最短路径(适合节点密集的图)

     由于此算法时间复杂度为O(V³).大多数情况下不如迪杰斯特拉算法的.迪杰斯特拉算法适合于节点疏散的图.  演示样例图例如以下:     Step 1 创建节点与边的最短路径结果表(直接可达关系).数 ...

  2. 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】

    背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该 ...

  3. 具有可视化的功能的一款开源软件Gource

    今天为大家介绍一个非常有趣儿的开源软件,Gource可以将代码版本控制系统里面的日志全部可视化,也就是说可以看见每个成员在系统里面提交代码的行为,Gource目前支持git,hg,svn. 650) ...

  4. golang binarySearch

    func binarySearch(nodes []*node, word Text) (int, bool) { start := end := len(nodes) - // 特例: { // 当 ...

  5. 利用日志使管理Linux更轻松

    利用日志使管理Linux更轻松 操作系统的日志主要具有审计与监测的功能,通过对日志信息的分析,可以检查错误发生的原因,监测追踪入侵者及受到攻击时留下的痕迹,甚至还能实时的进行系统状态的监控.有效利用日 ...

  6. easyui树查找

    前端查询 /* 树查询*/ function searchMaterial(){ var parentNode=$('#selectMaterialTree').tree('getRoots'); / ...

  7. 推广一下新Blog www.hrwhisper.me

    新博客地址:www.hrwhisper.me 欢迎互访加友链~

  8. secureCRT The remote system refused the connection问题解决

    问题: Ubuntu系统必须开启ssh服务后,XP或者其它的主机才干够远程登陆到Ubuntu系统. 1,安装软件包,运行sudo apt-get install openssh-server Ubun ...

  9. hiho 1182 : 欧拉路&#183;三

    1182 : 欧拉路·三 这时题目中给的提示: 小Ho:是这种.每次转动一个区域不是相当于原来数字去掉最左边一位,并在最后加上1或者0么. 于是我考虑对于"XYYY",它转动之后能 ...

  10. .NET中StringBuilder用法实例分析

    string s1 = "33"; string s2 = "44"; string s3 = "55"; //需求是把s1 s2 s3拼接 ...