题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=1611
题解:
直接bfs。。。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define m (300)
using namespace std;
struct node{
int x;int y;
};
queue<node>q;
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]+1<vis[nex][ney]&&dis[nex][ney]==-1)
{
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");
}
Usaco2008 Meteor Shower: BFS Solution & Code
This blog post provides a detailed explanation and code for solving the Usaco2008 February contest problem, Meteor Shower (bzoj1611), using Breadth First Search (BFS) algorithm. The author shares insights into their approach and presents the implementation to help readers understand the problem-solving process."
135353090,7337247,从知识获取到创造:深度学习时代的人工智能探索,"['深度学习', '机器学习', '神经网络', '自然语言处理', '知识图谱']
1877

被折叠的 条评论
为什么被折叠?



