HDU 1429 勝利大逃亡(續)

學習到位壓縮,位壓縮還是很重要的,第一次做位壓縮的題目,感覺以前學習C語言位運算的時候,不太紮實啊!

開始的時候,dir數組中少加了一個逗號,調試了十多分鐘才發現,還是不夠仔細……大哭

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char map[21][21];
int flag[21][21][1205];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int sx,sy,n,m,t;
struct node
{
    int x,y,time,key;
};
void bfs()
{
    int i;
    node cur,next;
    queue<node> q;
    cur.x=sx;
    cur.y=sy;
    cur.time=0;
    cur.key=0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(map[cur.x][cur.y]=='^')
        {
            if(cur.time<t) {cout<<cur.time<<endl;return ;}
            break;
        }
        for(i=0;i<4;i++)
        {

            next.time=cur.time;
            next.key=cur.key;
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            if(next.x>=0&&next.x<n&next.y>=0&&next.y<m&&map[next.x][next.y]!='*')
            {
                next.time+=1;
    //           cout<<next.time<<endl;
                if(map[next.x][next.y]>='A'&&map[next.x][next.y]<='J')
                {
                    int key1=map[next.x][next.y]-'A';
                    if(((next.key>>key1)&1)&&!flag[next.x][next.y][next.key])    //手中是否有開此門的鑰匙
                    {
                   //     cout<<"ok1"<<endl;
                        flag[next.x][next.y][next.key]=1;
                        q.push(next);
                    }
                }
                else if(map[next.x][next.y]>='a'&&map[next.x][next.y]<='j')
                {
                    int key1=(1<<(map[next.x][next.y]-'a'));  //因爲0000是0001
                    next.key|=key1;                             //取鑰匙
                    if(!flag[next.x][next.y][next.key])
                    {
                      //   cout<<"ok2"<<endl;
                        flag[next.x][next.y][next.key]=1;
                        q.push(next);
                    }
                }
                else
                {
                    if(!flag[next.x][next.y][next.key])
                    {
                     //    cout<<"ok3"<<endl;
                        flag[next.x][next.y][next.key]=1;
                        q.push(next);
                    }
                }
            }
        }
    }
    cout<<"-1"<<endl;
}
int main()
{
 //   freopen("in.txt","r",stdin);
    int i,j;
    while(cin>>n>>m>>t)
    {
        memset(flag,0,sizeof(flag));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='@')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        bfs();
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章