首页 > 代码库 > HDU 3137 No Left Turns(BFS)

HDU 3137 No Left Turns(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3137


Problem Description


ALL HEADS: You‘re a Knight of the Round Table?

ROBIN: I am.

LEFT HEAD: In that case I shall have to kill you.

MIDDLE HEAD: Shall I?

RIGHT HEAD: Oh, I don‘t think so.

MIDDLE HEAD: Well, what do I think?

LEFT HEAD: I think kill him.

RIGHT HEAD: Well let‘s be nice to him.

MIDDLE HEAD: Oh shut up.

As the story goes, the Knight scarpers off. Right Head has taken it upon himself to search the grounds for the knight so he, Left, and Middle can go extinguish him (and then have tea and biscuits.)

Consider the following 8 by 12 maze, where shaded squares are walls that can’t be entered.


The shortest path between the Right Head (denoted by the S, for start) and the knight (denoted by the F, for finish) is of length 3, as illustrated above. But! Right Head can’t turn left or make UTurns. He can only move forward and turn right. That means the shortest path that Right Head can find is significantly longer: at 29!


 

Input
The input file will consist of a single integer N (N > 0) specifying the number of mazes in the file. Following this, on a maze by maze basis will be the number of rows, r (3 < r <= 20), a space, then the number of columns, c (3 < c <= 20). After this will follow r lines of c characters, representing a map of the maze:
XXXXXXXXXXXXXX
X          XXX
X XFXXXXX    X
XXX   XX  XX X
X S          X
XX  XXXXXX X X
X        X X X
X X      X X X
XXX XX       X
XXXXXXXXXXXXXX

X’s mark those locations that are walls and can’t be occupied. S marks the start location, and F marks the Knight. Blanks are locations that can be freely traveled.
 

Output
The output is the length of the shortest path between the start and finish locations. Based on the above maze, your program would output the minimum no-left-turns path length of 29.

Hint

Additional Constraints/Information:
  • Right Head is capable of moving from the start position in any of the four primary compass directions. After that, he’s constrained to either step forward or right.
  • The start and end locations will never be the same.
  • The maze is always surrounded by four walls.
  • You can assume that a path between the start and final locations always exists.

 

Sample Input
1 10 14 XXXXXXXXXXXXXX X XXX X XFXXXXX X XXX XX XX X X S X XX XXXXXX X X X X X X X X X X X XXX XX X XXXXXXXXXXXXXX
 

Sample Output
29
 

Source
2006 ACM-ICPC Pacific Northwest Region

题意:

最少的步数从S走到F,每次只能走当前方向的右方或者直走!

X代表不能走!

代码如下:

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

const int maxn = 26;
int xx[4] = {-1,0,1,0};
int yy[4] = {0,1,0,-1};
int vis[5][maxn][maxn];
char mm[maxn][maxn];
int n, m;
int x1, y1;//起点
int x2, y2;//终点
struct node
{
    int x;
    int y;
    int step;
    int dir;//标记方向
};
int judge(int a, int b)
{
    if(a>=0 && a<n && b>=0 && b<m)
        return 1;
    return 0;
}
int BFS()
{
    memset(vis,-1,sizeof(vis));
    node frontt, next;
    queue<node>p,q;
    int tx, ty;
    for(int i = 0; i < 4; i++)
    {
        tx = x1 + xx[i];
        ty = y1 + yy[i];
        if(mm[tx][ty] != 'X')
        {
            frontt.x = tx;
            frontt.y = ty;
            frontt.step = 0;
            frontt.dir = i;
            q.push(frontt);
            vis[i][tx][ty] = 1;
        }
    }
    while(!q.empty())
    {
        frontt = q.front();
        q.pop();
        if(frontt.x == x2 && frontt.y == y2)
        {
            return frontt.step+1;
        }
        for(int i = 1; i <= 2; i++)
        {
            if(i == 1)
            {
                next.dir = frontt.dir;//当前方向继续向前
            }
            else
            {
                next.dir = (frontt.dir+1)%4;//向当前方向的右方
            }
            tx = frontt.x + xx[next.dir];
            ty = frontt.y + yy[next.dir];
            next.step = frontt.step+1;
            if(mm[tx][ty]!='X' && judge(tx,ty) && (next.step<vis[next.dir][tx][ty] || vis[next.dir][tx][ty]==-1))
            {
                next.x = tx;
                next.y = ty;
                vis[next.dir][tx][ty] = next.step;
                q.push(next);
            }
        }
    }
}
int main()
{
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        getchar();
        for(int i = 0; i < n; i++)
        {
            gets(mm[i]);
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(mm[i][j] == 'S')
                {
                    x1 = i;
                    y1 = j;
                }
                else if(mm[i][j] == 'F')
                {
                    x2 = i;
                    y2 = j;
                }
            }
        }
        int ans = BFS();
        printf("%d\n",ans);
    }
    return 0;
}



HDU 3137 No Left Turns(BFS)