Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7866   Accepted: 2586

Description

The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time necessary when travelling from one place in the store to another. The robot can move only along a straight line (track). All tracks form a rectangular grid. Neighbouring tracks are one meter apart. The store is a rectangle N x M meters and it is entirely covered by this grid. The distance of the track closest to the side of the store is exactly one meter. The robot has a circular shape with diameter equal to 1.6 meter. The track goes through the center of the robot. The robot always faces north, south, west or east. The tracks are in the south-north and in the west-east directions. The robot can move only in the direction it faces. The direction in which it faces can be changed at each track crossing. Initially the robot stands at a track crossing. The obstacles in the store are formed from pieces occupying 1m x 1m on the ground. Each obstacle is within a 1 x 1 square formed by the tracks. The movement of the robot is controlled by two commands. These commands are GO and TURN. 
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

The input consists of blocks of lines. The first line of each block contains two integers M <= 50 and N <= 50 separated by one space. In each of the next M lines there are N numbers one or zero separated by one space. One represents obstacles and zero represents empty squares. (The tracks are between the squares.) The block is terminated by a line containing four positive integers B1 B2 E1 E2 each followed by one space and the word indicating the orientation of the robot at the starting point. B1, B2 are the coordinates of the square in the north-west corner of which the robot is placed (starting point). E1, E2 are the coordinates of square to the north-west corner of which the robot should move (destination point). The orientation of the robot when it has reached the destination point is not prescribed. We use (row, column)-type coordinates, i.e. the coordinates of the upper left (the most north-west) square in the store are 0,0 and the lower right (the most south-east) square are M - 1, N - 1. The orientation is given by the words north or west or south or east. The last block contains only one line with N = 0 and M = 0. 

Output

The output contains one line for each block except the last block in the input. The lines are in the order corresponding to the blocks in the input. The line contains minimal number of seconds in which the robot can reach the destination point from the starting point. If there does not exist any path from the starting point to the destination point the line will contain -1. 

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

这题改了好几天。。。。。
错误的点:
1.在DEBUG的时候我尝试恢复路径,此时发现有的结点的pre信息被后来修改,是因为应当在入队的时候标记,而不是出队的时候
2.对于位置的移动判断写错,首先边界不能触碰,而且一个黑色方格周围的点也不能。
3.在枚举一个点沿着一个方向行走的可行距离的时候,当遇到黑色方块或者边界的时候要break 代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 109
#define N 33
#define MOD 1000000
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0); int X[] = { -,,, }, Y[] = { ,,,- };
bool been[MAXN][MAXN][];//四个方向
int n, m, rx, ry, g[MAXN][MAXN];
struct node
{
int x, y, dir, time;
node() = default;
node(int _x, int _y, int _dir, int _t) :x(_x), y(_y), dir(_dir), time(_t) {}
};
node pre[MAXN][MAXN][];
int d[MAXN][MAXN];
void print(node u)
{
vector<node> v;
for (;;)
{
//cout << u.x <<' '<< u.y << ' '<< u.dir << endl;
v.push_back(u);
if (u.time == ) break;
u = pre[u.x][u.y][u.dir];
}
int cnt = ;
for (int i = v.size() - ; i >= ; i--)
{
printf("%d %d %d %d\n", v[i].x, v[i].y, v[i].dir, v[i].time);
}
}
bool CanGo(int x, int y)
{
if (x< || x >= n || y< || y >= m)
return false;
if (g[x][y] || g[x + ][y] || g[x][y + ] || g[x + ][y + ])
return false;
return true;
}
int BFS(int x, int y, int d)
{
been[x][y][d] = true;
queue<node> q;
q.push(node(x, y, d, ));
while (!q.empty())
{
node t = q.front();
q.pop();
//cout << t.x << ' ' << t.y << ' ' << t.dir <<' ' << t.time << endl; if (t.x == rx&&t.y == ry)
{
//cout << t.prex << ' ' << t.prey << "::::" << t.dir << endl;
//print(t);
return t.time;
} if (!been[t.x][t.y][(t.dir + ) % ])
{
been[t.x][t.y][(t.dir + ) % ] = true;
pre[t.x][t.y][(t.dir + ) % ] = t;
q.push(node(t.x, t.y, (t.dir + ) % , t.time + ));
}
if (!been[t.x][t.y][(t.dir - + ) % ])
{
been[t.x][t.y][(t.dir - + ) % ] = true;
pre[t.x][t.y][(t.dir - + ) % ] = t;
q.push(node(t.x, t.y, (t.dir - + ) % , t.time + ));
}
for (int i = ; i <= ; i++)
{
int tx = t.x + X[t.dir] * i, ty = t.y + Y[t.dir] * i;
if (CanGo(tx, ty))
{
if(!been[tx][ty][t.dir])
{
been[tx][ty][t.dir] = true;
pre[tx][ty][t.dir] = t;
q.push(node(tx, ty, t.dir, t.time + ));
}
}
else
break;
}
}
return -;
}
int main()
{
while (scanf("%d%d", &n, &m), n + m)
{
memset(been, false, sizeof(been));
int tx, ty, d;
char op[];
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
scanf("%d", &g[i][j]);
scanf("%d%d%d%d%s", &tx, &ty, &rx, &ry, op);
if (!CanGo(rx, ry))
{
printf("-1\n");
continue;
}
if (op[] == 'n')
d = ;
else if (op[] == 'e')
d = ;
else if (op[] == 's')
d = ;
else
d = ;
printf("%d\n", BFS(tx, ty, d));
}
}

POJ 1376 Robot的更多相关文章

  1. 模拟 POJ 1573 Robot Motion

    题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...

  2. Robot POJ - 1376

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  3. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  4. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

  5. POJ 1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12978   Accepted: 6290 Des ...

  6. poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】

                                                                                                         ...

  7. POJ 1573 Robot Motion 模拟 难度:0

    #define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...

  8. poj 1573 Robot Motion_模拟

    又是被自己的方向搞混了 题意:走出去和遇到之前走过的就输出. #include <cstdlib> #include <iostream> #include<cstdio ...

  9. poj 1367 robot(搜索)

    题意:给你一个图,求起点 到 终点的最少时间 每次有两种选择①:往前走1~3步                ②原地选择90°   费时皆是1s 图中1为障碍物,而且不能出边界.还要考虑机器人的直径 ...

随机推荐

  1. 一款使用C# .NET开发的SIP网络电话客户端完整源码

    一款使用C# .NET开发的SIP客户端开源项目.测试可编译通过运行,特此分享一下.可以对接ASTGO.VOS2009.VOS3000.NTS.UCS等各种SIP协议的软交换! 下载地址:https: ...

  2. ubuntu下的路由实验

    这个实验先演示两个client是如何通过路由器进行通信的. 我们至少需要三个虚拟机:clientA.clientB和route. 对clientA的网卡进行设置: #the primary netwo ...

  3. flux,redux,vuex状态集管理工具之间的区别

    一:redux和flux的区别 1)redux是flux中的一个实现 2))在redux中我们只能定义一个store,在flux中我们可以定义多个 3)在redux中,store和dispatch都放 ...

  4. akka设计模式系列-Backend模式

    上一节我们介绍了Akka使用的基本模式,简单点来说就是,发消息给actor,处理结束后返回消息.但这种模式有个缺陷,就是一旦某个消息处理的比较慢,就会阻塞后面所有消息的处理.那么有没有方法规避这种阻塞 ...

  5. ajax 以json 的形式来传递返回参数的实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestWcf.aspx.c ...

  6. linux命令(001) -- chkconfig

    一.准备知识 在说明chkconfig命令的用途之前,有必要先了解一下Linux系统中/etc/rc[0-6].d目录的用途. 众所周知,在Linux系统定义了7种不同的启动级别,这7种启动级别的含义 ...

  7. [hihocoder][Offer收割]编程练习赛57

    1-偏差排列 斐波那契数列 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> ...

  8. Java中常用的操作PDF的类库

    iText iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好的给合.使用 ...

  9. 清瘦的记录者: 一个比dbutils更小巧、好用的的持久化工具

    https://gitee.com/bitprince/memory 1. 概述 1.1 连接.语句和结果集 从JDBC的规范上看,其对数据访问层有相当简洁的抽象:1.连接(connection) 2 ...

  10. XSS攻击前端需注意

    XSS攻击,在WEB安全领域已经是老生常谈的问题,每每提到安全问题,也会首当其冲拿出来说事. 针对XSS攻击的解决方案,也非常成熟,主要就是:对用户输入信息的地方进行转义处理,当然我这里要提起的一个点 ...