Sicily--1471. No Left Turns(BFS)

来源:互联网 发布:c语言24关键字 编辑:程序博客网 时间:2024/06/06 04:04

题意重述

一个迷宫,从起点走到终点,走一步有直走,以及直走再右转两个选择(注意不能原地转弯),问在这种情况下的最短路径是多少,起始方向可以任意选择,题目中保证这样的路径一定存在

题目分析与算法选择

既然要搜索最短的路径,那就是用的bfs了,因为这里状态每深入一层,路径就增加固定的值1,因此对状态由浅到深搜索,得出的就是最短的路径。

具体实现

状态的确定
首先,在网格地图里移动,状态肯定包括所在点的坐标,而且由于“Right Head”的下一步走法与当前的方向相关,所以方向也是状态的一个成员。同时,用一个数组steps来储存初始状态到当前状态的路径长,当状态向下深入一层时,就把深层的状态的steps值设置为上一层的steps加一。这样当找到终点时,当时状态的steps值就是初始状态到终点的最短路径

剪枝
如果不进行剪枝,直接最原始的广搜的话,会产生很多没有用的搜索过程,例如下图中从最初状态分出两条路线,分别是绿色路线和蓝色路线。蓝色路线在第二层(箭头数字2)中产生了一个位置在(3,1),方向向左的状态;而绿色路线在第四层(箭头数字4)中同样产生了一个位置在(3,1),方向向左的状态,很明显蓝色路线在该处产生的状态的路径比绿色路径在此产生的状态的路径短(2 < 4),因此绿色路径的状态(3,1,左)不应该再向下搜索。
这里写图片描述

代码如下:

#include<stack>#include<iostream>#include<fstream>#include<cmath>#include<string>#include<cstdio>#include<algorithm>#include<sstream>#include<iomanip>#include <cstring>#include <cstdlib>#include <vector>#include <map>#include <queue>#include <set>using namespace std;struct status {    int x;    int y;    int dir;    status(int _x, int _y, int _dir)    {        x = _x, y = _y, dir = _dir;    }};char maze[21][21];int directions[4][2] = { {-1,0},{0,1},{ 1,0 },{ 0,-1 } };//up, right, down, leftint steps[21][21][21];queue<status> que;int test;int* dir2pair(int dir)//0 for up, 1 for right, 2 for down, 3 for left{    dir = dir % 4;    return directions[dir];}int dfs(int x, int y, int dir){    memset(steps, 1, sizeof(steps));//steps[][][] = 16843009 = (2进制)00000001 00000001 00000001 00000001    steps[x][y][dir] = 0;    status sta = status(x, y, dir);    que.push(sta);    while (!que.empty())    {        status s = que.front(); que.pop();        int x = s.x; int y = s.y; int dir = s.dir;        for (int d = dir; d <= dir + 1; d++)        {            int dir_real = d % 4;            int* dirpair = dir2pair(dir_real);            int next_x = x + dirpair[0]; int next_y = y + dirpair[1];            if (maze[next_x][next_y] == 'F')                return steps[x][y][dir] + 1;            //steps[next_x][next_y][dir_real] > steps[x][y][dir] + 1 在此之前没有出现更少的步数,才能更新。 这句用来剪枝             if (maze[next_x][next_y] != 'X' && steps[next_x][next_y][dir_real] > steps[x][y][dir] + 1)            {                steps[next_x][next_y][dir_real] = steps[x][y][dir] + 1;                status sta = status(next_x,next_y,dir_real);                que.push(sta);            }        }    }    return 9999999;}int main(){    int n, r, c;    cin >> n;    int sx, sy, fx, fy;    while (n--)     {        while (!que.empty())            que.pop();        cin >> r >> c;        for (int i = 0; i < r; i++)        {            getchar();                  //将换行符读掉             for (int k = 0; k < c; k++)            {                cin.get(maze[i][k]);    //get方法能读取空格                 if (maze[i][k] == 'S')                    sx = i, sy = k;                else if (maze[i][k] == 'F')                    fx = i, fy = k;            }        }        int min_res = 99999999;        for (int i = 0; i < 4; i++)        {            int res = dfs(sx, sy, i);            min_res = min_res < res ? min_res : res;        }        cout << min_res << endl;    }}
0 0