码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1429 胜利大逃亡(续)(BFS+位压缩)

时间:2015-06-05 12:18:09      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int n,m,t,vis[25][25][1024];
char mp[25][25];
int dx[]= {0,0,1,-1};
int dy[]= {1,-1,0,0};

struct node
{
    int x,y,t,k;
} p1,p2;

int bfs(int sx,int sy,int ex,int ey)
{
    int i,j,flag=0;
    queue<node>q;
    p1.x=sx;
    p1.y=sy;
    p1.t=0;
    p1.k=0;
    q.push(p1);
    while(!q.empty())
    {

        p1=q.front();
        q.pop();
        //printf("%d %d\n",p1.x,p1.y);
        if(p1.x==ex&&p1.y==ey)
        {
            flag=p1.t;
            break;
        }

        for(i=0; i<4; i++)
        {

            p2.x=p1.x+dx[i];
            p2.y=p1.y+dy[i];
            p2.t=p1.t+1;
            p2.k=p1.k;

            if(p2.x<0||p2.x>=n||p2.y<0||p2.y>=m) continue;
            if(mp[p2.x][p2.y]==‘*‘) continue;
            if(mp[p2.x][p2.y]<=‘J‘&&mp[p2.x][p2.y]>=‘A‘) 
            {
                int k=p2.k&(1<<mp[p2.x][p2.y]-‘A‘);
                if(!k) continue;
            }

            if(mp[p2.x][p2.y]<=‘j‘&&mp[p2.x][p2.y]>=‘a‘) 
            {
                p2.k=p2.k|(1<<(mp[p2.x][p2.y]-‘a‘));
            }

            while(vis[p2.x][p2.y][p2.k]==0)
            {
                vis[p2.x][p2.y][p2.k]=1;
                q.push(p2);
            }
        }
    }

    if(flag>=t||flag==0) flag=-1;
    return flag;
}

int main()
{
    int i,j,sx,sy,ex,ey,flag;
    while(~scanf("%d%d%d",&n,&m,&t))
    {
        memset(vis,0,sizeof(vis));
        for(i=0; i<n; i++)
            scanf("%s",mp[i]);

        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                if(mp[i][j]==‘@‘)
                {
                    sx=i;
                    sy=j;
                    mp[i][j]=‘.‘;
                }
                if(mp[i][j]==‘^‘)
                {
                    ex=i;
                    ey=j;
                }
            }

        printf("%d\n",bfs(sx,sy,ex,ey));
    }
    return 0;
}

hdu 1429 胜利大逃亡(续)(BFS+位压缩)

标签:

原文地址:http://blog.csdn.net/xinag578/article/details/46374785

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!