描述
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.

Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
<dl class="others"> <dt> 输入 </dt> <dd> The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.

</dd> <dt> 输出 </dt> <dd> For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.

</dd> <dt> 样例输入 </dt> <dd>
2
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
</dd> <dt> 样例输出 </dt> <dd>
4
-1
</dd> </dl>

题目大意:

输入一个数a,表示接下来有a组测试数据。输入x,y表示接下来有一个x*y的矩阵。其中2表示起点;3表示终点;1表示道路;0表示墙,不可通过;4表示炸弹重置时间的点。Ignatius从2出发,身上有一个定时炸弹,时间为6。到4时,只要炸弹时间不为1,即可重置为6。到终点3时,时间也不可为1。问Ignatius走出迷宫的最少步数,如果走不出,输出-1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m[10][10];
int s[4][2]={1, 0, -1, 0, 0, 1, 0, -1};
int x,y,x1;
struct zd
{
    int a,b,c,d;
};
int bfs(int a,int b)
{
    int c,d,e,f,g;
    zd i,j,w;
    i.a=a;i.b=b;i.c=0;i.d=6;
    queue<zd>t;
    t.push(i);
    while(!t.empty())
    {
        j=t.front();
        t.pop();
        if(j.d==1)
            continue;
        for(c=0;c<4;c++)
        {
            d=j.a+s[c][0];
            f=j.b+s[c][1];
            if(m[d][f]==3&&d>=0&&d<x&&f>=0&&f<y)
                return j.c+1;
            x1++;
            if(m[d][f]==4&&d>=0&&d<x&&f>=0&&f<y)
            {
                m[d][f]=1;
                i.a=d;i.b=f;i.c=j.c+1;i.d=6;
                t.push(i);
            }
            else if(m[d][f]==1&&d>=0&&d<x&&f>=0&&f<y)
            {
                i.a=d;i.b=f;i.c=j.c+1;i.d=j.d-1;
                t.push(i);
            }
        }
    }
    return -1;
}
int main()
{
    int a,b,c,d,e,f,i,j;
    cin>>a;
    while(a--)
    {
        cin>>x>>y;
        for(d=0;d<x;d++)
        {
            for(e=0;e<y;e++)
            {
                cin>>m[d][e];
                if(m[d][e]==2)
                {
                    i=d;j=e;
                }
            }
        }
        x1=0;
        f=bfs(i,j);
        cout<<f<<endl;
    }
}