POJ 1736 Robot BFS

本文介绍了一道关于机器人在网格地图中寻找从起点到终点最短路径的问题,并使用BFS算法解决。机器人只能沿直线轨道移动且每次移动或转向消耗一秒。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Robot
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7561 Accepted: 2444

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。这个题注意有4点:

  1、它一秒可以执行2种命令,一种是向现在所面向的方向走1-3步,另外一种是向左或向右90度转向(不能向后转)。

  2、图中为1的是障碍物,是不允许通过的,包括边界也不能允许,这一点需要注意下。

  题目分析:这道题就是简单BFS,  有五种转变方式, 走1步, 走2步, 走三步, 向左转, 向右转, 将这五种状态分别推入队列BFS,  记住当走一步都超出边界的时候, 再多走肯定超出边界,

 可以break掉。然后就是细节的问题了, 就是说做这种题如何快速简单的建模非常重要, 将题意转化成好的程序语言表达方式, 这点需要靠多多做题来巩固。

  代码:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;

struct Node {
    int x;
    int y;
    int time;
    int pos;
};
int m, n;
int map[100][100];
int vis[100][100][5];
int B1, B2, E1, E2;
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
string original_position;
queue<Node> Q;

int getPos( string str ) {
    if( str == "north" ) return 0;
    else if( str == "east" ) return 1;
    else if( str == "south" ) return 2;
    return 3;
}

bool canGo( int x, int y ) {
    if( x < 1 || x >= n || y < 1 || y >= m ) return false;
    if( map[x][y] || map[x+1][y] || map[x][y+1] || map[x+1][y+1] ) {
        return false;
    }
    return true;
}

int bfs() {
    Node start;
    start.x = B1;
    start.y = B2;
    start.time = 0;
    start.pos = getPos( original_position );
    Q.push( start );
    vis[start.x][start.y][start.pos] = 1;
    while( !Q.empty() ) {
        
        Node vn = Q.front();
        Q.pop();
        
        int x = vn.x;
        int y = vn.y;
        int time = vn.time;
        int pos = vn.pos;
        if( x == E1 && y == E2 ) return time;
        for( int i = 1; i <= 3; i++ ) {
            x += dx[vn.pos];
            y += dy[vn.pos];
            if( !canGo( x, y ) ) {
                break;
            }
            
            if( !vis[x][y][vn.pos] ) {
                Node next;
                next.x = x;
                next.y = y;
                next.pos = vn.pos;
                next.time = time + 1;
                vis[next.x][next.y][next.pos] = 1;
                Q.push( next );
            }
        }
        for( int i = 0; i < 4; i++ ) {
            if( max( pos, i ) - min( pos, i ) == 2 ) {
                continue;
            }
            if( vis[vn.x][vn.y][i] ) continue;
            Node next;
            next.x = vn.x;
            next.y = vn.y;
            next.pos = i;
            next.time = time + 1;
            vis[next.x][next.y][next.pos] = 1;
            Q.push( next );
        }
        
    }
    return -1;
}

int main() {
    while( cin >> n >> m && ( n || m ) ) {
        while( !Q.empty() ) Q.pop();
        memset( map, 0, sizeof( map ) );
        memset( vis, 0, sizeof( vis ) );
        for( int i = 1; i <= n; i++ ) {
            for( int j = 1; j <= m; j++ ) {
                cin >> map[i][j];
            }
        }
        cin >> B1 >> B2 >> E1 >> E2 >> original_position;
        cout << bfs() << endl;
    }
    return 0;
}
View Code

 

  最近很少写博客了, 注重思维多刷题, 不要懈怠啊, 人一我十, 人十我万!

转载于:https://www.cnblogs.com/FriskyPuppy/p/6031842.html

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值