hdu1429

来源:互联网 发布:数据库的基本对象是 编辑:程序博客网 时间:2024/06/05 20:28
很久以前写的。。
#include <queue>#include <iostream>#include <algorithm>#include <string>using namespace std;int dir[4][2]={1,0,-1,0,0,1,0,-1};struct node{    int key,step;    int x,y;};int ans,n,m,t,s_x,s_y;char map[22][22];int hash[22][22][1025];void bfs(){    queue<node>q;    node cur,next;    int key,i;    memset(hash,0,sizeof(hash));    cur.x=s_x;    cur.y=s_y;    cur.step=0;    cur.key=0;    q.push(cur);    while (!q.empty())    {        cur=q.front();        q.pop();        if (cur.step>=t) return ;        if (map[cur.x][cur.y]=='^') {ans=cur.step; return ;}        for (i=0;i<4;i++)        {            next.x=cur.x+dir[i][0];            next.y=cur.y+dir[i][1];            next.step=cur.step+1;            next.key=cur.key;            if (next.x<0 || next.x>=n || next.y<0 || next.y>=m) continue;            if (map[next.x][next.y]=='*') continue;            if (map[next.x][next.y]<='j' && map[next.x][next.y]>='a' )            {                key=1<<(map[next.x][next.y]-'a');                next.key=next.key|key;                if (hash[next.x][next.y][next.key]==0) { q.push(next); hash[next.x][next.y][next.key]=1;}            }            else             if (map[next.x][next.y]<='J' && map[next.x][next.y]>='A' )            {                key=map[next.x][next.y]-'A';                if (( (next.key>>key)&1) && (hash[next.x][next.y][next.key]==0))                {                    hash[next.x][next.y][next.key]=1;                    q.push(next);                }            }            else             {                if (hash[next.x][next.y][next.key]==0)                {                    q.push(next);                    hash[next.x][next.y][next.key]=1;                }            }        }    }}int main(){    int i,j;    while (scanf("%d%d%d",&n,&m,&t)!=EOF)    {        for (i=0;i<n;i++)            scanf("%s",map[i]);        for (i=0;i<n;i++)            for (j=0;j<m;j++)                if (map[i][j]=='@') { s_x=i; s_y=j;}        ans=-1;        bfs();        printf("%d\n",ans);    }    return 0;}

0 0
原创粉丝点击