HDU 1429 bfs+优先队列+状态压缩

7 篇文章 0 订阅

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1429

先用了DFS+BFS上一题的套路。然后把钥匙叠加到门上。
后来发现自己错了- -。这样想有问题。比如。AB门在左边。ab钥匙在右边。
emmmmm错的代码也放上来。毕竟还是有些地方可以参考一下??- -

#include<bits/stdc++.h>
#define INF 1e18
#define inf 1e9
#define min(a,b) a<b?a:b
#define max(a,b) a>b?a:b
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 25;
int n,m,t,cnt,len,mp[300][300];
char smp[_max][_max];
bool vis[_max][_max];
bool mvis[300];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
struct node{
    int x,y,step;
    void init(int x,int y,int step){
        this->x=x;
        this->y=y;
        this->step=step;
    }
};
queue<node> q;
bool outside(node n1){
    int x = n1.x;
    int y = n1.y;
    if(smp[x][y] == '*') return false;
    if(x > n || x < 1) return false;
    if(y > m || y < 1) return false;
    if(!vis[x][y]) return false;
    return true;
}
void bfs(char b,int bx,int by){
    while(!q.empty()) q.pop();
    node now,nxt;
    now.init(bx,by,0);
    q.push(now);
    vis[bx][by] = 0;
    while(!q.empty()){
        now = q.front();
        q.pop();
        for(int i = 0 ; i < 4 ; i++){
            nxt.init(now.x+dx[i],now.y+dy[i],now.step+1);
            if(nxt.step >= t) continue;
            if(!outside(nxt)) continue;
            vis[nxt.x][nxt.y] = 0;
            char c = smp[nxt.x][nxt.y];
            if(c != '.'){
                if( c >= 'A' && c <= 'J')
                    mp[b][c] = nxt.step;    
                else{
                    mp[b][c] = nxt.step;
                    q.push(nxt);
                }
            }

            else q.push(nxt);
        }
    }
    return ;
}
void dfs(char c,int step){
    mvis[c] = false; 
    if(step >= t) return ;
    if(c == '^'){
        cnt = min(cnt,step);
        return ;
    }
    if(mp[c]['^'] != -1){
        dfs('^',step+mp[c]['^']);
        mvis['^'] =1;   
    }
    for(int i = 0 ; i < len ; i++){
        char nc = 'A'+i;
        if(!mvis[nc]) continue;
        if(mp[c][nc] == -1) continue;   
        dfs(nc,step+mp[c][nc]);
        mvis[nc] =true;
    } 
}
int main(){
    while(cin>>n>>m>>t){
        memset(mp,-1,sizeof(mp));
        for(int i = 1 ; i <= n ; i++)
            for(int j = 1 ; j <= m ; j++)
                cin>>smp[i][j];
        len = 0;
        for(int i = 1 ; i <= n ; i++)
            for(int j = 1 ; j <= m ; j++){
                memset(vis,1,sizeof(vis));
                if(smp[i][j] <= 'J' && smp[i][j] >= 'A' || smp[i][j] == '@'){
                    bfs(smp[i][j],i,j);
                    if(smp[i][j] != '@')
                        len++;
                }   
            }
        char s = '@';
        for(int i = 0 ; i < len ; i++){
            char c = 'A'+i;
            char c1 = 'a'+i;
            if(mp[s][c1]!=-1&&mp[c][c1]!=-1&&mp[s][c]!=-1){
                mp[s][c] = mp[s][c1]+mp[c][c1];
            }
            else mp[s][c] = -1;
        }
        for(int i = 0 ; i < len ; i++){
            s = 'A'+i;
            for(int j = 0 ; j < len ; j++){ 
                char c = 'A'+j;
                char c1 = 'a'+j;
                if(c == s) continue;
                if(mp[s][c1]!=-1&&mp[c][c1]!=-1){
                    mp[s][c] = mp[s][c1]+mp[c][c1];
                }
                else mp[s][c] = -1;
            }
        }
        cnt = inf;
        memset(mvis,true,sizeof(mvis));
        dfs('@',0);
        if(cnt == inf) cnt = -1;
        cout<<cnt<<endl;
    }
    return 0;
} 

然后看了discuss大家都用了压状。就想怎么压,因为开门都要钥匙。但是只有A-J所以就很少,可以用01表示当前钥匙的状态。然后用bfs就行了。

#include<bits/stdc++.h>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;

const int _max = 25;
const int _m = ('J'-'A')+1;
struct node{
    int x,y,step,sta;
    node(int x,int y,int step,int sta):x(x),y(y),step(step),sta(sta){};
    bool operator < (const node &n1) const{
        return step > n1.step;
    }
};
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
char mp[_max][_max];
bool vis[_max][_max][1<<_m];
int n,m,t;
priority_queue<node> q;
bool outside(node n1){
    if(mp[n1.x][n1.y] == '*') return false;
    if(n1.x > n || n1.x < 1) return false;
    if(n1.y > m || n1.y < 1) return false;
    if(!vis[n1.x][n1.y][n1.sta]) return false;
    return true;
}
int bfs(int sx,int sy,int ex,int ey){
    memset(vis,1,sizeof(vis));
    while(!q.empty()) q.pop();
    node now(sx,sy,0,0);
    vis[sx][sy][0] = 0;
    q.push(now);
    while(!q.empty()){
        now = q.top();
        q.pop();
        if(now.step >= t)
            return -1;
        if(now.x == ex && now.y == ey)
            return now.step;
        for(int i = 0 ; i < 4 ; i++){
            node next(now.x+dx[i],now.y+dy[i],now.step+1,now.sta);
            if(!outside(next)) continue;
            char c = mp[next.x][next.y];
            if(c >= 'A' && c <= 'J'){
                if(!((next.sta>>(c-'A'))&1)) continue;
            }
            else if(c >= 'a' && c <= 'j')
                next.sta = next.sta|(1<<(c-'a'));
            q.push(next);
            vis[next.x][next.y][next.sta] = 0;
        }   
    }
    return -1;
}
int main(){
    IOS;
    int sx,sy,ex,ey;
    while(cin>>n>>m>>t){
        for(int i = 1 ; i <= n ; i++)
            for(int j = 1 ; j <= m ; j++){
                cin>>mp[i][j];
                if(mp[i][j] == '@'){
                    sx = i;
                    sy = j;
                }
                if(mp[i][j] == '^'){
                    ex = i;
                    ey = j;
                }
            } 
        cout<<bfs(sx,sy,ex,ey)<<endl;;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值