寒假训练——搜索——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 ...
随机推荐
- Java先比较日期再比较时间
package com.bihang.seaya; import lombok.Data; import java.text.ParseException; import java.text.Simp ...
- couldn't resolve host api.weixin.qq.com
1.代理服务器突然出现 couldn't resolve host api.weixin.qq.com 不知原因 2.重启nginx无效-----代码肯定没有动过(之前出现过,过了一天恢复) 3.防火 ...
- nodeJs express mongodb 建站(window 10 版)
一.环境搭建 安装 node.git.npm.express.mongodb.主要介绍express.mongodb 的安装. (1)node安装:https://nodejs.org/en/down ...
- HTML5中的input type为file控件限制上传文件类型及扩展
简单介绍 input file控件限制上传文件类型如下:1.文件类型中间用,分开:2.html和htm这样的要写成两个: 3实例: <input type="file" na ...
- mybatis的三种批量插入以及次效率比较
1.表结构 CREATE TABLE `t_user` ( `id` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT '主键', `name` varc ...
- python语言学习--1
第一天 item: 当字符串中出现 反斜杠'\' 时,输出时会把它当做转义字符处理,所以结果中不会出现它,若要正常输出则需要在字符串前面加上r, 我想r的意思是religion即“原始”的意思: it ...
- postman测试方法的 时候总是出现状态码500
postman测试方法的 时候总是出现状态码500 { "timestamp": "2018-07-23T05:43:51.773+0000", ...
- git 入门教程之版本控制
版本控制 我们知道 git 是分布式版本控制系统,所以称被控制对象是版本本身没错,但是从git 命令中发现,并没有版本这个名词,有的只是commit,所以前几节我一直称其为提交. 为了避免后续教程引发 ...
- svn状态与常见错误
TortoiseSVN 1.6.16是最后一个目录独立管理自身cache的svn版本(每个目录下都有一个隐藏的.svn文件夹) 之后的版本会则会根目录上统一进行管理(只有根目录下有一个隐藏的.svn文 ...
- Linux平台下RMAN异机恢复总结
下面总结.整理一下RMAN异机恢复这方面的知识点,这篇笔记在个人笔记里面躺了几年了,直到最近偶然被翻看到,遂整理.总结一下.如下所示,个人将整个RMAN异机恢复分为准备工作和操作步骤两大部分.当然,准 ...