【bzoj1611】【Usaco2008 Feb】Meteor Shower流星雨 (bfs)题解&代码

题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=1611
题解:
直接bfs。。。
代码:

#include
#include
#include
#include
#define m (300)
using namespace std;
struct node{
    int x;int y;
};
queueq;
node jia(int xx,int yy)
{
    node tmp;
    tmp.x=xx;
    tmp.y=yy;
    return tmp;
}
int n,vis[305][305],dis[305][305];
int d[2][4]={{0,0,-1,1},{1,-1,0,0}};
int main()
{
    for (int i=0;i<=301;i++)
    for (int j=0;j<=301;j++)
    {
        vis[i][j]=999999;
        dis[i][j]=-1;
    }
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int x,y,t;
        scanf("%d%d%d",&x,&y,&t);
        vis[x][y]=min(vis[x][y],t);
        for (int j=0;j<4;j++)
        {
            int nex=x+d[0][j];
            int ney=y+d[1][j];
            if (nex>=0&&ney>=0)
            vis[nex][ney]=min(vis[nex][ney],t);
        }
    }
    q.push(jia(0,0));
    dis[0][0]=0;
    if (vis[0][0]==999999)
    {
        printf("0\n");
        return 0;
    }
    if (vis[0][0]<=0)
    {
        printf("-1\n");
        return 0;
    }
    while(!q.empty())
    {
        node now=q.front();q.pop();
        for (int i=0;i<4;i++)
        {
            int nex=now.x+d[0][i];
            int ney=now.y+d[1][i];
            if (nex>=0&&nex<=301&&ney>=0&&ney<=301)
            if (dis[now.x][now.y]+11)
            {
                dis[nex][ney]=dis[now.x][now.y]+1;
                if (vis[nex][ney]==999999)
                {
                    printf("%d\n",dis[nex][ney]);
                    return 0;
                }
                q.push(jia(nex,ney));
            }
        }
    }
    printf("-1\n");
}

你可能感兴趣的:(bzoj)