J - Abbott's Revenge 搜索 寒假训练
这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动
移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向。
思路:
需要写一个读入函数,这个需要读入起点,终点,方向(简单),还有就是接下来几行的不同位置
可以转的方向,可以写几个函数,根据函数来判断方向,最后转换成数字,用bool类型数组0,1分别代表
可以或不可以。
之后要写一个bfs,这个比较简单,就是和普通差不多,但是之后要输出路线,所以
要有两个数组,一个用来存储路程,一个用来存储路线。
具体一点的bfs,就是先把初始位置放进去,写清楚距离,之后进去while,while里面要有一个判断
是否到了终点,之后就用walk函数进行走路,然后判断这一步是否合法,合法就走下一步,不过,走下一步之前,记住两个点
第一个就是去记录距离,第二个就是记录路线。
最后有一个输出函数,用vector
#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 搜索 寒假训练的更多相关文章
- UVa816 Abbott's Revenge
Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge Abbott’s Revenge The 1999 World FinalsC ...
- L - Abbott's Revenge(比较复杂的bfs)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UV ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- 寒假训练——搜索 K - Cycle
A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...
- 寒假训练——搜索 E - Bloxorz I
Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...
- 寒假训练——搜索——C - Robot
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- 寒假训练——搜索 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 ...
- 寒假训练 A - A Knight's Journey 搜索
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- HRBUST - 2347 - 递归画图 - vj大一上寒假训练2.11
其他题可由本题变形得到. 思路:利用坐标dfs搜索. 注意:1,初始化.2,坐标实时更新(x,y) 代码: #include<iostream> #include<cstdio> ...
随机推荐
- oracle 汉字转化拼音函数
FN_GETPY('中华人民共和国') -------------------------------------------------------------------------------- ...
- 8张图让你一步步看清 async/await 和 promise 的执行顺序
摘要: 面试必问 原文:8张图帮你一步步看清 async/await 和 promise 的执行顺序 作者:ziwei3749 Fundebug经授权转载,版权归原作者所有. 为什么写这篇文章? 说实 ...
- JS预解析机制
JS的预解析过程: 1,预解析 2,再逐行解读代码, 实例: ---------------------------- <script> var name="xm& ...
- vue从入门到进阶:渲染函数 & JSX(八)
Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template 更接 ...
- Sublime Text 2 JS 格式化插件 JsFormat
这里下载这插件包 https://github.com/jdc0589/JsFormat ,点油下角的zip就能下载插件包放到sublime安装目录的DataPackages目录中重新打开sublim ...
- Html5新增标签的学习。
随笔,记录的比较随便. 今天新学习了9个标签. <audio> 简单的说就是一个音频标签,他的主要常用属性有src=""音频的路径 controls="con ...
- AI在汽车中的应用:实用深度学习
https://mp.weixin.qq.com/s/NIza8E5clC18eMF_4GMwDw 深度学习的“深度”层面源于输入层和输出层之间实现的隐含层数目,隐含层利用数学方法处理(筛选/卷积)各 ...
- Linux PCI设备驱动的实现思路与思想
概述 1.PCI设备一般都具有双重身份,一方面作为PCI设备注册到Linux内核,另一方面,作为字符设备或者块设备,或者网络设备注册到Linux内核,所以,在看PCI设备时一定要注意到这点. 2. 一 ...
- python itchat 爬取微信好友信息
原文链接:https://mp.weixin.qq.com/s/4EXgR4GkriTnAzVxluJxmg 「itchat」一个开源的微信个人接口,今天我们就用itchat爬取微信好友信息,无图言虚 ...
- js导出Excel表格
js导出Excel表格 直接上代码: 红色部分:如果表格数据中有“1/1”这样的值,会在导出的Excel中转化为日期“1月1日”,所以才加上了红色那两句.如果返回值中没有这样的格式,红色部分可以不写. ...