zoukankan      html  css  js  c++  java
  • 地图上的点到出口的最大距离——USACO2.4.2

    bfs搜索,两次分别从两个出口搜,开数组ste[i][j]时刻记录(更新)点i,j到出口的最短距离
    最后在ste[i][j]查找最大值……
    View Code
    #include<stdio.h>
    #include
    <iostream>
    #include
    <queue>
    using namespace std;

    int map[209][80];
    int hash[209][80];
    int ste[209][80];
    int Max;
    int p[4][2]={0,-1,-1,0,0,1,1,0};
    int w,h;

    struct data
    {
    int tei;
    int tej;
    int step;
    };

    void bfs(int ti,int tj)
    {
    queue
    <data>q;

    int i;
    data f,s,t;
    f.tei
    =ti;
    f.tej
    =tj;
    f.step
    =0;
    while(!q.empty())
    q.pop();
    q.push(f);
    while(!q.empty())
    {
    s
    =q.front();
    q.pop();
    for(i=0;i<4;i++)
    {
    t.tei
    =s.tei+p[i][0];
    t.tej
    =s.tej+p[i][1];

    if(t.tei>=1&&t.tei<=2*h+1&&t.tej>=1&&t.tej<=2*w+1)
    if(hash[t.tei][t.tej]!=1)
    {
    if(hash[t.tei][t.tej]==0)
    {
    t.step
    =s.step+1;
    if(ste[t.tei][t.tej]!=0)
    {
    if(ste[t.tei][t.tej]>t.step)
    ste[t.tei][t.tej]
    =t.step;
    }
    else
    {
    ste[t.tei][t.tej]
    =t.step;
    }
    }

    hash[t.tei][t.tej]
    =1;
    q.push(t);
    }
    }
    }
    }

    int main()
    {

    while(scanf("%d%d",&w,&h)!=EOF)
    {
    int i,j;
    char t;

    for(i=1;i<=h*2+1;i++)
    {
    getchar();
    for(j=1;j<=w*2+1;j++)
    {
    scanf(
    "%c",&t);
    if(t==' ')
    map[i][j]
    =0;
    else
    map[i][j]
    =1;
    }
    }

    for(i=1;i<=h*2+1;i++)
    {
    for(j=1;j<=w*2+1;j++)
    {
    ste[i][j]
    =0;
    if(i%2==1||j%2==1)
    {
    if(map[i][j]==0)
    map[i][j]
    =2;//2表示不占地的通路
    }
    }
    }

    int ti[2],tj[2];
    bool one=0,two=0;
    for(j=1;j<=w*2+1;j++)
    {
    if(map[1][j]==2)
    {
    if(one==0)
    {
    ti[
    0]=1;
    tj[
    0]=j;
    one
    =1;
    }
    else
    {
    ti[
    1]=1;
    tj[
    1]=j;
    two
    =1;
    break;
    }
    }
    }

    if(two==0)
    for(j=1;j<=w*2+1;j++)
    {
    if(map[2*h+1][j]==2)
    {
    if(one==0)
    {
    ti[
    0]=2*h+1;
    tj[
    0]=j;
    one
    =1;
    }
    else
    {
    ti[
    1]=2*h+1;
    tj[
    1]=j;
    two
    =1;
    break;
    }
    }
    }

    if(two==0)
    for(i=2;i<=2*h;i++)
    {
    if(map[i][1]==2)
    {
    if(one==0)
    {
    ti[
    0]=i;
    tj[
    0]=1;
    one
    =1;
    }
    else
    {
    ti[
    1]=i;
    tj[
    1]=1;
    two
    =1;
    break;
    }
    }
    }

    if(two==0)
    for(i=2;i<=2*h;i++)
    {
    if(map[i][2*w+1]==2)
    {
    if(one==0)
    {
    ti[
    0]=i;
    tj[
    0]=2*w+1;
    one
    =1;
    }
    else
    {
    ti[
    1]=i;
    tj[
    1]=2*w+1;
    two
    =1;
    break;
    }
    }
    }

    for(i=1;i<=2*h+1;i++)
    {
    for(j=1;j<=w*2+1;j++)
    {
    hash[i][j]
    =map[i][j];
    }
    }


    bfs(ti[
    0],tj[0]);

    for(i=1;i<=2*h+1;i++)
    {
    for(j=1;j<=w*2+1;j++)
    {
    hash[i][j]
    =map[i][j];
    }
    }
    bfs(ti[
    1],tj[1]);

    Max
    =0;
    for(i=1;i<=2*h+1;i++)
    {
    for(j=1;j<=w*2+1;j++)
    {
    if(ste[i][j]>Max)
    Max
    =ste[i][j];
    }
    }

    printf(
    "%d\n",Max);
    }
    return 0;
    }
  • 相关阅读:
    IfcLightSourceSpot
    IfcLightSourcePositional
    IfcLightSourceGoniometric
    IfcLightSourceDirectional
    IfcLightSourceAmbient
    IfcLightSource
    从一系列的图片文件夹中随机抽取图片
    IfcPolygonalBoundedHalfSpace
    IfcBoxedHalfSpace
    IfcHalfSpaceSolid
  • 原文地址:https://www.cnblogs.com/huhuuu/p/1986138.html
Copyright © 2011-2022 走看看