poj 2049poj2049 - Finding Nemo

<a target=_blank href="http://blog.csdn.net/wangjian8006/article/details/7997609" style="color: rgb(102, 102, 102); text-decoration: none; font-family: "Microsoft YaHei"; font-size: 20px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 30px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">poj2049 - Finding Nemo</a><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 26px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">题目大意:有一个迷宫,在迷宫中有墙与门
有m道墙,每一道墙表示为(x,y,d,t)
x,y表示墙的起始坐标
d为0即向右t个单位,都是墙
d为1即向上t个单位,都是墙
有n道门,每一道门表示为(x,y,d)
x,y表示门的起始坐标
d为0即向右一个单位表示门
d为1即向上一个单位表示门
再给出你起点的位置(f1,f2),并保证这个点的位置不会再墙或者门中,为起点到(0,0)最少要穿过多少条门</span></p><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 26px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">注意:在迷宫中不只有门和墙,还有空地,因为理解错题目意思WA了一上午。</span></p><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 26px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"> </p><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 26px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"> </p><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 26px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<span style="font-size:18px;">解题思路:调试的我有点头晕.
将坐标系看成网格,在这里我以每个格子的左下点为基点,那么坐标对应网格
坐标(0,0)的网格为(1,1)
坐标(1,1)的网格为(2,2)
坐标(1,2)的网格为(2,3)
...
依次类推</span></p><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 26px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">我再定义xa[i][j]代表网格(i,j)的上边的值
ya[i][j]代表网格(i,j)的右边的值
值分为三种,空地,墙,门
空地为0,墙为INF,门为1
首先全赋值为空地
然后根据输入来赋值
这样就便于搜索了
这里要注意的是要全部搜索完,因为要穿过最少的门,用优先队列搞定
还有当在迷宫外面时,直接输出0,这个不看讨论版真会WA到死
因为迷宫可能在[1,199]但是目标人物所在坐标可能超出这个范围</span></p><p style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 26px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"> //  dis数组储存穿越的门数  表格是从(1,1)开始的千万别离解错了,左下角控制这个表格
</p><pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;

#define INF 1<<29
#define WALL INF
#define DOOR 1
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)

int xa[1000][1000];
int ya[1000][1000];
int dis[1000][1000];//储存穿越的门数

int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int xmax,ymax;

bool pd(int x,int y)//判断是否越界
{
    if(x>0&&x<=xmax&&y<=ymax&&y>0)  return 1;
    return 0;
}
int getvalue(int x,int y,int i)//判断下一步 是门还是墙还是空地 并返回这块地方的价值
{
    if(i == 0) return ya[x-1][y];
    if(i == 1) return ya[x][y];
    if( i ==2 )  return xa[x][y-1];
    return xa[x][y];
}
int bfs(int tx,int ty)
{
    int i,j,vx,vy,dx,dy,tmp;
    queue<int>q;

    for(i=1;i<=ymax;i++)//根据题目建 yamx表示所开数组的行 其实就是题目中的y轴  xmax同理 就是x轴 
        for(j=1;j<=xmax;j++)
        dis[i][j]=INF;

    dis[1][1]=0;
    q.push(1);
    q.push(1);
    while(!q.empty())
    {
        vx=q.front();q.pop();
        vy=q.front();q.pop();
        for(i=0; i < 4; i++)
        {
             dx=vx+dir[i][0];
             dy=vy+dir[i][1];
             tmp = getvalue(vx,vy,i);//判断下一价值 
             //printf("+++++ %d %d %d\n",dx,dy,tmp);
             if(pd(dx,dy) && dis[dx][dy] >  dis[vx][vy] + tmp)//如果没出边界 并且 不断优化更新最小值
             {
                 //printf("------\n");
                 dis[dx][dy] = dis[vx][vy] + tmp;
                  q.push(dx);
                q.push(dy);
             }


        }
    }
    return (dis[tx][ty]==INF?-1:dis[tx][ty]);//当队列为空时表示全部遍历完 这是看看dis【tx】【ty】的值 此时肯定最少




}
int main()
{
    int m,n,i,j;
    int x,y,d,t;
    while(~scanf("%d%d",&m,&n))
    {
        if(m==-1&&n==-1) break;
         xmax=ymax=-1;
         memset(xa,0,sizeof(xa));
         memset(ya,0,sizeof(ya));
        for(i=0;i<m;i++)//建图
        {
            scanf("%d%d%d%d",&x,&y,&d,&t);
            if(d)
            {
                for(j=0;j<t;j++)
                ya[x][y+1+j] = WALL;//自己画图就明白
                xmax=max(x+1,xmax);//找到最大的边界
                ymax=max(y+t+1,ymax);
            }
            else
            {
                for(j=0;j<t;j++)
                xa[x+1+j][y] = WALL;
                xmax=max(xmax,x+1+t);
                ymax=max(ymax,y+1);
            }

        }

        for( i =0; i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&d);

                if(d) ya[x][y+1] = DOOR;
                else  xa[x+1][y] = DOOR;

        }
        double sx,sy;
        scanf("%lf%lf",&sx,&sy);
        if(!(sx>=1 && sx<=199 && sy>=1 && sy<=199)) printf("0\n");
		else printf("%d\n",bfs((int)sx+1,(int)sy+1));//取整加1是将儿子的位置表示在表格里

    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值