1044独轮车(广搜)

1044.独轮车

时限:1000ms 内存限制:10000K  总时限:3000ms

描述

独轮车的轮子上有红、黄、蓝、白、绿(依顺时针序)5种颜色,在一个如下图所示的20*20的迷宫内每走一个格子,轮子上的颜色变化一次。独轮车只能向前推或在原地转向。每走一格或原地转向90度均消耗一个单位时间。现给定一个起点(S)和一个终点(T),求独轮车以轮子上的指定颜色到达终点所需的最短时间。

输入

本题包含一个测例。测例中分别用一个大写字母表示方向和轮子的颜色,其对应关系为:E-东、S-南、W-西、N-北;R-红、Y-黄、B-蓝、W-白、G-绿。在测试数据的第一行有以空格分隔的两个整数和两个大写字母,分别表示起点的坐标S(x,y)、轮子的颜色和开始的方向,第二行有以空格分隔的两个整数和一个大写字母,表示终点的坐标T(x,y)和到达终点时轮子的颜色,从第三行开始的20行每行内包含20个字符,表示迷宫的状态。其中'X'表示建筑物,'.'表示路.

输出

在单独的一行内输出一个整数,即满足题目要求的最短时间。

输入样例

3 4 R N
15 17 Y
XXXXXXXXXXXXXXXXXXXX
X.X...XXXXXX......XX
X.X.X.....X..XXXX..X
X.XXXXXXX.XXXXXXXX.X
X.X.XX....X........X
X...XXXXX.X.XX.X.XXX
X.X.XX....X.X..X.X.X
X.X.X..XX...XXXX.XXX
X.X.XX.XX.X....X.X.X
X.X....XX.X.XX.X.X.X
X.X.X.XXXXX.XX.X.XXX
X.X.X.XXXXX....X...X
X.X.......X.XX...X.X
X.XXX.XXX.X.XXXXXXXX
X.....XX.......X...X
XXXXX....X.XXXXXXX.X
X..XXXXXXX.XXX.XXX.X
X.XX...........X...X
X..X.XXXX.XXXX...XXX
XXXXXXXXXXXXXXXXXXXX

输出样例

56

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<string>
using namespace std;
char a[20][20];
int direction[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
int book[20][20][4][5];
int head,tail;
char maps[5]={'R','Y','B','W','G'};
char sc,ec,sdir;
int ex,ey;
int len=300;
struct node
{
    int row;
    int col;
    int dir;
    int  color;
    int num;
}s[300];
int used(struct node x)
{
    if(book[x.row][x.col][x.dir][x.color]==0)return 0;
    else return 1;
}
int judge(struct node x)
{
    if(x.row<=0||x.col<=0||x.row>20||x.col>20||a[x.row][x.col]=='X')
    {
        return 0;
    }
    return 1;
}
int isaim(struct node x)
{
    if(x.row==ex&&x.col==ey&&maps[x.color]==ec)
    {
        return 1;
    }
    else
        return 0;
}
struct node takeout()
{
    struct node x;
    x=s[head++];
    head=head%len;
    return x;
}
struct node moveahead(struct node x)
{
    struct node y;
    y.row=x.row+direction[x.dir][0];
    y.col=x.col+direction[x.dir][1];
    y.color=(x.color+1)%5;
    y.dir=x.dir;
    y.num=x.num+1;
    return y;
}
struct node turnleft(struct node x)
{
    struct node y;
    y=x;
    y.dir=(x.dir+1)%4;
    y.num=x.num+1;
    return y;
}
struct node turnright(struct node x)
{
     struct node y;
     y=x;
     y.dir=(x.dir+3)%4;
     y.num=x.num+1;
     return y;
}
void adds(struct node x)
{
     s[tail++]=x;
     tail=(tail)%len;
     book[x.row][x.col][x.dir][x.color]=1;
}

int bfs()
{
    struct node x,y;
    while(head!=tail)
    {
        x=takeout();
        y=moveahead(x);
        if(judge(y))
        {
            if(isaim(y))
            {
                return y.num;
            }
            if(!used(y))
            {
                adds(y);
            }
        }
        y=turnleft(x);
        if(used(y)==0)
        {
            adds(y);
        }
        y=turnright(x);
        if(used(y)==0)
        {
            adds(y);
        }
    }
    return -1;
}
int main()
{
    struct node s;
    memset(book,0,sizeof(book));
    memset(a,0,sizeof(a));
    cin>>s.row>>s.col>>sc>>sdir;
    if(sc=='R')s.color=0;
    else if(sc=='Y')s.color=1;
    else if(sc=='B')s.color=2;
    else if(sc=='W')s.color=3;
    else if(sc=='G')s.color=4;

    if(sdir=='N')s.dir=0;
    else if(sdir=='W')s.dir=1;
    else if(sdir=='S')s.dir=2;
    else if(sdir=='E')s.dir=3;
    cin>>ex>>ey>>ec;
    for(int i=1;i<=20;i++)
        for(int j=1;j<=20;j++)
    {
        cin>>a[i][j];
    }
    head=tail=0;
    s.num=0;
    adds(s);
    cout<<bfs()<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/rain699/article/details/82924392