寒假训练——搜索——C - Robot
The GO command has one integer parameter n in {1,2,3}. After receiving this command the robot moves n meters in the direction it faces.
The TURN command has one parameter which is either left or right. After receiving this command the robot changes its orientation by 90o in the direction indicated by the parameter.
The execution of each command lasts one second.
Help researchers of RMI to write a program which will determine the minimal time in which the robot can move from a given starting point to a given destination.
Input
Output
Sample Input
9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 south
0 0
Sample Output
12 题目大意:
就是给机器人编一个行走的代码,求最短的时间,其实就是最短路。
这个有点不同,第一个是有方向,第二个是他是一个圆,不是点有面积。
思路:
用bfs,先处理这三种步法,走一步,走两步,走三步,
处理完之后再处理方向。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int n,m,x1,y1,x2,y2,d;
char b[20];
int tu[105][105];
bool vis[105][105][4];
int dx[4]={-1,0,1,0};//这里要注意东南西北相对应
int dy[4]={0,1,0,-1};
struct node
{
int x,y,step,fang;
};
int check(int x,int y)
{
if(x<1||y<1||x>=n||y>=m||tu[x][y]||tu[x+1][y]||tu[x][y+1]||tu[x+1][y+1]) return 0;//因为是左上角,所以x不能等于n,同理y也不能等于m。
return 1;
}
int bfs()
{
queue<node>que;
node a;
a.x=x1;
a.y=y1;
a.fang=d;
a.step=0;
que.push(a);
vis[a.x][a.y][a.fang]=1;
while(!que.empty())
{ a=que.front();que.pop();
if(a.x==x2&&a.y==y2) return a.step;
node ex=a;//注意这个ex要定义在外面,因为ex会累加,累加一次说明走一步,两次走两步,以此类推
for(int i=1;i<4;i++)//第一个for来走步数,三种情况,所以三次循环
{
ex.x+=dx[a.fang];
ex.y+=dy[a.fang];
if(!check(ex.x,ex.y)) break;
if(!vis[ex.x][ex.y][a.fang])
{
ex.fang=a.fang;
vis[ex.x][ex.y][ex.fang]=1;
ex.step=a.step+1;
que.push(ex);
}
}
for(int i=0;i<4;i++)//第二个for来走方向,四种情况,四次循环
{
if(max(a.fang,i)-min(a.fang,i)==2) continue;
if(vis[a.x][a.y][i]) continue;
vis[a.x][a.y][i]=1;
node ex=a;
ex.fang=i;
ex.step=a.step+1;
que.push(ex);
}
}
return -1;
} int main()
{
while(scanf("%d%d",&n,&m)&&(n+m))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%d",&tu[i][j]);
}
memset(vis,0,sizeof(vis));
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
scanf("%s",b);
if(b[0]=='n') d=0;
if(b[0]=='e') d=1;
if(b[0]=='s') d=2;
if(b[0]=='w') d=3;
printf("%d\n",bfs());
}
return 0;
}
寒假训练——搜索——C - Robot的更多相关文章
- 寒假训练——搜索 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 ...
- 寒假训练——搜索 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 ...
- J - Abbott's Revenge 搜索 寒假训练
题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...
- 寒假训练 A - A Knight's Journey 搜索
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- 算法专题训练 搜索a-T3 Ni骑士(ni)
搞了半天八数码弄不出来就只好来打题解 这道题是在搜索a碰到的(链接: http://pan.baidu.com/s/1jG9rQsQ ) 感觉题目最大亮点就是这英文简写"ni", ...
- HRBUST - 2347 - 递归画图 - vj大一上寒假训练2.11
其他题可由本题变形得到. 思路:利用坐标dfs搜索. 注意:1,初始化.2,坐标实时更新(x,y) 代码: #include<iostream> #include<cstdio> ...
- 寒假集训——搜索 B - Sudoku
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream&g ...
- 寒假集训——搜索 D - Cubes for Masha
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...
随机推荐
- codeM美团编程大赛初赛B轮D题(考验你的数学思维!)
[编程题] 模 时间限制:1秒空间限制:32768K 给定四个正整数a,b,c,k,回答是否存在一个正整数n,使得a*n在k进制表示下的各位的数值之和模b为c.输入描述:第一行一个整数T(T < ...
- ajax 跨域请求解决方案
1.为什么出现跨域: 前端和后端同一个项目下,ajax请求的地址是localhost同一个端口是话,是不会出现跨域问题的,所以相反前端和后端分开时,ajax请求的地址或者端口不是跟后台相同时就会出现跨 ...
- git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:
方法1:如果你想保留刚才本地修改的代码,并把git服务器上的代码pull到本地(本地刚才修改的代码将会被暂时封存起来) git stash git pull origin master git sta ...
- java Web三大组件--监听器
监听器概述 监听器(Listener)是一种特殊的Servlet技术,它可以监听Web应用的上下文信息.Servlet请求信息和Servlet会话信息,即ServletContext.ServletR ...
- Navicat Premium for Mac 破解版地址
找了好几个都不能使用 试了一下这个 可以使用 放地址:http://www.orsoon.com/Mac/85386.html
- Jump Flood Algorithms for Centroidal Voronoi Tessellation
Brief Implemented both CPU and GPU version, you could consider this as the basic playground to imple ...
- <自动化测试方案_10>第十章、自动化测试部署策略
第十章.自动化测试部署策略 按照以下顺序部署自动化框架 1,部署API自动化 时间:三周2,部署PC端UI自动化 时间:1个月 3,部署Sonar代码质量管理 时间:1个月 4,集成到Jenki ...
- 使用vuejs2.0和element-ui 搭建的一个后台管理界面
说明: 这是一个用vuejs2.0和element-ui搭建的后台管理界面. 相关技术: vuejs2.0:一套构建用户界面的渐进式JavaScript框架,易用.灵活.高效. element-ui: ...
- python leetcode 字符串相乘
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...
- Cannot obtain the required interface ("IID_IDBCreateCommand") from OLE DB provider "OraOLEDB.Oracle" for linked server xxxx
今天遇到了一个关于LINKED SERVER查询报错的案例,链接服务器链接ORACLE数据库,测试没有错误,但是执行脚本的时候,报如下错误: Msg 7399, Level 16, State 1 ...