HDU nightmare
#include<iostream>
#include<queue>
int n, m, a[10][10], d[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
bool b;
using namespace std;
struct node
{
int x, y, t, T;
};
int main()
{
void bfs(int, int);
int T;
cin >> T;
while (T--)
{
cin >> n >> m; int xx, yy;
for(int i=0;i<n;i++)
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
if (a[i][j] == 2) xx = i, yy = j;
}
b = 0;
bfs(xx, yy);
if (b == 0) cout << "-1" << endl;
}
return 0;
}
void bfs(int k, int l)
{
queue<node> P;
node start, next;
start.x = k; start.y = l; start.t = 6; start.T = 0;
P.push(start);
while (!P.empty())
{
start = P.front(); P.pop();
if (a[start.x][start.y] == 3)
{
b = 1; cout << start.T << endl; break;
}
if (start.t <= 1) continue;
for (int i = 0; i < 4; i++)
{
int xx, yy;
xx = start.x + d[i][0]; yy = start.y + d[i][1];
if (xx >= 0 && xx < n&&yy >= 0 && yy < m&&a[xx][yy] != 0)
{
next.x = xx; next.y = yy;
next.t = start.t - 1;
if (a[xx][yy] == 4)
{
next.t = 6;
a[xx][yy] = 0;
}
next.T = start.T + 1;
P.push(next);
}
}
}
}