hdu1072(bfs)

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int a[10][10];
int state_time[10][10];
int f[4][2]={0,1,0,-1,1,0,-1,0};
struct state
{
    int x,y;
    int time;
    int count;
};
int main()
{
    int t,n,m;
    cin>>t;
    while(t--)
    {
       memset(state_time,-1,sizeof(state_time));
       memset(a,0,sizeof(a));
       cin>>n>>m;
       int sx,sy;
       int min=99909990;
       for(int i=0;i<n;i++)
         for(int j=0;j<m;j++)
              {
                  cin>>a[i][j];
                  if(a[i][j]==2)
                  {sx=i;sy=j;}
                 // if(a[i][j]==3)
                  //{ lx=i;ly=j;}
              }
        state start;
        start.x=sx;start.y=sy;start.count=0;start.time=6;
        queue<state>q;
        q.push(start);
        while(!q.empty())
        {
            state cur=q.front();q.pop();
            if(state_time[cur.x][cur.y]>=cur.time)continue;
            //cout<<cur.x<<" "<<cur.y<<endl;
            if(state_time[cur.x][cur.y]<cur.time)
            {state_time[cur.x][cur.y]=cur.time;}

            if(cur.time==0)continue;
            if(a[cur.x][cur.y]==3)
            {
                if(cur.count<min)min=cur.count;
                continue;
            }
            if(a[cur.x][cur.y]==4)
            {
                cur.time=6;
            }
            for(int i=0;i<4;i++)
            {
                state zijiedian(cur);
                zijiedian.count++;
                zijiedian.time--;
                zijiedian.x+=f[i][0];
                zijiedian.y+=f[i][1];
                if(a[zijiedian.x][zijiedian.y]==0)continue;
                q.push(zijiedian);
            }
        }
        if(min==99909990)cout<<-1<<endl;
        else cout<<min<<endl;

    }
    return 0;
}

posted @ 2013-11-28 19:11  天羽屠龙舞  阅读(152)  评论(0编辑  收藏  举报