题目

题目大意:
这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动
移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向。
思路:
需要写一个读入函数,这个需要读入起点,终点,方向(简单),还有就是接下来几行的不同位置
可以转的方向,可以写几个函数,根据函数来判断方向,最后转换成数字,用bool类型数组0,1分别代表
可以或不可以。
之后要写一个bfs,这个比较简单,就是和普通差不多,但是之后要输出路线,所以
要有两个数组,一个用来存储路程,一个用来存储路线。
具体一点的bfs,就是先把初始位置放进去,写清楚距离,之后进去while,while里面要有一个判断
是否到了终点,之后就用walk函数进行走路,然后判断这一步是否合法,合法就走下一步,不过,走下一步之前,记住两个点
第一个就是去记录距离,第二个就是记录路线。
最后有一个输出函数,用vector
总的来说,这也是一个最短路问题,和robot有点像,就是每个位置不一定每个方向都是对的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
string name;
int x1,x2,y1,y2,dir,x,y;
int dx[]={,,,-};
int dy[]={,,-,};
const char *dirs="ESWN";
const char *turns="FLR";
const int maxn=;
bool has_edge[maxn][maxn][][];
int d[maxn][maxn][]; int dir_id(char c)
{
return (strchr(dirs,c)-dirs);
}
int turn_id(char c)
{
return (strchr(turns,c)-turns);
} struct node
{
int x,y,dir;
node(int x=,int y=,int dir=):x(x),y(y),dir(dir){}
}b[maxn][maxn][]; node walk(const node& u,int turn)
{
int dir=u.dir;
if(turn==) dir=(dir+)%;
if(turn==) dir=(dir+)%;
return node(u.x+dx[dir],u.y+dy[dir],dir);
} bool scan()
{
memset(has_edge,,sizeof(has_edge));
cin>>name;
if(name=="END") return false;
char s[];
cin>>x>>y>>s>>x2>>y2; dir=dir_id(s[]);
x1=x+dx[dir];
y1=y+dy[dir];
int r,c;
while(cin>>r)
{
if(r==) break;
cin>>c;
char s1[];
while(cin>>s1)
{
if(s1[]=='*') break;
int l=strlen(s1);
for(int i=;i<l;i++)
{
has_edge[r][c][dir_id(s1[])][turn_id(s1[i])]=;
// printf("has_edge[%d][%d][%d][%d]=1;\n",r,c,dir_id(s1[0]),turn_id(s1[i]));
}
}
}
cout<<name<<endl;
return true;
} bool judge(int r, int c)
{
return r >= && r <= && c >= && c <= ;//迷宫从1开始
} void print_ans(node exa)
{
vector<node>nodes;
while(true)
{
nodes.push_back(exa);
if(d[exa.x][exa.y][exa.dir]==) break;
exa=b[exa.x][exa.y][exa.dir];
}
nodes.push_back(node(x,y,dir));
int cnt=;
for(int i=nodes.size()-;i>=;i--)
{
if(cnt%==) printf(" ");
cnt++;
printf(" (%d,%d)",nodes[i].x,nodes[i].y);
if(cnt%==) printf("\n");
}
if(cnt%!=) printf("\n");
} void bfs()
{
queue<node>que;
node exa=node(x1,y1,dir);
memset(d,-,sizeof(d));
d[exa.x][exa.y][exa.dir]=;
que.push(exa);
while(!que.empty())
{
node exa=que.front();
//printf("%d %d %d\n",exa.x,exa.y,exa.dir);
que.pop();
if(exa.x==x2&&exa.y==y2)
{
print_ans(exa);
return ;
}
for(int i=;i<;i++)
{
node v=walk(exa,i);
// printf("www %d %d %d %d\n",v.x,v.y,v.dir,i);
// printf("d[%d][%d][%d]=%d\n",v.x,v.y,v.dir,d[v.x][v.y][v.dir]);
if(has_edge[exa.x][exa.y][exa.dir][i]&&judge(v.x,v.y)&&d[v.x][v.y][v.dir]<)
{
d[v.x][v.y][v.dir]=d[exa.x][exa.y][exa.dir]+;
b[v.x][v.y][v.dir]=exa;
//printf("%d %d %d\n",v.x,v.y,v.dir);
que.push(v);
}
}
}
printf(" No Solution Possible\n");
} int main()
{
while(scan())
{ bfs();
}
return ;
}

J - Abbott's Revenge 搜索 寒假训练的更多相关文章

  1. UVa816 Abbott's Revenge

    Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge  Abbott’s Revenge The 1999 World FinalsC ...

  2. L - Abbott's Revenge(比较复杂的bfs)

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UV ...

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

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

  4. 寒假训练——搜索 K - Cycle

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  5. 寒假训练——搜索 E - Bloxorz I

    Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...

  6. 寒假训练——搜索——C - Robot

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  7. 寒假训练——搜索 G - Xor-Paths

    There is a rectangular grid of size n×mn×m . Each cell has a number written on it; the number on the ...

  8. 寒假训练 A - A Knight's Journey 搜索

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  9. HRBUST - 2347 - 递归画图 - vj大一上寒假训练2.11

    其他题可由本题变形得到. 思路:利用坐标dfs搜索. 注意:1,初始化.2,坐标实时更新(x,y) 代码: #include<iostream> #include<cstdio> ...

随机推荐

  1. Winform杂项

    1.控件右键属性:ContextMenuStrip,设置菜单 2.编辑代码:this.treeView1.Nodes.Remove(this.treeView1.SelectedNode);//获取树 ...

  2. ASP.NET MVC 学习笔记-5.Controller与View的数据传递

    ViewData属性 ViewData属性是System.Web.Mvc.ControllerBase中的一个属性,它相当于一个数据字典.Controller中向该字典写入数据,ViewData[“K ...

  3. Echarts图表legend的排版问题(转载)

    来源:https://blog.csdn.net/david_jiahuan/article/details/80096922 案例一 项目中现有样式: 客户需求:将图例分为两列,并且上下两列的图例要 ...

  4. springMVC_04controller四种配置总结

    一.通过url对应bean,加粗部分为必须有的 <bean class=" org.springframework.web.servlet.handler.BeanNameUrlHan ...

  5. java关于字符串的一些实用操作工具类方法

    package cn.edu.stdu; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.Se ...

  6. 1:Python开发:初识Python

    1.开发语言: 高级语言:Python, Java, PHP ,C# ,Go, ruby, c++ ==>字节码 低级语言:C,汇编 ==>机器码 2.开发语言的对比: PHP类:适用于写 ...

  7. 51NOD 1185 威佐夫游戏 V2(威佐夫博弈)

    1185 威佐夫游戏 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取 ...

  8. Linux查看机器的硬件信息

    转载:https://linux.cn/article-9932-1.html

  9. Android Studio--gradle:download 过慢甚至超时timeout报错

    问题描述 今天第一次学习安卓,配置环境花了不少时间其他都比较容易的解决了 gradle 因为会从外国的网站下载东西会非常的慢(具体原理我也不清楚),所以最好让下载地址变成国内的 解决方案(最后有升级方 ...

  10. [Android][Framework] 添加系统服务

    新博客地址 http://wossoneri.github.io/2018/09/15/[Android][Framework]create-system-service/ 做系统开发,有时候需要自己 ...