HDU1429胜利大逃亡(续)&&HDU 1885 Key Task BFS+状态压缩+水

HDU1429

只有10把钥匙 1<<10足够了

标记用三维数组

用Linux好不习惯 继续克服~

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include<queue>
const int INF = 999999;
char mp[33][33];
bool vis[33][33][1026];
int n,m;
struct node
{
    int x,y,step;
    int key;
};
queue<node>q;
int xx[4]= {1,0,-1,0};
int yy[4]= {0,1,0,-1};
int bfs(int x,int y)
{
    while(!q.empty())
        q.pop();
    memset(vis,false,sizeof(vis));
    node front,rear;
    front.x=x,front.y=y;
    front.key=0;
    front.step=0;
    vis[x][y][0]=true;
    q.push(front);
    while(!q.empty())
    {
        front=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int dx=front.x+xx[i],dy=front.y+yy[i];
            if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='*'&&!vis[dx][dy][front.key])
            {
                if(mp[dx][dy]=='^')//到终点
                {
                    return front.step+1;
                }
                else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿到钥匙
                {
                    vis[dx][dy][front.key]=true;//标记
                    rear.key =(front.key )| ( 1<<(mp[dx][dy]-'a'));//添加钥匙
                    rear.x=dx,rear.y=dy,rear.step=front.step+1;
                    q.push(rear);
                }
                else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//遇到门
                {
                    if(front.key&(1<<(mp[dx][dy]-'A')))//如果有对应的钥匙
                    {
                        rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;
                        vis[dx][dy][rear.key]=true;
                        q.push(rear);
                    }
                }
                else
                {
                        rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;
                        vis[dx][dy][rear.key]=true;
                        q.push(rear);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int t;
 //  freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        node s;
        for(int i=0; i<n; i++)
            scanf("%s",mp[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='@')
                    s.x=i,s.y=j,s.step=0;
            }
        }
        int ans=bfs(s.x,s.y);
        if(ans==0||ans>=t)
        printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}

HDU1885

这个就4把钥匙

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include<queue>
const int INF = 999999;
char mp[133][133];
bool vis[133][133][33];
int n,m;
int ke[28];
struct node
{
    int x,y,step;
    int key;
};
queue<node>q;
int xx[4]= {1,0,-1,0};
int yy[4]= {0,1,0,-1};
int bfs(int x,int y)
{
    while(!q.empty())
        q.pop();
    memset(vis,false,sizeof(vis));
    node front,rear;
    front.x=x,front.y=y;
    front.key=0;
    front.step=0;
    vis[x][y][0]=true;
    q.push(front);
    while(!q.empty())
    {
        front=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int dx=front.x+xx[i],dy=front.y+yy[i];
            if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='#'&&!vis[dx][dy][front.key])
            {
                if(mp[dx][dy]=='X')//到终点
                {
                    return front.step+1;
                }
                else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿钥匙
                {
                    vis[dx][dy][front.key]=true;
                    rear.key =(front.key )| ( 1<<(ke[mp[dx][dy]-'a']));
                    rear.x=dx,rear.y=dy,rear.step=front.step+1;
                    q.push(rear);
                }
                else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//开门
                {
                    if(front.key&(1<<(ke[mp[dx][dy]-'A'])))
                    {
                        rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;
                        vis[dx][dy][rear.key]=true;
                        q.push(rear);
                    }
                }
                else
                {
                    rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;
                    vis[dx][dy][rear.key]=true;
                    q.push(rear);
                }
            }
        }
    }
    return 0;
}
int main()
{
  //freopen("in.txt","r",stdin);

    ke['a'-'a']=0,ke['b'-'a']=1,ke['r'-'a']=2,ke['g'-'a']=3;
    while(scanf("%d%d",&n,&m),n+m)
    {
        node s;
        for(int i=0; i<n; i++)
            scanf("%s",mp[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='*')
                    s.x=i,s.y=j,s.step=0;
            }
        }
        int ans=bfs(s.x,s.y);
        if(ans==0)
            printf("The poor student is trapped!\n");
        else printf("Escape possible in %d steps.\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值