poj 2049 Finding Nemo (坑爹的搜索题)

此题坑了我太久,不想解释了,想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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define max(q,w) q<w?w:q
int map[205][205][2];
bool vis[205][205][4];
int flag;
struct node
{
  int x,y;
  int dis;
  bool operator <(const node &a) const
  {
      return dis>a.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<node> 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;j<t;j++)
            {
                if(d) {map[x][y+j][d]=1;m=max(m,y+t);}
                else {map[x+j][y][d]=1;n=max(n,x+t);}
            }
        }
        t=1;
        for(int i=1;i<=t2;i++)
        {
            scanf("%d%d%d",&x,&y,&d);
            for(int j=0;j<t;j++)
            {
                if(d) {map[x][y+j][d]=2;m=max(m,y+t);}
                else {map[x+j][y][d]=2;n=max(n,x+t);}
            }
        }
        scanf("%lf%lf",&sx,&sy);
        if (n==0 && m==0)
			printf ("0\n");
		else if (sx<0||sy<0||sx>199||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);
         }
    }
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TommyTT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值