【例题 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> ...
随机推荐
- 洛谷 P2614 计算器弹琴
P2614 计算器弹琴 题目描述 总所周知,计算器可以拿来干很多它本不应该干的事情,比如写作文.(参看洛谷P2549) 小A发现了一个计算器的另一个隐藏功能——弹琴. http://www.bilib ...
- HDU 5375 Gray code(DP)
题意:给一串字符串,里面可能出现0,1,?,当中问号可能为0或1,将这个二进制转换为格雷码后,格雷码的每位有一个权值,当格雷码位取1时.加上该位权值,求最大权值和为多少. 分析:比赛的时候愚了.竟然以 ...
- Spark MLlib协同过滤算法
算法说明 协同过滤(Collaborative Filtering,简称CF,WIKI上的定义是:简单来说是利用某个兴趣相投.拥有共同经验之群体的喜好来推荐感兴趣的资讯给使用者,个人透过合作的机制给予 ...
- HDU 2689 Tree
Tree Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- telint---切换当前正在运行的Linux系统的运行等级
telint命令用于切换当前正在运行的Linux系统的运行等级 Send control commands to the init daemon. --help Show this help --no ...
- visualSVN+花生壳实现外网访问局域网内SVN
使用SubVersion+TortoiseSVN局域网内访问SVN成功后,想从外网访问SVN,使用花生壳绑定路由器动态DNS,但是折腾半天没搞定,突然发现一个帖子http://hi.baidu.com ...
- c、c++ 结构体的嵌套
c.c++ 结构体的嵌套 /************************************************************************/ /* 嵌套结构体 * C ...
- 用for和while循环求e的值[e=1+1/1!+1/2!+1/3!+1/4!+1/5!+...+1/n!]
/*编敲代码,依据下面公式求e的值. 要求用两种方法计算: 1)for循环.计算前50项 2)while循环,直至最后一项的值小于10-4 e=1+1/1!+1/2!+1/3!+1/4!+1/5!+. ...
- Git管理软件
软件下载地址 首先下载库 https://code.google.com/p/msysgit/ 接着安装 https://code.google.com/p/tortoisegit/ 然后就能够用了
- 负载均衡器&http正向代理
透明的负载均衡器&http正向代理 * master-workers架构,http正向代理由独立的dns请求以及缓冲进程 * 使用epoll(ET)模式,採用全异步方式(双缓存,实现双向同一 ...