zoukankan      html  css  js  c++  java
  • ZOJ1310-Robot (BFS)

    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 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 #include <queue>
     6 #define ll long long
     7 using namespace std;
     8 const int MAX = 55;
     9 const int INF = 1<<30;
    10 const int dir[4][2]{{-1,0},{0,1},{1,0},{0,-1}};
    11 struct Nod{
    12     int x,y,t,d;
    13     bool operator < (const Nod &a) const{
    14         return t > a.t;
    15     }
    16 };
    17 int g[MAX][MAX], n, m, mark[MAX][MAX][4];
    18 int bfs(int bx, int by, int ex, int ey, int f) {
    19     priority_queue<Nod> que;
    20     Nod nod;
    21     mark[bx][by][f] = 0;
    22     que.push((Nod){bx,by,0,f});
    23     while(!que.empty()) {
    24         nod = que.top();
    25         que.pop();
    26         int x = nod.x, y = nod.y;
    27         if(x == ex && y == ey) return nod.t;
    28         int d = nod.d;
    29         if(mark[x][y][(d+1)%4] == -1 || mark[x][y][(d+1)%4] > nod.t + 1){
    30             mark[x][y][(d+1)%4] = nod.t+1;
    31             que.push((Nod){x,y,nod.t+1,(d+1)%4});
    32         }
    33         if(mark[x][y][(d-1+4)%4] == -1 || mark[x][y][(d-1+4)%4] > nod.t + 1){
    34             mark[x][y][(d-1+4)%4] = nod.t + 1;
    35             que.push((Nod){x,y,nod.t+1,(d-1+4)%4});
    36         }
    37         int nx = x, ny = y;
    38         for(int i = 1; i <= 3; i ++) {
    39             nx += dir[d][0];
    40             ny += dir[d][1];
    41             if(nx <= 0 || nx >= m || ny <= 0 || ny >= n)break;
    42             if(g[nx][ny] == 1 || g[nx-1][ny] == 1 || g[nx][ny-1] == 1 || g[nx-1][ny-1] == 1) break;
    43             if(mark[nx][ny][d] == -1 || mark[nx][ny][d] > nod.t+1){
    44                 mark[nx][ny][d] = nod.t+1;
    45                 que.push((Nod){nx,ny,nod.t+1,d});
    46             }
    47         }
    48     }
    49     return -1;
    50 }
    51 int main() {
    52     int bx, by, ex, ey, d, ans;
    53     char str[20];
    54     while(scanf("%d%d",&m,&n)&&(n+m)) {
    55         for(int i = 0; i < m; i ++) {
    56             for(int j = 0; j < n; j ++) {
    57                 scanf("%d",&g[i][j]);
    58             }
    59         }
    60         scanf("%d %d %d %d %s",&bx,&by,&ex,&ey,str);
    61         if(str[0] == 'n') d = 0;
    62         else if(str[0] == 'e') d = 1;
    63         else if(str[0] == 's') d = 2;
    64         else if(str[0] == 'w') d = 3;
    65         memset(mark,-1,sizeof(mark));
    66         ans = bfs(bx,by,ex,ey,d);
    67         printf("%d
    ",ans);
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    安卓四核PDA手持PDA智能POS机 打印二维码 分享
    安卓智能POS开单神器-成为零售批发商亲睐的生意帮手-pda销售扫描开单 现场结算打印凭据
    一个神奇的POS -扫描 现场销售 开单打印票据 安卓物联网POS机 手持开单终端机 省时省力 高效准确!!
    浩瀚土石方车辆管理计数器_刷卡计数器手持式土石方车辆计数器系统方案
    浩瀚ocr数字识别扫描枪 进口冻货抄码器 抄码器 牛羊肉 抄码器 抄码抄码枪 扫码器重量累加
    浩瀚抄码器 冻品扫码枪 扫码机识别数字 进口肉抄码器 牛羊抄码器 冻品抄码
    浩瀚牛肉扫码器 牛羊肉抄码 进口牛肉扫码枪 进口牛羊肉扫码机 抄码系统
    多进程
    不定长参数和进程
    面向对象进阶2
  • 原文地址:https://www.cnblogs.com/wydxry/p/7243565.html
Copyright © 2011-2022 走看看