寒假训练——搜索——C - Robot

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

题目大意:就是给机器人编一个行走的代码,求最短的时间,其实就是最短路。这个有点不同,第一个是有方向,第二个是他是一个圆,不是点有面积。思路:用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;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10354842.html

时间: 2024-10-14 14:17:06

寒假训练——搜索——C - Robot的相关文章

寒假训练——搜索 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 cell (i,ji,j ) is ai,jai,j . Your task is to calculate the number of paths from the upper-left cell (1,11,1 ) to the bottom-right cell (n,mn,m ) meet

NUC-ACM/ICPC 寒假训练 简单DP A - G题

第一题:数塔 HDU - 2084 做法: 从第 i , j 个 节点往下走的最优解可以由从第 i+1,j 个节点往下走的最优解和第i+1,j+1个节点往下走的最优解得出,二者取其优即可. 代码: 记忆化搜素 1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 using namespace std; 5 int n; 6 int f[100][100]; 7 int v[100][100]; 8 int

常州大学新生寒假训练会试 题解

[题目链接] A - 添加逗号 注意是从后往前三个三个加逗号,最前面不允许有逗号 #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; char s[maxn]; char ans[maxn]; int sz; int main() { scanf("%s", s); int len = strlen(s); sz = 0; int t = 0; for(int i = len -

寒假集训——搜索 B - Sudoku

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; char s[10][10]; int panduan(int row,int cew) { for(int i=0;i<4;i++) { if(s[row][i]==s[row][cew]&&i!=cew) return 0; } for

FZU ICPC 2020 寒假训练 4 —— 模拟(一)

P1042 乒乓球 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华就是其中一位,他退役之后走上了乒乓球研究工作,意图弄明白11分制和21分制对选手的不同影响.在开展他的研究之前,他首先需要对他多年比赛的统计数据进行一些分析,所以需要你的帮忙. 题目描述 华华通过以下方式进行分析,首先将比赛每个球的胜负列成一张表,然后分别计算在11分制和21分制下,双方的比赛结果(

FJUT2017寒假训练二题解

A题 题意:让你找出唯一的一个四位数,满足对话时的要求. 思路:因为是4位数,可以直接从1000-9999遍历一遍,判断是否有唯一的数能满足所有条件,如果不是唯一的或者没有满足条件的数就输出Not sure.特别丑的代码附上... 1 #include<stdio.h> 2 int a[10000],b[10000],c[10000]; 3 int main() 4 { 5 int n; 6 while(~scanf("%d",&n)) 7 { 8 if(n==0)

寒假训练第九场 Brocard Point of a Triangle

题意:求布洛卡点坐标 思路:直接利用布洛卡点的性质.http://pan.baidu.com/s/1eQiP76E 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<cstdlib> 6 #include<cstring> 7 #include<cmath> 8 #include<v

zsc_寒假训练 8

Description Ignatius was born in a leap year, so he want to know when he could hold his birthday party. Can you tell him? Given a positive integers Y which indicate the start year, and a positive integer N, your task is to tell the Nth leap year from

寒假训练5解题报告

1.HDU 1114 Piggy Bank 一道简单的背包问题 #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define ll long long const int N = 10005; const int INF = 0x3f3f3f3f; int dp[N]; int main() { // freopen("a.in" , &qu