uva 816 BFS求最短路的经典问题……
一开始情况没有考虑周全,直接WA掉了,
然后用fgets()出现了WA,给改成scanf就AC了
题目不是很难,用心就好……
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map> using namespace std;
const int INF = 0xffffff;
const double Pi = * atan();
const int Maxn = + ;
//int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}};
int dr[] = {-,,,};
int dc[] = {,,,-}; struct Node{
int r;
int c;
int dir;
Node(int rr,int cc,int dd){
r = rr;
c = cc;
dir = dd;
}
Node(){}
};
const char * dirs = "NESW";
const char * turns = "FLR";
bool has_edge[][][][];
int d[][][];
int r1,c1,dir;
int r0,c0;
int r2,c2;
bool flag;
Node p[][][]; int dir_id(char ch){
return strchr(dirs,ch) - dirs;
}
int turn_id(char ch){
return strchr(turns,ch) - turns;
} Node walk(Node & u,int turn){
int dd = u.dir;
if(turn == ){
dd = (dd + ) % ;
}
else if(turn == ){
dd = (dd + ) % ;
}
return Node(u.r + dr[dd],u.c + dc[dd],dd);
} void print_ans(Node u){
vector<Node>nodes;
while(){
nodes.push_back(u);
if(d[u.r][u.c][u.dir] == )
break;
u = p[u.r][u.c][u.dir];
}
nodes.push_back(Node(r0,c0,dir));
int cnt = ;
for(int i = nodes.size()-;i >= ;i--){
if(cnt % == )
cout << " ";
cout << " (" << nodes[i].r << "," << nodes[i].c << ")";
if(++cnt % == )
cout << endl;
}
if(nodes.size() % != )
cout << endl;
} bool inside(int r,int c){
if(r > && r < && c > && c < )
return true;
return false;
} void solve(){
queue<Node>q;
memset(d,-,sizeof(d));
memset(p,,sizeof(p));
Node u(r1,c1,dir);
d[u.r][u.c][u.dir] = ;
q.push(u);
while(!q.empty()){
Node u = q.front();
q.pop();
if(u.r == r2 && u.c == c2){
flag = ;
print_ans(u);
return;
}
for(int i = ;i < ;i++){
Node v = walk(u,i);
if(has_edge[u.r][u.c][u.dir][i] && inside(v.r,v.c) && d[v.r][v.c][v.dir] < ){
d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + ;
p[v.r][v.c][v.dir] = u;
q.push(v);
}
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
char str[];
while(~scanf("%s",str) && strcmp(str,"END")){
printf("%s\n",str);
flag = ;
memset(has_edge,,sizeof(has_edge));
char ch;
cin >> r0 >> c0 >> ch >> r2 >> c2;
dir = dir_id(ch);
r1 = r0 + dr[dir];
c1 = c0 + dc[dir];
int r,c;
char str2[]; while(cin >> r){
if(r == ){
break;
}
cin >> c;
while(cin >> str2){
if(str2[] == '*')
break;
int dirID = dir_id(str2[]);
int len = strlen(str2);
for(int i = ;i < len;i++){
int turnID = turn_id(str2[i]);
has_edge[r][c][dirID][turnID] = ;
}
}
}
solve();
if(flag){
cout << " No Solution Possible" << endl;
}
getchar();
}
return ;
}
测试数据:
SAMPLE
N
WL NR *
WLF NR ER *
NL ER *
SL WR NF *
SL WF ELF *
SFR EL * NOSOLUTION
N
WL NR *
NL ER *
SL WR NFR *
SR EL * MyMaze
N MyMaze
N MyMaze
N MyMaze
W
NR *
ER *
WR *
SF * MyMaze
N
WL *
NL *
SF *
NR *
SL *
EL * Circle
N
NR *
ER *
SF *
WR *
SR * Robert Abbott's Atlanta Maze
N
NR WL *
NLR WF EFR *
EFR WFR NL *
ER NL *
SFL WL NFR *
EL SFLR WFRL NFL *
EFLR SF NF WFRL *
SR ELR NF *
WR SL *
EFL SLR WR NF *
EFL SLR WL *
EL SR * END
SAMPLE
(,) (,) (,) (,) (,) (,) (,) (,) (,) (,)
(,) (,) (,) (,) (,)
NOSOLUTION
No Solution Possible
MyMaze
No Solution Possible
MyMaze
No Solution Possible
MyMaze
(,) (,)
MyMaze
(,) (,) (,) (,) (,) (,)
MyMaze
(,) (,) (,) (,) (,) (,) (,) (,)
Circle
(,) (,) (,) (,) (,) (,) (,)
Robert Abbott's Atlanta Maze
(,) (,) (,) (,) (,) (,) (,) (,) (,) (,)
(,) (,) (,) (,) (,) (,) (,) (,) (,) (,)
uva 816 BFS求最短路的经典问题……的更多相关文章
- UVa 816 (BFS求最短路)
/*816 - Abbott's Revenge ---代码完全参考刘汝佳算法入门经典 ---strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:char * strchr (cons ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- 6.4.2 用BFS求最短路
前面的篇幅占了太多,再次新开一章,讲述BFS求最短路的问题 注意此时DFS就没有BFS好用了,因为DFS更适合求全部解,而BFS适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- BFS求最短路
假设有一个n行m列的迷宫,每个单位要么是空地(用1表示)要么是障碍物(用0表示).如和找到从起点到终点的最短路径?利用BFS搜索,逐步计算出每个节点到起点的最短距离,以及最短路径每个节点的前一个节点. ...
- 利用BFS求最短路
利用BFS求图的最短路, POJ3984 #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<string.h& ...
- hdu 3760(2次bfs求最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3760 思路:首先是建反图,从点n开始做spfa求出n到各点的最短路,然后从1点开始搜最小序列,对于边( ...
- CodeForces - 987D Fair (BFS求最短路)
题意:有N个城市,M条双向道路连接两个城市,整个图保证连通.有K种物品,但每个城市只有一种,现在它们都需要S种物品来举办展览,可以去其他城市获取该城市的物品,花费是两城市之间的最短路径长度.求每个城市 ...
随机推荐
- 【.Net基础拾遗】适配器模式(Adapter)与多态
今天晚上跟大家主要来讨论下适配器模式和多态,什么是适配器模式呢?先抛给大家一个问题:假设两个类Student和Teacher继承一个抽象基类Person,如何在不改动三类情况下实现多Student.T ...
- java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode(尼玛,蛋疼的错误)
java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode \-[M ...
- AWVS介绍(转)
使用AWVS对域名进行全局分析,深入探索: 首先,介绍一下AWVS这个工具. Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网 ...
- poj 2356鸽笼原理水题
关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...
- Ruby学习-第一章
第一章 字符串,数字,类和对象 为了证明Ruby真的好用,hello world也能写的如此简洁: puts 'hello world' 1.输入/输出 print('Enter your name' ...
- Nginx 因 Selinux 服务导致无法远程訪问
文章来源:http://blog.csdn.net/johnnycode/article/details/41947581 2014-12-16日 昨天晚上处理好的网络訪问连接.早晨又訪问不到了. 现 ...
- 用Swift完成不同View Controller之间的切换
之前用objective-c开发时,页面之间的切换很容易.其实用swift没有很大的变化,如果你是用storyboard完成的界面,基本上是同样的方式,只不过在代码部分写成swift风格的就行了. 今 ...
- 1 #安装php
#安装php #备注:php5..3以后的版本源码不需要打php-fpm补丁,该补丁已经集成进5..3中强制启用fastcgi. [root@dba01 nginx-]# cd [root@dba01 ...
- pyfits例子
下面是一个读入fits文件,画积分强度图,再把某个星表里的天体画到图上的python程序. ====================================================== ...
- iTextSharp
iTextSharp 116毫秒处理6G的文件 前言: 有一家印刷企业专为米兰新娘,微微新娘,金夫人这样的影楼印刷婚纱相册.通过一个B2B销售终端软件,把影楼的相片上传到印刷公司的服务器,服务器对 ...