Nightmare

1.比迷宫问题多了一个reset按钮,算法还是BFS;

2.看解题报告看了很久才看懂,当初自己卡在如何判断该不该走reset这个点;

3.BFS和DFS的题的思路已经清晰,就是每道题在结束循环的条件不同,并且需要用到的变量结构不同而已;

4.开始弄错横纵坐标了,以后只要保持一致就行。


以下是代码,参考别人的,几乎快能背过了。。。

#include <iostream>
#include <queue>
using namespace std;
struct Node
{
    int x;
    int y;
    int remtime;
    int step;
} begin;
int map[8][8];
int mark[8][8];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int m, n;

void bfs()
{
    queue <Node> q;
    q.push(begin);
    mark[begin.x][begin.y] = begin.remtime;
    Node p, tmp;
    while(!q.empty())
    {
        p = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            tmp = p;
            tmp.x += dir[i][0];
            tmp.y += dir[i][1];
            if(tmp.x >= n || tmp.x < 0 || tmp.y >= m || tmp.y < 0 || map[tmp.x][tmp.y] == 0)
				continue;
            tmp.step ++;
            tmp.remtime --;
            if(map[tmp.x][tmp.y] == 3)
            {
                cout << tmp.step << endl;
                return;
            }
            else if(map[tmp.x][tmp.y] == 4)
                tmp.remtime = 6;
            if(tmp.remtime > 1 && tmp.remtime > mark[tmp.x][tmp.y])
            {
                mark[tmp.x][tmp.y] = tmp.remtime;
                q.push(tmp);
            }
        }
    }
    cout << "-1" << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                cin >> map[i][j];
                if(map[i][j] == 2)
                {
                    begin.x = i;
                    begin.y = j;
                    begin.remtime = 6;
                    begin.step = 0;
                }
                mark[i][j] = 0;
            }
        bfs();
    }
    return 0;
}


你可能感兴趣的:(r)