hdoj-- Walking Ant
Walking Ant
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 9
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
/*蚂蚁在2,家在3,4是食物刚开始是hp=6,走一步hp-1,吃过食物之后hp=6,但是到家的时候
hp=0失败,到食物的时候hp=0也失败,失败之后输出-1,bfs查找*/
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[1010][1010];
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int x,y,ex,ey,m,n;
struct node
{
int x,y;
int hp;
int time;
}p,temp;
bool judge(node s)
{
if(s.x<0||s.x>=n||s.y<0||s.y>=m)
return 1;
if(map[s.x][s.y]==0)/*不能越界,不能是墙*/
return 1;
return 0;
}
int bfs()
{
queue<node>q;
while(!q.empty()) q.pop();
p.x=x;
p.y=y;
p.time=0;
p.hp=6;
q.push(p);
//vis[x][y]=1;/*不需要标记,每一步走过之后还可以走回来,但是食物只能吃一次*/
while(!q.empty())
{
p=q.front();
q.pop();
if(p.hp==0) continue;/*到家的时候hp不能等于零*/
if(map[p.x][p.y]==3)
return p.time;
for(int i=0;i<4;i++)
{
temp.x=p.x+dx[i];
temp.y=p.y+dy[i];
temp.time=p.time+1;
if(judge(temp))
continue;
temp.hp=p.hp-1;/*每走一步时间加一,hp-1*/
if(map[temp.x][temp.y]==4&&temp.hp)
{
temp.hp=6;
map[temp.x][temp.y]=1;/*吃过之后标记,表示为路*/
}
//vis[temp.x][temp.y]=1;
q.push(temp);
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&m,&n),m|n)
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2)
{
x=i;y=j;
}
/*if(map[i][j]==3)
{
ex=i;ey=j;
}*/
}
/*for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
printf("%d ",map[i][j]);
printf("\n");
}*/
int num =bfs();
printf("%d\n",num);
}
return 0;
}
hdoj-- Walking Ant的更多相关文章
- 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 ...
- 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 ...
- hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...
- [POJ1852]Ants
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12431 Accepted: 5462 Description An a ...
- Ants(思维)
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12893 Accepted: 5637 Description ...
- Ants (POJ 1852)
题目描述: Description An army of ants walk on a horizontal pole of length l cm, each with a constant spe ...
- UVA - 10714 Ants
最多时间就是每仅仅蚂蚁选择最久的爬行方式 最少时间就是每仅仅蚂蚁选择最快地爬行方式 #include<iostream> #include<map> #include<s ...
随机推荐
- [原创]Linux(CentOS)下安装mongodb
和上一篇一样,装个这个踩了无数个坑…… 1.下载 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel55-3.2.12.tgz ...
- 设置Hadoop的 dataNode的单个Map的内存配置
1.进入hadoop的配置目录 ,找到 环境变量的 $HADOOP_HOME cd $HADOOP_HOME 2.修改dataNode 节点的 单个map的能使用的内存配置 找到配置的文件: /opt ...
- 来源页面地址 上一页面url
Uri uri = Request.UrlReferrer;
- 一个完整的Appium手机自动化测试实例
实现过程: 1.使用环境 appium .安卓SDK .python 本文重点是自动化实例,环境搭建过程省略. 2.找到被测APP的包名和Activity Name 手机连接上电脑后,在DOS环境先使 ...
- apicloud 第三方登录授权、微信、扣扣、微博登录授权
授权登录.接入第三方的配置 例如:微信的登录授权. 首先在模块里面添加 wx 这个模块,然后在项目的配置文件里面进行配置. 配置的时候要现在微信开放平台 https://open.weixin.qq. ...
- centos7网卡重命名为ethx格式
参考:https://www.cnblogs.com/zyd112/p/8143464.html CentOS 7 使用 eth0 这样的传统名称,那么在安装启动(pxe)时,按Tab键在下方输入以下 ...
- Android内存优化————加载长图
项目中总会遇到加载长图的需求,图片的长度可能是手机长度的很多倍,也就是需要通过滑动来查看图片.比较简单的实现方式就是使用ScrollView来加载长图,但是这样做有一个很严重的问题,就是内存消耗严重. ...
- java实现简单窗体小游戏----球球大作战
java实现简单窗体小游戏----球球大作战需求分析1.分析小球的属性: 坐标.大小.颜色.方向.速度 2.抽象类:Ball 设计类:BallMain—创建窗体 BallJPanel—画小 ...
- 【转载】java中的反射
主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...
- Day 1 初识python
1.Python简介 Python的历史 1989年圣诞节:Guido von Rossum开始写Python语言的编译器. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语 ...