UVa 816 Abbott的复仇(BFS)
寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了。希望以后每天也能多刷几道题。
题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同,允许出去的方向也不同。所以在记录迷宫的时候比较麻烦,可以用一个四元组has_edge[10][10][4][4]来记录,前两个元素代表坐标点,第三个元素代表进入该点时的方向,第四个元素代表离开该点时的方向。在BFS遍历时还是和以前的题目差不多的,记录好路径,最后回溯输出就行。
我还犯了个小错误,因为我用的是getline来输入每个迷宫的名字,所以在每次while循环最后应该加一句getchar()来吃掉回车。不然第二次循环时字符串就是回车了,这样就出错了。
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
using namespace std; struct node
{
int rr, cc, dir;
node(int a, int b, int c) { rr = a; cc = b; dir = c; }
node(){}
}; string str; int r0,c0,r1,c1,r2,c2;
char dir; const char* dirs = "NESW";
const char* turns = "FLR"; int dir_id(char c) { return strchr(dirs, c) - dirs; } //查找字符串中首次出现c的位置
int turn_id(char c) { return strchr(turns, c) - turns; } const int dr[] = { -, , , };
const int dc[] = { , , , - }; int has_edge[][][][]; int d[][][];
node v[][][]; node walk(const node &u, int turn)
{
int dir = u.dir;
if (turn == ) dir = (dir + ) % ;
if (turn == ) dir = (dir + ) % ;
return node(u.rr + dr[dir], u.cc + dc[dir], dir);
} bool inside(int x, int y)
{
if (x< && x> && y< && y>)
return true;
return false;
} void printfff(node u)
{
vector<node> nodes;
for (;;)
{
nodes.push_back(u);
if (d[u.rr][u.cc][u.dir] == ) break;
u = v[u.rr][u.cc][u.dir];
}
nodes.push_back(node(r0, c0, dir_id(dir))); int cnt = ;
for (int i = nodes.size() - ; i >= ; i--)
{
if (cnt % == ) printf(" ");
printf(" (%d,%d)", nodes[i].rr, nodes[i].cc);
if (++cnt % == ) cout << endl;
}
if (nodes.size() % != ) cout << endl;
} void BFS()
{
queue<node> q;
memset(d, -, sizeof(d));
node p(r1, c1, dir_id(dir));
d[p.rr][p.cc][p.dir] = ;
q.push(p);
while (!q.empty())
{
node p = q.front();
q.pop();
if (p.rr == r2 && p.cc == c2)
{
printfff(p);
return;
}
for (int k = ; k < ; k++)
{
node r = walk(p, k);
if (has_edge[p.rr][p.cc][p.dir][k] && inside(r.rr, r.cc) && d[r.rr][r.cc][r.dir]<)
{
d[r.rr][r.cc][r.dir] = d[p.rr][p.cc][p.dir] + ;
v[r.rr][r.cc][r.dir] = p;
q.push(r);
}
}
}
printf(" No Solution Possible\n");
} int main()
{
int x, y;
while (getline(cin,str) && str!= "END")
{
cout << str << endl;
memset(has_edge, , sizeof(has_edge));
cin >> r0 >> c0 >> dir >> r2 >> c2;
r1 = r0 + dr[dir_id(dir)]; //计算出初始位置点
c1 = c0 + dc[dir_id(dir)];
while (cin >> x && x != )
{
cin >> y;
char ch[];
while (cin >> ch && ch[] != '*')
{
int DIR = dir_id(ch[]);
int l = strlen(ch);
for (int i = ; i < l; i++)
{
int TURN = turn_id(ch[i]);
has_edge[x][y][DIR][TURN] = ;
}
}
}
BFS();
getchar();
}
return ;
}
UVa 816 Abbott的复仇(BFS)的更多相关文章
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- Uva 816 Abbott的复仇(三元组BFS + 路径还原)
题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...
- uva 816 Abbott的复仇
题目链接:https://uva.onlinejudge.org/external/8/816.pdf 紫书:P165 题意: 有一个最多包含9*9个交叉点的迷宫.输入起点.离开起点时的朝向和终点,求 ...
- Uva 816 Abbott's Revenge(BFS)
#include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...
- 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(有点困难bfs迷宫称号)
是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...
- UVA - 816 Abbott's Revenge(bfs)
题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...
- uva 816 abbott's revenge ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r
- UVA 816 Abbott’s Revenge
bfs求最短路,递归打印最短路的具体路径: 难点: 当前状态和转弯方式很复杂,要仔细处理: 递归打印:用一个数组存储路径中结点的前一个节点,递归查找 (bfs无法确定下一个结点,但对于没一个结点,它的 ...
随机推荐
- cp 覆盖 \cp a test\a
使用cp命令覆盖文件总是提示要输入yes或no,一个两个就算了,大量的文件复制就不行了,即使加上-f参数也无法强行覆盖.苦思冥想不得解,终于在查阅了众多资料后让我找到了解决方法,这里写出来,让有同样困 ...
- eclipse4.3 安装tomcat8
Go to the WTP downloads page, select the 3.6.0 version , and download the zip (under Traditional Zip ...
- Django:之ORM、CMS和二维码生成
Django ORM Django 的数据库接口非常好用,我们甚至不需要知道SQL语句如何书写,就可以轻松地查询,创建一些内容,所以有时候想,在其它的地方使用Django的 ORM呢?它有这么丰富的 ...
- Qt 5简介
Qt 5简介 Qt 5概要介绍 在Qt 5这个版本中,Qt Quick成为了Qt的核心.但是Qt 5也继续提供了本地C++强大的功能来完成更好的用户体验,也提供了对OpenGL/OpenGL ES图形 ...
- Inno Setup入门(二十一)——Inno Setup类参考(7)
Install Setup 2013-02-02 11:31 378人阅读 评论(0) 收藏 举报 复选框 复选框(CheckBox)用于多个并不互斥的几个选项中作出一个或者多选择,例如字体可以有粗体 ...
- IIS express 7.5 设置默认文档
在C:\Users\[电脑用户名]\Documents\IISExpress\config 下面有个applicationhost.config文件,打开文件找到<system.webServe ...
- mysql具体语句示例
建表:(not null ,auto_increment, unique , primary key) create database balfish;use balfish;create table ...
- vi命令的常用操作
G:移动到底部 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件, ...
- Spring MVC中,事务是否可以加在Controller层
一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找 ...
- Apache 的常见问题
Apache "No services installed"问题的处理以及Apache提示 the requested operation has failed而无法启动 安装完 ...