题目

题目大意:
这个题目就是大小不超过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. nginx静态资源文件无法访问,403 forbidden错误

    在安装 nginx 服务器后,我想把网站的根目录设置为 /root/www/ ,于是对 nginx 的 nginx.conf 文件进行配置 先打开 nginx.conf #user nobody; w ...

  2. 《Office 365开发入门指南教程》正式上线,限时优惠和邀请分享推广

    我很高兴地通知大家,<Office 365 开发入门指南教程>已经正式在网易云课堂上线,你可以通过直接访问  https://aka.ms/office365devlesson 这个短地址 ...

  3. 今天给大家补充一下 background 用法

    补充一个知识点 1,浏览器默认字体大小是font-size:16px:谷歌最小字体是10px,其他浏览器是12px 2. 选择器 通配符选择器     *   表示 3.background  背景 ...

  4. Unix awk的流程控制BEGIN和END的讲解

    你可能对Unix比较熟悉,但你可能对Unix awk很陌生,这一点也不奇怪,的确,与其优秀的功能相比,awk还远没达到它应有的知名度. 流程控制语句是任何程序设计语言都不能缺少的部分.任何好的语言都有 ...

  5. 解密JavaScript闭包

    译者按: 从最简单的计数器开始,按照需求对代码一步步优化,我们可以领会闭包的神奇之处. 原文: Closures are not magic 译者: Fundebug 为了保证可读性,本文采用意译而非 ...

  6. js之单例模式

    单例模式是指一个类,只有一个实例.实现的思路是,创建实例时候加判断,如果有实例则返回,如果没有就new一个,并返回. 第一步: 创建类. function Waiter(id, name, salar ...

  7. unable to locate nuget.exe

    今日使用vs 从github fork 一份代码到本地之后,提示项目 unable to locate nuget.exe. 原因:代码托管时未提交 nuget.exe 或其他原因丢失 解决方法:在解 ...

  8. Android view显示在软键盘上方

    给EditText外加一个ScrollView,将高度设置统一,并给ScrollView设置属性 android:fillViewport="true".  注:ScrollVie ...

  9. log4j介绍

    log4j.properties文件的三种加载方式 1.默认自动加载 满足以下条件时: 1).配置文件名为log4j.properties 2).在classpath根目录下(即resources根目 ...

  10. Socket编程(网络编程)

    网络通信的第一要素:IP地址 通过IP地址唯一的定位到互联网的主机 通过 IP+port(端口号) 来确定互联网的主机上的某个软件 InetAddress:位于java.net包下 getHostNa ...