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

题意:这道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. Oracle小技术集锦

  2. Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLIC

    在mysql5中遇到的问题: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) f ...

  3. hdu_5800_To My Girlfriend(变种背包)

    题目链接:hdu_5800_To My Girlfriend 题意: 给你n和物品和一个重量m,让你求 题解: To My Girlfriend 令dp[i][j][s1][s2]表示前i个物品填了j ...

  4. hdu_5791_Two(DP)

    题目链接:hdu_5791_Two 题意: 给你两串数列,问你相同的子序列有多少个,要注意,可以重复,比如1 和1 1 1 ,相同的子序列为3个 题解: 就和求最长公共子序列差不多,只不过要全部加起来 ...

  5. HDU2206:IP的计算

    Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方 ...

  6. 一个小知识点强引用__strong 弱引用__weak

    __weak的类型的指针是不会影响对象的释放 当系统释放后 会自动的指向nil: __Strong 至少有一个__Strong类型的指针是 对象不会释放  

  7. MyEclipse的Expressions没有结果的解决办法

    之前我的Expressions在Value这一列什么都不显示,就连简单的1+2结果3都不显示出来. 然后我咬咬牙把它卸载了,然后重装就好了,我也不清楚是什么原因. 1.之前我安装的目录是"C ...

  8. ActionBar更改背景颜色(主题)

    1.默认是黑色的背景, 2.更改主题theme为Theme.AppCompat.Light即可,清单文件主题如下: <application android:name="com.ith ...

  9. 树形dp Codeforces Round #364 (Div. 1)B

    http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...

  10. windowSoftInputMode键盘把输入框挡住了

    android:windowSoftInputMode="stateHidden|adjustResize" >