Robot POJ - 1376
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
机器人搬运研究所正在当地商店中使用机器人来运输不同的物品。当然,机器人只需要花费从商店的一个地方到另一个地方旅行所需的最短时间。机器人只能沿着一条直线(轨道)移动。所有轨道形成一个矩形网格。相邻的轨道相隔一米。该商店是一个矩形的N×M米,它完全被这个网格覆盖。最接近商店一侧的跑道距离只有一米。机器人具有直径等于1.6米的圆形形状。轨道穿过机器人的中心。机器人总是面向北,南,西或东。轨道位于南北和东西方向。机器人只能朝它面对的方向移动。它的方向可以在每个轨道交叉处改变。最初机器人站在轨道交叉处。商店中的障碍物是由地面上的1m×1m的碎片组成的。每个障碍物都在由轨道形成的1×1方格内。机器人的运动由两个命令控制。这些命令是GO和TURN。
GO命令在{1,2,3}中有一个整数参数n。接收到这个命令后,机器人按照它所面对的方向移动n米。
TURN命令有一个参数可以是左或右。接收到该命令后,机器人按照参数指示的方向将其方向改变90°。
每个命令的执行持续一秒钟。
帮助RMI的研究人员编写一个程序,该程序将确定机器人从一个给定的起点移动到一个给定的目的地的最短时间。
这个机器人有两种指令走向当前放下走1-3步或者是向左或向右90度转向(不能向后转)。
注意构图,机器人在线上走动,而给的数据是一个一个的方块格子,
标记数组为vis[100][100][4],4代表4个方向
if (!check(nx,ny)) break;这个的break说明如果你前方已经走不通了 那么你就不能再往前走,于是break
这题的方向特别容易卡 要注意
int dx[4]= {-1,0,1,0}; int dy[4]= {0,1,0,-1};
if (b[0]=='n') d=0;
if (b[0]=='e') d=1;
if (b[0]=='s') d=2;
if (b[0]=='w') d=3;
这些都是一一对应关系
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,x1,y1,x2,y2,tu[][],vis[][][],d;
char b[];
struct node {
int x,y,step,fang;
};
int dx[]= {-,,,};
int dy[]= {,,,-};
int check(int x, int y) {
if (x < || x >=n || y < || y >= m || tu[x][y] || tu[x+][y] || tu[x][y+] || tu[x+][y+]) return ;
return ;
}
int bfs() {
queue<node>q;
node a;
a.x=x1,a.y=y1,a.fang=d,a.step=;
vis[a.x][a.y][d]=;
q.push(a);
while(!q.empty()) {
a=q.front();
q.pop();
if (a.x==x2 && a.y==y2) return a.step;
int nx=a.x;
int ny=a.y;
for (int i = ; i < ; i++) {
nx += dx[a.fang];
ny += dy[a.fang];
if (!check(nx, ny))
break;
if (!vis[nx][ny][a.fang]) {
vis[nx][ny][a.fang] = ;
node cnt;
cnt.x = nx, cnt.y = ny;
cnt.fang =a.fang, cnt.step = a.step+;
q.push(cnt);
}
}
for (int i = ; i < ; i++) {
if (max(a.fang, i)-min(a.fang, i) == )
continue;
if (vis[a.x][a.y][i])
continue;
vis[a.x][a.y][i] = ;
node cnt = a;
cnt.fang = i;
cnt.step = a.step+;
q.push(cnt);
}
}
return -;
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
if (n== && m== ) break;
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
scanf("%d", &tu[i][j]);
memset(vis, , sizeof(vis));
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
scanf("%s", b);
if (b[]=='n') d=;
if (b[]=='e') d=;
if (b[]=='s') d=;
if (b[]=='w') d=;
printf("%d\n", bfs());
}
return ;
}
Robot POJ - 1376的更多相关文章
- POJ 1376 Robot
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7866 Accepted: 2586 Description The R ...
- Cleaning Robot POJ - 2688
题目链接:https://vjudge.net/problem/POJ-2688 题意:在一个地面上,有一个扫地机器人,有一些障碍物,有一些脏的地砖,问,机器热能不能清扫所有的地砖, (机器人不能越过 ...
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
- Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...
- POJ 1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12978 Accepted: 6290 Des ...
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
...
随机推荐
- u-boot.bin生成过程分析
ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...
- java web项目使用ant编译将不同的功能代码打包成jar,进而分局点将项目打包成不同的tar.gz包进而部署
使用ant可以轻松的将一个项目分离代码,直接打包成不同需求的tar.gz包使用 1.build.properties (属性) version.num=1.0 #版本信息 2.build.xml (a ...
- .Net 面试题 汇总(三)
101.ASP.net的身份验证方式有哪些?分别是什么原理? 答:Windwos(默认)用IIS... From(窗体)用帐户 Passport(密钥) 102.在.net中,配件的意思是? 答:程序 ...
- linux io 学习笔记(01)---锁,信号量
1.采用信号量访问:当有段临界代码,需要保证排他的访问一个资源. 2.sudo dmesg -c 消除dmesg缓冲 3.互斥锁:代表的是一种锁资源,互斥锁的工作原理是:保证对共享资源操作的原子性 ...
- LI 标签中让文章标题左对齐,日期右对齐的方法
希望实现标题在左对齐,日期在右对齐,当直接给日期的span加上float:right时,IE8和FF都OK,但IE6/7则会换行,下面给出一个简单有效的解决办法. <!DOCTYPE html ...
- webapi到处excel
最近项目用的webapi前几天做了个导出excel功能,给大家分享下,自己也记录下... 在用的过程中,可以直接请求就可以得到下载的excel文件,在实际的项目中可以通过js打开新页面,encodeU ...
- 区分Oracle的数据库,实例,服务名,SID
文章摘自:http://www.zhetao.com/content240 感谢分享O(∩_∩)O~ 在实际的开发应用中,关于Oracle数据库,经常听见有人说建立一个数据库,建立一个Instance ...
- html页面导出word文档
1.加入两个外部js 1)FileSaver.js /* FileSaver.js * A saveAs() FileSaver implementation. * 1.3.2 * 2016-06-1 ...
- javascript 自定义发布与订阅
//声明一个类,与普通的类的声明不一样, function Girl() { //将类的事件声明成一个私有的属性,里面是一个对象 this._events = {} } /* { "失恋&q ...
- 程序员编程利器:20款最好的免费的IDEs和编辑器
程序员编程利器:20款最好的免费的IDEs和编辑器 还没转眼明年可就大年三十了,忙的可真是晕头转了个向,看着亲朋好友们那让人欣羡的小肚腩,不禁感慨,岁月是一把猪饲料,绿了芭蕉,肥了那杨柳小蛮腰,可怜我 ...