• 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<stdio.h>
     2 #include<queue>
     3 #include<string.h>
     4 using namespace std;
     5 struct Node{
     6     int nx,ny,str;
     7 }; 
     8 int disx[4]={0,1,0,-1};
     9 int disy[4]={1,0,-1,0};
    10 //int visit[9][9];
    11 int hp[9][9];//定义一个hp数组,主要是为了记录上一次到这个点的能量, 
    12 Node a,b;
    13 int map[9][9],t,w,h,flot,time;
    14 void bfs(int x,int y){
    15     queue<Node>dl;
    16     a.nx=x;a.ny=y;a.str=6;
    17     dl.push(a);
    18     //visit[a.nx][a.ny]=1;
    19     hp[x][y]=6;
    20     while(!dl.empty()){
    21     t=dl.size();
    22     time++;
    23     while(t--){
    24                 a=dl.front();
    25                 dl.pop();
    26                 if(a.str==0)continue;
    27                 for(int i=0;i<4;i++){
    28                     b.nx=a.nx+disx[i];
    29                     b.ny=a.ny+disy[i];
    30                     //b.time=a.time+1;
    31                     if(b.nx<0||b.ny<0||b.nx>=h||b.ny>=w)continue;
    32                     if(map[b.nx][b.ny]==0)continue;
    33                     if(a.str-1<hp[b.nx][b.ny])continue;//这句主要是为了比较上一次到这个位置的血量 ,如果上一次到这个点的能量高,就要这个点了; 
    34                     if(a.str-1==0)continue;
    35                     if(map[b.nx][b.ny]==3){
    36                         flot=1;
    37                         //time=b.time;
    38                         return;
    39                     }
    40                     if(map[b.nx][b.ny]==4){
    41                         //visit[b.nx][b.ny]=1;
    42                         b.str=6;
    43                         hp[b.nx][b.ny]=b.str;
    44                         dl.push(b);
    45                     }
    46                     if(map[b.nx][b.ny]==1){
    47                         b.str=a.str-1;
    48                         hp[b.nx][b.ny]=b.str;dl.push(b);
    49                     }
    50                 }
    51         }
    52     }
    53 }
    54     int main(){int x,y,sx,sy;
    55         while(~scanf("%d%d",&w,&h),w||h){flot=0;time=0;
    56         //memset(visit,0,sizeof(visit));
    57         memset(hp,0,sizeof(hp));
    58             for(x=0;x<h;x++){
    59                 for(y=0;y<w;y++){
    60                     scanf("%d",&map[x][y]);
    61                     if(map[x][y]==2)sx=x,sy=y;
    62                 }
    63             }
    64             bfs(sx,sy);
    65         //    printf("%d
    ",time);
    66             if(flot)printf("%d
    ",time);
    67              else puts("-1");
    68         }
    69         return 0;
    70     }

    代码二:

     1 #include<stdio.h>
     2 #include<queue>
     3 #include<string.h>
     4 using namespace std;
     5 struct Node{
     6     int nx,ny,str,time;
     7 }; 
     8 int disx[4]={0,1,0,-1};
     9 int disy[4]={1,0,-1,0};
    10 //int visit[9][9];
    11 int hp[9][9];
    12 Node a,b;
    13 int map[9][9],t,w,h,flot,time;
    14 void bfs(int x,int y){
    15     queue<Node>dl;
    16     a.nx=x;a.ny=y;a.str=6;a.time=0;
    17     dl.push(a);
    18     //visit[a.nx][a.ny]=1;
    19     hp[x][y]=6;
    20     while(!dl.empty()){
    21                 a=dl.front();
    22                 dl.pop();
    23                 if(a.str==0)continue;
    24                 for(int i=0;i<4;i++){
    25                     b.nx=a.nx+disx[i];
    26                     b.ny=a.ny+disy[i];
    27                     b.time=a.time+1;
    28                     if(b.nx<0||b.ny<0||b.nx>=h||b.ny>=w)continue;
    29                     if(map[b.nx][b.ny]==0)continue;
    30                     if(a.str-1<hp[b.nx][b.ny])continue;
    31                     if(a.str-1==0)continue;
    32                     if(map[b.nx][b.ny]==3){
    33                         flot=1;
    34                         time=b.time;
    35                         return;
    36                     }
    37                     if(map[b.nx][b.ny]==4){
    38                         //visit[b.nx][b.ny]=1;
    39                         b.str=6;
    40                         hp[b.nx][b.ny]=b.str;
    41                         dl.push(b);
    42                     }
    43                     if(map[b.nx][b.ny]==1){
    44                         b.str=a.str-1;
    45                         hp[b.nx][b.ny]=b.str;dl.push(b);
    46                     }
    47                 }
    48         }
    49     }
    50     int main(){int x,y,sx,sy;
    51         while(~scanf("%d%d",&w,&h),w||h){flot=0;time=0;
    52         //memset(visit,0,sizeof(visit));
    53         memset(hp,0,sizeof(hp));
    54             for(x=0;x<h;x++){
    55                 for(y=0;y<w;y++){
    56                     scanf("%d",&map[x][y]);
    57                     if(map[x][y]==2)sx=x,sy=y;
    58                 }
    59             }
    60             bfs(sx,sy);
    61         //    printf("%d
    ",time);
    62             if(flot)printf("%d
    ",time);
    63              else puts("-1");
    64         }
    65         return 0;
    66     }

    感觉第一个比较省内存,好理解;

  • 相关阅读:
    爬虫-基于scrapy-redis两种形式的分布式爬虫
    爬虫-Scrapy框架(CrawlSpider)
    爬虫-User-Agent和代理池
    爬虫-scrapy框架之递归解析和post请求
    爬虫-scrapy数据的持久化存储
    爬虫-scrapy框架简介和基础应用
    爬虫-移动端数据爬取
    爬虫-图片懒加载技术、selenium和PhantomJS
    爬虫-验证码处理
    爬虫-requests模块
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4708359.html
Copyright © 2020-2023  润新知