寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了。希望以后每天也能多刷几道题。

题意:这道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)的更多相关文章

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

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

  2. Uva 816 Abbott的复仇(三元组BFS + 路径还原)

    题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...

  3. uva 816 Abbott的复仇

    题目链接:https://uva.onlinejudge.org/external/8/816.pdf 紫书:P165 题意: 有一个最多包含9*9个交叉点的迷宫.输入起点.离开起点时的朝向和终点,求 ...

  4. Uva 816 Abbott's Revenge(BFS)

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

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

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

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

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

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

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

  8. uva 816 abbott's revenge ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r

  9. UVA 816 Abbott’s Revenge

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

随机推荐

  1. Chapter 2 Open Book——7

    I gunned my deafening engine to life, ignoring the heads that turned inmy direction, and backed care ...

  2. 用tomcat6自定义域名

    第一步:tomcat配置 修改server.xml文件 8080端口 更改为 80端口 并在<Host name="localhost"  appBase="web ...

  3. linux下mysql root密码忘记修改方法

    一.MySQL密码的恢复方法之一 如果忘记了MySQL的root密码,可以用以下方法重新设置:1.切换到root下su root 2. KILL掉系统里的MySQL进程: killall -TERM ...

  4. .net简单的静态页生成

    1.得到实体对象model,读取模板 string htmlMaster = File.ReadAllText(HttpContext.Current.Server.MapPath("/ma ...

  5. Hibernate 关系映射方式(1)

    来源:本文转载自:http://blog.csdn.net/huangaigang6688/article/details/7761310 Hibernate映射解析——七种映射关系 首先我们了解一个 ...

  6. 第一次安装ubuntu要设置的东西

    1. 安装网卡驱动 lscpi 查看网卡型号 根据型号找到驱动源码 下载下来并编译 安装 2. 编译安卓源码的时候出现jdk型号不对的情况 把/usr/bin/java 删除,就可以了.

  7. sql数据库删除表的外键约束(INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX)

    使用如下SQL语句查询出表中外键约束名称: 1 select name 2 from sys.foreign_key_columns f join sys.objects o on f.constra ...

  8. FileSystemXmlApplicationContext方法的绝对路径问题

    public AgentServer(Socket c,String confDir) { this.client = c; ApplicationContext ac = new FileSyste ...

  9. 【转】linux grep命令详解

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  10. POJ1734/Floyd求最小环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6647   Accepted: 2538 ...