1130 POJ#1376 Robot

摘要
-模拟机器人行走,找到耗时最短路径
原题目摘要
-POJ1376 Robot http://poj.org/problem?id=1376

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进行最短路的寻找;
注意
去过的状态不要重复
旋转方向的控制量范围保持好
机器人行走中的道路是否可行检测
日期
-2017 11 30
附加
-
代码
-
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <memory>
#include <list>
#define MAX 50
using namespace std;

int m,n;
int b1,b2,e1,e2,d0;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

struct Status{
    bool dir[4];
    void init(){dir[0]=dir[1]=dir[2]=dir[3]=false;}
};
struct QStatus{// single state store in queue
    int _x, _y;
    int d;//direction
    int step;
};
bool Matrix[MAX][MAX];
Status Vis_Matrix[MAX][MAX];
bool check_point_border(int x,int y){
    return x>=0&&x<m
        && y>=0&&y<n;
}
bool vis_check_path(int x,int y,int d){//可以前行
    return check_point_border(x-1,y-1)&& !Matrix[x-1][y-1]
        && check_point_border(x-1,y)&& !Matrix[x-1][y ]
        && check_point_border(x,y-1)&& !Matrix[x ][y-1]
        && check_point_border(x,y)&& !Matrix[x ][y ];
}



bool Go(QStatus qs,int length){
    if(vis_check_path(qs._x+length*dx[qs.d],qs._y+length*dy[qs.d],qs.d)) return true;
    else return false;
}
bool Turn(QStatus &qs,int ro){
    qs.d=(qs.d+ro+4)%4;
    return !Vis_Matrix[qs._x][qs._y].dir[qs.d] ;
}

void addStatus(queue<QStatus> &q,QStatus qs){
    qs.d = (qs.d+4)%4;
    if(!Vis_Matrix[qs._x][qs._y].dir[qs.d]){
        Vis_Matrix[qs._x][qs._y].dir[qs.d] = true;
        q.push(qs);
//cout<<"\tADD:"<<qs._x<<" "<<qs._y<<" dir:"<<qs.d<<" step:"<<qs.step<<endl;
    }
}
int find_min_path(){
    queue<QStatus> Q;
    addStatus(Q,{b1,b2,d0,0});

    while(!Q.empty()){
        QStatus &qs = Q.front();
//cout<<qs._x<<" "<<qs._y<<" dir:"<<qs.d<<" step:"<<qs.step<<endl;
        if(qs._x==e1&&qs._y==e2){
            return qs.step;
        }
        for(int i=1;i<=3;i++){
             if(Go(qs,i)){//只要可行 就要测试 2,3
                addStatus(Q,{qs._x+i*dx[qs.d],qs._y+i*dy[qs.d],qs.d,qs.step+1});
             }else break;
        }
        addStatus(Q,{qs._x,qs._y,qs.d-1,qs.step+1});
        addStatus(Q,{qs._x,qs._y,qs.d+1,qs.step+1});
        Q.pop();
    }
    return -1;
}

void solve(){
    int tmp;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&tmp);
            Matrix[i][j]=tmp;
            Vis_Matrix[i][j].init();
        }
    }
    scanf("%d%d%d%d",&b1,&b2,&e1,&e2);
    char dir[5];
    scanf("%s",dir);
    switch(dir[0]){
        case 'n':d0=0;break;
        case 'e':d0=1;break;
        case 's':d0=2;break;
        case 'w':d0=3;break;
    }
    if(b1==e1&&b2==e2) printf("0\n");
    else{
        int ans = find_min_path();
        if(ans==0) printf("-1\n");
        else printf("%d\n",ans);
    }
}

int main(){
   
    while(scanf("%d%d",&m,&n)){
        if(m>0 && n>0) solve();
        else break;
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值