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
题解:醉了,写了一下午,各种问题。。。。
代码一:
- #include<stdio.h>
- #include<queue>
- #include<string.h>
- using namespace std;
- struct Node{
- int nx,ny,str;
- };
- int disx[]={,,,-};
- int disy[]={,,-,};
- //int visit[9][9];
- int hp[][];//定义一个hp数组,主要是为了记录上一次到这个点的能量,
- Node a,b;
- int map[][],t,w,h,flot,time;
- void bfs(int x,int y){
- queue<Node>dl;
- a.nx=x;a.ny=y;a.str=;
- dl.push(a);
- //visit[a.nx][a.ny]=1;
- hp[x][y]=;
- while(!dl.empty()){
- t=dl.size();
- time++;
- while(t--){
- a=dl.front();
- dl.pop();
- if(a.str==)continue;
- for(int i=;i<;i++){
- b.nx=a.nx+disx[i];
- b.ny=a.ny+disy[i];
- //b.time=a.time+1;
- if(b.nx<||b.ny<||b.nx>=h||b.ny>=w)continue;
- if(map[b.nx][b.ny]==)continue;
- if(a.str-<hp[b.nx][b.ny])continue;//这句主要是为了比较上一次到这个位置的血量 ,如果上一次到这个点的能量高,就要这个点了;
- if(a.str-==)continue;
- if(map[b.nx][b.ny]==){
- flot=;
- //time=b.time;
- return;
- }
- if(map[b.nx][b.ny]==){
- //visit[b.nx][b.ny]=1;
- b.str=;
- hp[b.nx][b.ny]=b.str;
- dl.push(b);
- }
- if(map[b.nx][b.ny]==){
- b.str=a.str-;
- hp[b.nx][b.ny]=b.str;dl.push(b);
- }
- }
- }
- }
- }
- int main(){int x,y,sx,sy;
- while(~scanf("%d%d",&w,&h),w||h){flot=;time=;
- //memset(visit,0,sizeof(visit));
- memset(hp,,sizeof(hp));
- for(x=;x<h;x++){
- for(y=;y<w;y++){
- scanf("%d",&map[x][y]);
- if(map[x][y]==)sx=x,sy=y;
- }
- }
- bfs(sx,sy);
- // printf("%d\n",time);
- if(flot)printf("%d\n",time);
- else puts("-1");
- }
- return ;
- }
代码二:
- #include<stdio.h>
- #include<queue>
- #include<string.h>
- using namespace std;
- struct Node{
- int nx,ny,str,time;
- };
- int disx[]={,,,-};
- int disy[]={,,-,};
- //int visit[9][9];
- int hp[][];
- Node a,b;
- int map[][],t,w,h,flot,time;
- void bfs(int x,int y){
- queue<Node>dl;
- a.nx=x;a.ny=y;a.str=;a.time=;
- dl.push(a);
- //visit[a.nx][a.ny]=1;
- hp[x][y]=;
- while(!dl.empty()){
- a=dl.front();
- dl.pop();
- if(a.str==)continue;
- for(int i=;i<;i++){
- b.nx=a.nx+disx[i];
- b.ny=a.ny+disy[i];
- b.time=a.time+;
- if(b.nx<||b.ny<||b.nx>=h||b.ny>=w)continue;
- if(map[b.nx][b.ny]==)continue;
- if(a.str-<hp[b.nx][b.ny])continue;
- if(a.str-==)continue;
- if(map[b.nx][b.ny]==){
- flot=;
- time=b.time;
- return;
- }
- if(map[b.nx][b.ny]==){
- //visit[b.nx][b.ny]=1;
- b.str=;
- hp[b.nx][b.ny]=b.str;
- dl.push(b);
- }
- if(map[b.nx][b.ny]==){
- b.str=a.str-;
- hp[b.nx][b.ny]=b.str;dl.push(b);
- }
- }
- }
- }
- int main(){int x,y,sx,sy;
- while(~scanf("%d%d",&w,&h),w||h){flot=;time=;
- //memset(visit,0,sizeof(visit));
- memset(hp,,sizeof(hp));
- for(x=;x<h;x++){
- for(y=;y<w;y++){
- scanf("%d",&map[x][y]);
- if(map[x][y]==)sx=x,sy=y;
- }
- }
- bfs(sx,sy);
- // printf("%d\n",time);
- if(flot)printf("%d\n",time);
- else puts("-1");
- }
- return ;
- }
感觉第一个比较省内存,好理解;
Walking Ant(一道有意思的蚂蚁游戏,bfs)的更多相关文章
- zoj 1671 Walking Ant【简单bfs】
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- Walking Ant(bfs)
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- zoj 1671 Walking Ant
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- 记录一道有意思的js题目
偶然机会,在codewars上面开始做题,遇到一道有意思的题目,记录一下: 题目是这样的: In this kata, you will write a function that returns t ...
- Problem: 棋盘小游戏(一道有意思的acm入门题
Problem Description 现有一个2行13列的棋盘,棋盘上的任意一个位置可以向他临近的8个位置移动.棋盘上的每一个位置的标号由一个大写的英文字母表示.现在给你一个移动的顺序,问你如何设置 ...
- 一道有意思的 CSS 面试题,FizzBuzz ~
FizzBuzz 是一道很有意思的题目.我们来看看题目: 如果遇见了 3 的倍数要说 Fizz,5 的倍数就说 Buzz,如果即是 3 的倍数又是 5 的倍数就说 FizzBuzz. 如果是在一些 ...
- 记一道经典树上Nim游戏
这道题首先是 Hanriver 提出来的,但是大家都不会做,今天看到了一道一模一样的题目 AT2667 题目大意是,每个人删掉一个不是整棵树的原树的子树,给定一个树问游戏状态. 首先,这是需要用到多个 ...
- 一道有意思的笔试题引发的对于new操作符的思考
楼主比较喜欢看一些很短但很有意思的题目,无意间又瞥到了一题,大家不妨可以一试.(原题链接猛戳这里) function Fn1() { this.name = 'peter'; return { nam ...
- [ZJOI2005]九数码游戏(BFS+hash)
Solution 这题的话直接上BFS就可以了,因为要输出方案,所以我们要开一个pre数组记录前驱,最后输出就可以了. 对于状态的记录,一般都用哈希来存,但因为这道题比较特殊,它是一个排列,所以我们可 ...
随机推荐
- 启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration. - Class: java.net.PlainSocketImpl
今天刚好宿舍断网,打开电脑,打开ide工具,启动tomcat后,访问web项目工程,页面显示404,查看控制台,运行报错信息如下: 严重: Exception starting filter stru ...
- 安装Php时候报错信息:virtual memory exhausted: Cannot allocate memory (不能分配内存)
原因是fileinfo这个函数在编译时非常消耗内存,而系统内存又不够了,所以才会出现此问题. 网上找了方法: 1,关闭其他占用大内存的进程. 2,在编译是添加参数 --disable-fileinfo
- mysql 5.5源码包安装
注:由于mysql5.5的源码包安装与mysql之前的版本安装方法不同,故写一篇随笔记录.5.5的版本不再是./configure make make install 这里用到了cmake了,cmak ...
- NET基础课--异常处理X
通常不建议如下的捕获方式 正确的方法是:某一功能函数的入口捕获基本异常即exception,分支方法或片段方法中捕获特定异常 高级: 另附:Fxcop异常监控工具
- 「前端开发者」如何把握住「微信小程序」这波红利?
由于前两周一直在老家处理重要事情,虽然朋友圈被「微信小程序」刷爆了,但并没有时间深入了解. 昨天回广州之后,第一件事情就是把「微信小程序」相关的文章.开发文档.设计规范全部看了一遍,基本上明白了「微信 ...
- Reverse Linked List II java
public static ListNode reverseBetween(ListNode head, int m, int n) { ListNode pre=head,current=head, ...
- Oracle11g客户端安装及plsql配置
1,项目使用的是oracle11g数据库,安装个客户端访问服务器. 到oracle官方下载: http://www.oracle.com/technology/global/cn/software/t ...
- How to Avoid Producing Legacy Code at the Speed of Typing
英语不好翻译很烂.英语好的去看原文. About the Author I am a software architect/developer/programmer.I have a rather p ...
- Android布局控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- GPS坐标转换
由于经常涉及到GPS程序的编写,现在貌似这个GPS是越来越火,越来越多的朋友在编写GPS程序,估计是个人都会遇到这个GPS坐标转换的问题,很惭愧的是,作为一个测量专业出身的学生,我还得时不时的要把这些 ...