题目

题目大意:
这个题目就是大小不超过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. git常用命令总结(资源来自廖雪峰)

    自己把命令弄出来方便以后看看,,应该有错的emmmm 原文地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67 ...

  2. 利用OpenCV给图像添加中文标注

    利用OpenCV给图像添加中文标注 : 参考:http://blog.sina.com.cn/s/blog_6bbd2dd101012dbh.html  和https://blog.csdn.net/ ...

  3. canvas-a11htmlANDcanvas.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. K8S Calico

    NetworkPolicy是kubernetes对pod的隔离手段,是宿主机上的一系列iptables规则. Egress 表示出站流量,就是pod作为客户端访问外部服务,pod地址作为源地址.策略可 ...

  5. Django的下载安装以及实现一个简单示例

    一.Django下载安装 Django下载链接 1. 下载Django: pip3 install django==1.11.9    (大的版本1.11不要错) 2.创建一个django proje ...

  6. UOJ#310. 【UNR #2】黎明前的巧克力(FWT)

    题意 题目链接 Sol 挂一个讲的看起来比较好的链接 然鹅我最后一步还是没看懂qwq.. 坐等SovietPower大佬发博客 #include<bits/stdc++.h> using ...

  7. Linux扩展分区记录

    Vmware中新建的Linux虚拟机,数据盘规划太小,数据量超出磁盘大小,本文介绍如何快速扩充空间 参考:https://blog.csdn.net/lyd135364/article/details ...

  8. IE打开https网站时,取消证书问题提示

    上面介绍了,调用IE来打开对应的网页问题,但是在实际测试中,有些网站是采用https协议的,这时候IE浏览器会弹出如下窗口,一般手动选择后,才可进入登录界面,那么该如何解决呢? 1.点击[继续浏览此网 ...

  9. 资深程序员整理出来的Python面试题

    转载链接:https://www.cnblogs.com/fcxwz/p/9225791.html

  10. Ext 日期格式化

    //日期格式化 Date.prototype.Format = function (fmt) { var o = { , //月份 "d+": this.getDate(), // ...