廣度優先搜索--推箱子

題目描述:

大家一定玩過“推箱子”這個經典的遊戲。具體規則就是在一個N*M的地圖上,有1個玩家、1個箱子、1個目的地以及若干障礙,其餘是空地。玩家可以往上下左右4個方向移動,但是不能移動出地圖或者移動到障礙裏去。如果往這個方向移動推到了箱子,箱子也會按這個方向移動一格,當然,箱子也不能被推出地圖或推到障礙裏。當箱子被推到目的地以後,遊戲目標達成。現在告訴你遊戲開始是初始的地圖佈局,請你求出玩家最少需要移動多少步才能夠將遊戲目標達成。

輸入描述:
每個測試輸入包含1個測試用例
第一行輸入兩個數字N,M表示地圖的大小。其中0<N,M<=8。
接下來有N行,每行包含M個字符表示該行地圖。其中 . 表示空地、X表示玩家、*表示箱子、#表示障礙、@表示目的地。
每個地圖必定包含1個玩家、1個箱子、1個目的地。


輸出描述:
輸出一個數字表示玩家最少需要移動多少步才能將遊戲目標達成。當無論如何達成不了的時候,輸出-1。

輸入例子1:
4 4
....
..*@
....
.X..
6 6
...#..
......
#*##..
..##.#
..X...
.@#...

輸出例子1:
3
11

#include<iostream>
#include<queue>
#include<string>
using namespace std;
int state[10][10][10][10];//四維數組表示人和箱子的位置狀態,開始全爲0

struct q
{
    int px, py, bx, by;
    q(int x, int y, int bx, int by) :px(x), py(y), bx(bx), by(by) {}
};
int moves[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };//四個方向
char map[10][10];//地圖數組
int chx, chy, cbx, cby, ex, ey, n, m;//分別表示當前人的位置,盒子的位置,終點位置,以及地圖尺寸。

bool bound(int x,int y)//邊界檢查,遇到不合理的位置返回真
{
    if (x < 0 || y < 0 || x >= n || y >= m || map[x][y] == '#')
        return true;
    else
        return false;
}
int bfs()
{
    state[chx][chy][cbx][cby] = 1;//當前其實狀態位置設步數爲1
    q temp(chx, chy, cbx, cby);
    queue<q> que; //狀態隊列
    que.push(temp);//初始狀態入棧
    while (que.size()) //只要隊列不爲空就一直尋找
    {
        temp = que.front();//獲取首元素
        que.pop();//首元素彈出
        if (temp.bx == ex&&temp.by == ey)
            return state[temp.px][temp.py][temp.bx][temp.by]-1;//如果箱子在終點,結束,返回步數
        for (int i = 0; i < 4; i++)//四個方向開始搜索了
        {
            //先更新人的位置
            int px = temp.px + moves[i][0];
            int py = temp.py + moves[i][1];
            if (bound(px, py))
                continue;//如果這個位置非法,探尋其它方向
            if (px == temp.bx&&py == temp.by)//如果此時人的位置與箱子的位置重合,說明人應當推動了箱子
            {
                if (bound(temp.bx + moves[i][0], temp.by + moves[i][1]))
                    continue;//如果箱子移動的位置不合法,則重新探尋其它方向
                state[px][py][temp.bx + moves[i][0]][temp.by + moves[i][1]] =
                    state[temp.px][temp.py][temp.bx][temp.by] + 1;//箱子推動,則人和箱子位置改變,記錄新狀態
                que.push(q(px, py, temp.bx + moves[i][0], temp.by + moves[i][1]));//新狀態入棧

            }
            else//人沒有推動箱子
            {
                if (state[px][py][temp.bx][temp.by])//如果移動後的狀態出現過,則重新搜索新方向
                    continue;
                state[px][py][temp.bx][temp.by] = state[temp.px][temp.py][temp.bx][temp.by] + 1;
                //沒有走過這條路就走着試試
                que.push(q(px, py, temp.bx, temp.by));//更新狀態


            }

        }
    }
    return -1;//如果所有位置都試過了,沒有找到,說明不存在

}


int main()
{
    cin >> n >> m;
    //cin.clear();
    for (int i = 0; i < n; i++) {
        scanf("%s", map[i],m+1);
    }
    for (int i = 0; i < n; i++)//初始化人,箱子,終點的位置
    {
        for (int j = 0; j < m; j++)
        {
            if (map[i][j]=='*')
            {
                cbx = i;
                cby = j;
            }
            else if (map[i][j] == 'X'){
                chx = i;
                chy = j;
            }
            else if (map[i][j] == '@')
            {
                ex = i;
                ey = j;
            }
        }
    }
    cout << bfs() << endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章