hdu4230

/*
分析:
    bfs水题,配合记忆搜索。
    这两天总是犯白痴错误,想当然的写了个东西,然后就只能de了会儿bug。。警示。。。
    
                                                      2013-07-30
*/







#include"iostream"
#include"cstdio"
#include"queue"
#include"cstring"
using namespace std;
const int N=106;
const int mod=1000000;
const int inf=123456789;

int n,m,s_x,s_y,s_face,e_x,e_y;
int step[N][N][4],cnt[N][N][4],flag[N][N][4];
char map[N][N];
int dir[4][2]={-1,0, 0,-1, 1,0, 0,1};

struct node{
    int x,y,step,face;
};
void bfs()
{
    int i;
    node now,next;
    queue<node>q;

    now.x=s_x;
    now.y=s_y;
    now.step=0;
    now.face=s_face;
    step[now.x][now.y][now.face]=0;
    cnt[now.x][now.y][now.face]=1;
    memset(flag,0,sizeof(flag));
    q.push(now);

    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(flag[now.x][now.y][now.face])    continue;
        flag[now.x][now.y][now.face]=1;
        for(i=0;i<4;i++)    if(step[now.x][now.y][now.step]<=step[e_x][e_y][i])    break;
        if(i>=4)    return ;
        //the same direction
        next.x=now.x+dir[now.face][0];
        next.y=now.y+dir[now.face][1];
        next.step=now.step+1;
        next.face=now.face;
        while(0<=next.x && next.x<n && 0<=next.y && next.y<m && map[next.x][next.y]!='*')
        {
            if(next.step<step[next.x][next.y][next.face])
            {
                step[next.x][next.y][next.face]=next.step;
                cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face]%mod;
                q.push(next);
            }
            else if(next.step==step[next.x][next.y][next.face])
            {
                cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
                q.push(next);
            }
            next.x+=dir[now.face][0];
            next.y+=dir[now.face][1];
        }
        //turn
        next.x=now.x;
        next.y=now.y;
        next.step=now.step+1;
        next.face=(now.face+1)%4;
        if(next.step<step[next.x][next.y][next.face])
        {
            step[next.x][next.y][next.face]=next.step;
            cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face];
            q.push(next);
        }
        else if(next.step==step[next.x][next.y][next.face])
        {
            cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
            q.push(next);
        }

        next.step=now.step+1;
        next.face=(now.face+3)%4;
        if(next.step<step[next.x][next.y][next.face])
        {
            step[next.x][next.y][next.face]=next.step;
            cnt[next.x][next.y][next.face]=cnt[now.x][now.y][now.face];
            q.push(next);
        }
        else if(next.step==step[next.x][next.y][next.face])
        {
            cnt[next.x][next.y][next.face]=(cnt[next.x][next.y][next.face]+cnt[now.x][now.y][now.face])%mod;
            q.push(next);
        }
    }
}
int main()
{
    int i,l,j;
    while(scanf("%d%d",&n,&m),n||m)
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(l=0;l<m;l++)
            {
                if(map[i][l]=='X')    {e_x=i;e_y=l;}
                if(map[i][l]=='N')    {s_x=i;s_y=l;s_face=0;}
                else if(map[i][l]=='W')    {s_x=i;s_y=l;s_face=1;}
                else if(map[i][l]=='S')    {s_x=i;s_y=l;s_face=2;}
                else if(map[i][l]=='E')    {s_x=i;s_y=l;s_face=3;}
                for(j=0;j<4;j++)    step[i][l][j]=inf;
            }
        }
        map[s_x][s_y]='.';
        memset(cnt,0,sizeof(cnt));
        bfs();
        int ans=0,minstep=inf;
        for(i=0;i<4;i++)    if(step[e_x][e_y][i]<minstep)    minstep=step[e_x][e_y][i];
        for(i=0;i<4;i++)    if(step[e_x][e_y][i]==minstep)    ans=(ans+cnt[e_x][e_y][i])%mod;
        if(minstep==inf)    printf("0 0\n");
        else    printf("%d %d\n",minstep,ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值