• ZOJ 1671 Walking ant(bfs)


    Walking Ant

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Ants are quite diligent. They sometimes build their nests beneath flagstones.

    Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to her nest.

    The ant takes exactly one second to move from one flagstone to another. That is, if the ant is on the flagstone with coordinates (x,y) at time t, she will be on one of the five flagstones with the following coordinates at time t+1:

    (x, y), (x+1, y), (x-1, y), (x, y+1), (x, y-1).

    The ant cannot go out of the rectangular area. The ant can visit the same flagstone more than once.

    Insects are easy to starve. The ant has to go back to her nest without starving. Physical strength of the ant is expressed by the unit "HP". Initially, the ant has the strength of 6 HP. Every second, she loses 1 HP. When the ant arrives at a flagstone with some food on it, she eats a small piece of the food there, and recovers her strength to the maximum value, i.e., 6 HP, without taking any time. The food is plenty enough, and she can eat it as many times as she wants.

    When the ant's strength gets down to 0 HP, she dies and will not move anymore. If the ant's strength gets down to 0 HP at the moment she moves to a flagstone, she does not effectively reach the flagstone: even if some food is on it, she cannot eat it; even if the hole is on that stone, she has to die at the entrance of her home.

    If there is a puddle on a flagstone, the ant cannot move there.

    Your job is to write a program which computes the minimum possible time for the ant to reach the hole with positive strength from her start position, if ever possible.


    Input

    The input consists of multiple maps, each representing the size and the arrangement of the rectangular area. A map is given in the following format.

    w h
    d11 d12 d13 ... d1w
    d21 d22 d23 ... d2w
    ...
    dh1 dh2 dh3 ... dhw

    The integers w and h are the numbers of flagstones in the x- and y-directions, respectively. w and h are less than or equal to 8. The integer dyx represents the state of the flagstone with coordinates (x, y) as follows.

    0: There is a puddle on the flagstone, and the ant cannot move there.
    1, 2: Nothing exists on the flagstone, and the ant can move there. `2' indicates where the ant initially stands.
    3: The hole to the nest is on the flagstone.
    4: Some food is on the flagstone.

    There is one and only one flagstone with a hole. Not more than five flagstones have food on them.

    The end of the input is indicated by a line with two zeros.

    Integer numbers in an input line are separated by at least one space character.


    Output

    For each map in the input, your program should output one line containing one integer representing the minimum time. If the ant cannot return to her nest, your program should output -1 instead of the minimum time.


    Sample Input

    3 3
    2 1 1
    1 1 0
    1 1 3
    8 4
    2 1 1 0 1 1 1 0
    1 0 4 1 1 0 4 1
    1 0 0 0 0 0 0 1
    1 1 1 4 1 1 1 3
    8 5
    1 2 1 1 1 1 1 4
    1 0 0 0 1 0 0 1
    1 4 1 0 1 1 0 1
    1 0 0 0 0 3 0 1
    1 1 4 1 1 1 1 1
    8 7
    1 2 1 1 1 1 1 1
    1 1 1 1 1 1 1 4
    1 1 1 1 1 1 1 1
    1 1 1 1 4 1 1 1
    4 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 3
    8 8
    1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1
    1 4 4 1 1 1 1 1
    1 4 4 2 1 1 0 0
    1 1 0 0 0 0 0 3
    1 1 0 4 1 1 1 1
    1 1 1 1 1 1 1 1
    8 8
    1 1 1 1 1 1 1 1
    1 1 2 1 1 1 1 1
    1 1 4 4 4 1 1 1
    1 1 1 4 4 1 0 1
    1 1 1 1 1 1 0 1
    1 1 1 1 1 1 0 3
    1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1
    0 0


    Sample Output

    4
    -1
    13
    20
    -1
    -1

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 
     6 struct point
     7 {
     8     int x,y;
     9     int step;
    10     int hp;
    11 };
    12 
    13 int sx,sy,ex,ey,m,n;
    14 int map[10][10];
    15 int d[4][2]={1,0,0,1,-1,0,0,-1};
    16 int vis[10][10][7];
    17 
    18 int bfs(int x,int y)
    19 {
    20     queue<point>q;
    21     point init;
    22     init.x=x,init.y=y;
    23     init.step=0;
    24     init.hp=6;
    25     q.push(init);
    26     vis[x][y][6]=1;
    27     while(!q.empty())
    28     {
    29         point t1,t2;
    30         t1=q.front();
    31         q.pop();
    32         if(t1.x==ex&&t1.y==ey)
    33         return t1.step;
    34         for(int i=0;i<4;i++)
    35         {
    36             t2.x=t1.x+d[i][0];
    37             t2.y=t1.y+d[i][1];
    38             t2.step=t1.step+1;
    39             if(t2.x>=0&&t2.x<n&&t2.y>=0&&t2.y<m&&map[t2.x][t2.y]!=0)
    40             {
    41                 if(map[t2.x][t2.y]==1||map[t2.x][t2.y]==4||map[t2.x][t2.y]==3)
    42                 {
    43                     int temp=t1.hp-1;
    44                     if(temp>0)
    45                     {
    46                         if(map[t2.x][t2.y]==1||map[t2.x][t2.y]==3)
    47                         t2.hp=temp;
    48                         else
    49                         t2.hp=6;
    50                         if(!vis[t2.x][t2.y][t2.hp])
    51                         q.push(t2);
    52                         vis[t2.x][t2.y][t2.hp]=1;
    53                     }
    54                 }
    55             }
    56         }
    57     }
    58     return -1;
    59 }
    60 
    61 int main()
    62 {
    63     while(scanf("%d%d",&m,&n),m|n)
    64     {
    65         memset(vis,0,sizeof(vis));
    66         for(int i=0;i<n;i++)
    67         for(int j=0;j<m;j++)
    68         {
    69             scanf("%d",&map[i][j]);
    70             if(map[i][j]==2)
    71             sx=i,sy=j;
    72             else if(map[i][j]==3)
    73             ex=i,ey=j;
    74             else;
    75         }
    76         int ans=bfs(sx,sy);
    77         printf("%d
    ",ans);
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    转: java语法与ide级入门介绍 from: IBM dev
    Java Servlet 技术简介 from:IBM Dev
    腾讯开源组件-毫秒服务引擎
    JS中的slice和splice
    validform校验框架不显示错误提示
    jQuery获取不到隐藏DIV的高度和宽度
    前端页面下载
    Java传统下载和SpringMVC下载
    先尽人事,再听天命
    Java生成PDF之iTextPDF的使用
  • 原文地址:https://www.cnblogs.com/homura/p/4708268.html
Copyright © 2020-2023  润新知