HDU 3533 Escape BFS搜索

题意:懒得说了

分析:开个no[100][100][1000]的bool类型的数组就行了,没啥可说的

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
const int N=101;
int n,m,k,d,l;
struct node
{
    char s[2];
    int x,y,v,t;
} o[N];
bool mp[N][N];
bool no[N][N][1001];
void No(int pos)
{
    l=0;
    for(int i=o[pos].x-1; i>=0; --i)
        if(mp[i][o[pos].y])
        {
            l=i+1;
            break;
        }
    for(int i=0; i<=d; i+=o[pos].t)
    {
        int now=o[pos].x-o[pos].v;
        for(int j=i+1; j<=d&&now>=l; ++j,now-=o[pos].v)
            no[now][o[pos].y][j]=1;
    }
}
void S(int pos)
{
    l=n;
    for(int i=o[pos].x+1; i<=n; ++i)
        if(mp[i][o[pos].y])
        {
            l=i-1;
            break;
        }
    for(int i=0; i<=d; i+=o[pos].t)
    {
        int now=o[pos].x+o[pos].v;
        for(int j=i+1; j<=d&&now<=l; ++j,now+=o[pos].v)
            no[now][o[pos].y][j]=1;
    }
}
void W(int pos)
{
    l=0;
    for(int i=o[pos].y-1; i>=0; --i)
        if(mp[o[pos].x][i])
        {
            l=i+1;
            break;
        }
    for(int i=0; i<=d; i+=o[pos].t)
    {
        int now=o[pos].y-o[pos].v;
        for(int j=i+1; j<=d&&now>=l; ++j,now-=o[pos].v)
            no[o[pos].x][now][j]=1;
    }
}
void E(int pos)
{
    l=m;
    for(int i=o[pos].y+1; i<=m; ++i)
        if(mp[o[pos].x][i])
        {
            l=i-1;
            break;
        }
    for(int i=0; i<=d; i+=o[pos].t)
    {
        int now=o[pos].y+o[pos].v;
        for(int j=i+1; j<=d&&now<=l; ++j,now+=o[pos].v)
            no[o[pos].x][now][j]=1;
    }
}
struct asd
{
    int x,y,w;
} a,e;
int dx[5]= {0,0,0,1,-1};
int dy[5]= {0,-1,1,0,0};
queue<asd>q;
int bfs()
{
    while(!q.empty())q.pop();
    q.push(asd {0,0,0});
    no[0][0][0]=1;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=0; i<5; ++i)
        {
            e.x=a.x+dx[i];
            e.y=a.y+dy[i];
            e.w=a.w+1;
            if(e.x<0||e.x>n||e.y<0||e.y>m)continue;
            if(e.w>d)continue;
            if(mp[e.x][e.y])continue;
            if(no[e.x][e.y][e.w])continue;
            if(e.x==n&&e.y==m)return e.w;
            no[e.x][e.y][e.w]=1;
            q.push(e);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&k,&d))
    {
        memset(mp,0,sizeof(mp));
        memset(no,0,sizeof(no));
        for(int i=1; i<=k; ++i)
        {
            scanf("%s%d%d%d%d",o[i].s,&o[i].t,&o[i].v,&o[i].x,&o[i].y);
            mp[o[i].x][o[i].y]=1;
        }
        int ans=0;
        if(mp[0][0]||mp[n][m])
            ans=-1;
        if(ans!=-1)
            for(int i=1; i<=k; ++i)
            {
                if(o[i].s[0]=='N')No(i);
                else if(o[i].s[0]=='S')S(i);
                else if(o[i].s[0]=='W')W(i);
                else E(i);
            }
        if(ans!=-1)ans=bfs();
        if(ans==-1)printf("Bad luck!\n");
        else printf("%d\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2016-03-08 21:10  shuguangzw  阅读(135)  评论(0编辑  收藏  举报