C - 胜利大逃亡(续) HDU - 1429

C - 胜利大逃亡(续) HDU - 1429

#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;



int n,m,t;
char mp[100][100];
struct data
{
    int x,y;
    int cost;
    int sta;

}st,en;


int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
queue<data>que;
int vis[27][27][1077];


bool ok(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||mp[x][y]=='*')
        return 0;
    return 1;

}
int bfs()
{
    memset(vis,0,sizeof vis);
    while(!que.empty())
        que.pop();

    vis[st.x][st.y][st.sta]=1;
    que.push(st);

    while(!que.empty())
    {
        data now=que.front();
        que.pop();


        if(now.x==en.x&&now.y==en.y)
        {

            return now.cost;
        }
        for(int i=0;i<4;i++)
        {
            data ne=now;
            ne.x+=dx[i];
            ne.y+=dy[i];
            ne.cost++;

            if(ok(ne.x,ne.y))
            {
                char c=mp[ne.x][ne.y];
                if(c>='a'&&c<='z')
                {
                    ne.sta|=(1<<(c-'a'));

                }

                int flag=1;
                char C=mp[ne.x][ne.y];
                if(C>='A'&&C<='Z')
                {
                    if(ne.sta&(1<<(C-'A')))
                    {

                    }
                    else
                        flag=0;

                }
                if(flag&&vis[ne.x][ne.y][ne.sta]==0)
                {
                    vis[ne.x][ne.y][ne.sta]=1;
                    que.push(ne);

                }
            }
        }

    }

    return 0x3f3f3f3f;

}
int main()
{
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {

        for(int i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
            for(int j=0;j<m;j++)
            {

                if(mp[i][j]=='@')
                {
                    st.x=i;
                    st.y=j;
                    st.cost=0;
                    st.sta=0;
                }
                if(mp[i][j]=='^')
                {
                    en.x=i;
                    en.y=j;
                    en.cost=0;
                    st.sta=0;

                }
            }
        }
        int tmp=bfs();
        if(tmp>=t)
            puts("-1");
        else
        printf("%d\n",tmp);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值