zoukankan      html  css  js  c++  java
  • 机器人搬重物

    机器人搬重物

    题目描述

    机器人移动学会(RMI)现在正尝试用机器人搬运物品。机器人的形状是一个直径1.6米的球。在试验阶段,机器人被用于在一个储藏室中搬运货物。储藏室是一个N*M的网格,有些格子为不可移动的障碍。机器人的中心总是在格点上,当然,机器人必须在最短的时间内把物品搬运到指定的地方。机器人接受的指令有:先前移动1步(Creep);向前移动2步(Walk);向前移动3步(Run);向左转(Left);向右转(Right)。每个指令所需要的时间为1秒。请你计算一下机器人完成任务所需的最少时间。

    输入

    输入的第一行为两个正整数N,M(N,M<=50),下面N行是储藏室的构造,0表示无障碍,1表示有障碍,数字之间用一个空格隔开。接着一行有四个整数和一个大写字母,分别为起始点和目标点左上角网格的行与列,起始时的面对方向(东E,南S,西W,北N),数与数,数与字母之间均用一个空格隔开。终点的面向方向是任意的。

    输出

    一个整数,表示机器人完成任务所需的最少时间。如果无法到达,输出-1。

    样例输入

    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 S
    

    样例输出

    12
    分析:经典的bfs,要注意细节;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=51;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,a[maxn][maxn],vis[maxn][maxn][4],sx,sy,ex,ey;
    char d;
    inline int gao(char p)
    {
        if(p=='S')return 3;
        if(p=='N')return 1;
        if(p=='E')return 0;
        if(p=='W')return 2;
    }
    bool check(int x,int y)
    {
        if(x>=0&&x<n&&y>=0&&y<m&&a[x][y]==0);
        else return false;
        if(x+1>=0&&x+1<n&&y>=0&&y<m&&a[x+1][y]==0);
        else return false;
        if(x>=0&&x<n&&y+1>=0&&y+1<m&&a[x][y+1]==0);
        else return false;
        if(x+1>=0&&x+1<n&&y+1>=0&&y+1<m&&a[x+1][y+1]==0);
        else return false;
        return true;
    }
    struct node
    {
        int x,y,dir;
        node(int xx,int yy,char dirr)
        {
            x=xx,y=yy,dir=dirr;
        }
    };
    queue<node>p;
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,0,n-1)rep(j,0,m-1)scanf("%d",&a[i][j]);
        scanf("%d%d%d%d %c",&sx,&sy,&ex,&ey,&d);
        sx--,sy--,ex--,ey--;
        if(sx==ex&&sy==ey)return 0*puts("0");
        node q(sx,sy,gao(d));
        p.push(q);vis[sx][sy][gao(d)]=1;
        while(!p.empty())
        {
            node u=p.front();p.pop();
            node v=u;
            for(int i=1;i<=3;i++)
            {
                v.x+=dis[v.dir][0];
                v.y+=dis[v.dir][1];
                if(!check(v.x,v.y))break;
                if(!vis[v.x][v.y][v.dir])
                {
                    p.push(v);
                    vis[v.x][v.y][v.dir]=vis[u.x][u.y][u.dir]+1;
                    if(v.x==ex&&v.y==ey)return 0*printf("%d
    ",vis[u.x][u.y][u.dir]);
                }
            }
            v=u;
            v.dir=(u.dir+3)%4;
            if(!vis[v.x][v.y][v.dir])
            {
                p.push(v);
                vis[v.x][v.y][v.dir]=vis[u.x][u.y][u.dir]+1;
            }
            v=u;
            v.dir=(u.dir+1)%4;
            if(!vis[v.x][v.y][v.dir])
            {
                p.push(v);
                vis[v.x][v.y][v.dir]=vis[u.x][u.y][u.dir]+1;
            }
        }
        puts("-1");
        //system("pause");
        return 0;
    }
  • 相关阅读:
    [置顶] 一个懦弱的IT人
    Android ListView的理解(一)
    不允许调用库函数,也不允许使用任何全局或局部变量编写strlen函数
    http-使用get和post方式提交数据
    ILOG的一个基本应用——解决运输问题、转运问题
    原生js获取execl里面的值 主要使用ActiveXObject
    (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)
    HDU 3032 Nim or not Nim? (sg函数)
    Hadoop入门实践之从WordCount程序说起
    仅复制备份(简单恢复模式)
  • 原文地址:https://www.cnblogs.com/dyzll/p/5777687.html
Copyright © 2011-2022 走看看