您的位置:首页 > 其它

bfs+STL cf242c

2015-08-19 10:39 267 查看
#include <iostream>
#include <map>
#include <cstdio>
#include <queue>

using namespace std;

map<int, map<int,int> >mp;
queue<int> q;

int main()
{
int x0,y0,x1,y1,n;
while(scanf("%d%d%d%d%d",&x0,&y0,&x1,&y1,&n)!=EOF)
{
int r,a,b;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&r,&a,&b);
for(int t=a;t<=b;t++)
{
mp[r][t]=-1;
}
}
q.push(x0);
q.push(y0);
q.push(0);
int ans=-1;
while(!q.empty())
{
int tx=q.front();
q.pop();
int ty=q.front();
q.pop();
int cnt=q.front();
q.pop();
if(tx==x1&&ty==y1)
{
ans=cnt;
break;
}
for(int i=-1;i<=1;i++)
{
for(int j=-1;j<=1;j++)
{
if(i==0&&j==0)
continue;
if(mp[tx+i][ty+j]==-1)
{
mp[tx+i][ty+j]=1;
q.push(tx+i);
q.push(ty+j);
q.push(cnt+1);
}
}
}
}
cout<<ans<<endl;
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: