Cow Navigation

来源:互联网 发布:查看手机ip和端口 编辑:程序博客网 时间:2024/06/07 05:31

链接:

  http://exam.upc.edu.cn/problem.php?id=3427


题目:

题目描述

Bessie has gotten herself stuck on the wrong side of Farmer John’s barn again, and since her vision is so poor, she needs your help navigating across the barn.
The barn is described by an N×N grid of square cells (2≤N≤20), some being empty and some containing impassable haybales. Bessie starts in the lower-left corner (cell 1,1) and wants to move to the upper-right corner (cell N,N). You can guide her by telling her a sequence of instructions, each of which is either “forward”, “turn left 90 degrees”, or “turn right 90 degrees”. You want to issue the shortest sequence of instructions that will guide her to her destination. If you instruct Bessie to move off the grid (i.e., into the barn wall) or into a haybale, she will not move and will skip to the next command in your sequence.

Unfortunately, Bessie doesn’t know if she starts out facing up (towards cell 1,2) or right (towards cell 2,1). You need to give the shortest sequence of directions that will guide her to the goal regardless of which case is true. Once she reaches the goal she will ignore further commands.

输入

The first line of input contains N.
Each of the N following lines contains a string of exactly N characters, representing the barn. The first character of the last line is cell 1,1. The last character of the first line is cell N, N.

Each character will either be an H to represent a haybale or an E to represent an empty square.

It is guaranteed that cells 1,1 and N,N will be empty, and furthermore it is guaranteed that there is a path of empty squares from cell 1,1 to cell N,N.

输出

On a single line of output, output the length of the shortest sequence of directions that will guide Bessie to the goal, irrespective whether she starts facing up or right.

样例输入

3
EHE
EEE
EEE

样例输出

9

提示

In this example, the instructions “Forward, Right, Forward, Forward, Left, Forward, Left, Forward, Forward” will guide Bessie to the destination irrespective of her starting orientation.


题意:

  有一头牛在左下角,要去右上角。现在有三种指令,向左转,向右转,前进。这头牛开始不知道自己面朝哪里,如果你的命令会让他撞墙的话他就会自动忽略这条命令。问你在不知道他最开始朝向的情况下最少需要多少条指令可以让他到达右上角。


思路:

  六维BFS,模拟两头牛,一头面朝上,一头面朝右,分别记录两头牛的x,y坐标和朝向。


实现:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<queue>#include<stack>#include<set>#include<map>#include<cmath>#include<algorithm>#define LL long long#define INF 0x3f3f3f3fusing namespace std;struct node{    int h1,l1;    int h2,l2;    int direct1,direct2;    int command;    node(){}    node(int h1,int l1,int h2,int l2,int d,int direct1,int direct2):h1(h1),l1(l1),h2(h2),l2(l2),command(d),direct1(direct1),direct2(direct2){}};const int turn[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int n;char a[27][27];char b[27][27];int visited[27][27][27][27][4][4];int judge(int x,int y){    if(0<=x && x<n && 0<=y && y<n) return 1;    return 0;}int bfs(){    memset(visited,0,sizeof(visited));    queue<node> q;    q.push(node(0,0,0,0,0,0,2));    while(!q.empty())    {        node u=q.front(); q.pop();        int h1=u.h1,l1=u.l1,h2=u.h2,l2=u.l2,d=u.command,direct1=u.direct1,direct2=u.direct2;        if(visited[h1][l1][h2][l2][direct1][direct2]) continue;        visited[h1][l1][h2][l2][direct1][direct2]=1;        if(h1==n-1 && l1==n-1 && h2==n-1 && l2==n-1) return d;        int direct1x,direct1y,direct2x,direct2y;        if(direct1==0) direct1x=3,direct1y=2;        if(direct1==1) direct1x=2,direct1y=3;        if(direct1==2) direct1x=0,direct1y=1;        if(direct1==3) direct1x=1,direct1y=0;        if(direct2==0) direct2x=3,direct2y=2;        if(direct2==1) direct2x=2,direct2y=3;        if(direct2==2) direct2x=0,direct2y=1;        if(direct2==3) direct2x=1,direct2y=0;        q.push(node(h1,l1,h2,l2,d+1,direct1x,direct2x));        q.push(node(h1,l1,h2,l2,d+1,direct1y,direct2y));        int dh1=h1+turn[direct1][0];        int dl1=l1+turn[direct1][1];        int dh2=h2+turn[direct2][0];        int dl2=l2+turn[direct2][1];        if(!judge(dh1,dl1) && !judge(dh2,dl2)) continue;        if(a[dl1][dh1]=='H' && a[dl2][dh2]=='H') continue;        if(a[dl1][dh1]=='H') dh1=h1,dl1=l1;        if(a[dl2][dh2]=='H') dh2=h2,dl2=l2;        if(!judge(dh1,dl1)) dh1=h1,dl1=l1;        if(!judge(dh2,dl2)) dh2=h2,dl2=l2;        if(h1==n-1 && l1==n-1) dh1=h1,dl1=l1;        if(h2==n-1 && l2==n-1) dh2=h2,dl2=l2;        q.push(node(dh1,dl1,dh2,dl2,d+1,direct1,direct2));    }    return -1;}int main(){    scanf("%d",&n);    for(int i=0;i!=n;++i)        scanf("%s",b[i]);    for(int i=0;i!=n;++i)        memcpy(a[i],b[n-i-1],sizeof(b[n-i-1]));    int res=bfs();    printf("%d\n",res);}
原创粉丝点击