此题坑了我太久,不想解释了,想AC的话看看讨论版里的trick就够了。
附:poj上没有这种数据
4 1 3 2 1 1 1 3 0 2 1 1 0 2 1 1 1 2 2 1 1 1.5 1.5答案应该是0,而不是1.
#include
#include
#include
#include
#include
using namespace std;
#define max(q,w) qa.dis;
}
};
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int tx,ty;
int n,m,ans;
bool isok(node &a)
{
if(a.x<=n&&a.x>=0&&a.y<=m&&a.y>=0) return true;
return false;
}
void bfs(int x,int y)
{
flag=0x3f3f3f3f;
priority_queue q;
node tmp,tt;
tmp.x=0;tmp.y=0;tmp.dis=0;
q.push(tmp);
for(int i=0;i<4;i++) vis[0][0][i]=1;
while(!q.empty())
{
tmp=q.top();q.pop();
if(tmp.dis>flag) break;
for(int d=0;d<4;d++)
{
tt=tmp;
tt.x+=dx[d];tt.y+=dy[d];
if(isok(tt)&&!vis[tt.x][tt.y][d])
{
if(d==1||d==2)
{
if(map[tmp.x][tmp.y][d-1]==1) continue;
if(map[tmp.x][tmp.y][d-1]==2) tt.dis++;
}
else if(d==0)
{
if(map[tt.x][tt.y][0]==1) continue;
if(map[tt.x][tt.y][0]==2) tt.dis++;
}
else if(d==3)
{
if(map[tt.x][tt.y][1]==1) continue;
if(map[tt.x][tt.y][1]==2) tt.dis++;
}
vis[tt.x][tt.y][d]=1;
if(tt.x==tx&&tt.y==ty)
{
flag=tmp.dis;
ans=min(ans,tt.dis);
}
q.push(tt);
}
}
}
if(flag==0x3f3f3f3f) printf("-1\n");
else printf("%d\n",ans);
}
int main()
{
int t1,t2,x,y,d,t;
double sx,sy;
while(scanf("%d%d",&t1,&t2)!=EOF)
{
if(t1+t2==-2) break;
n=m=-1;
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
ans=0x3f3f3f3f;
for(int i=1;i<=t1;i++)
{
scanf("%d%d%d%d",&x,&y,&d,&t);
for(int j=0;j199||sy>199)
printf ("0\n");
else
{
tx=(int)sx;
ty=(int)sy;
if(tx>n||ty>m||ty<0||tx<0) printf("0\n");
else bfs(tx,ty);
}
}
}