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
题解:错了好长时间,真心无语。。。。我用vis数组标记了,最后队友说不用标记,我就把标记去了,把血量吃了变0;终于输出正确答案了。。。。。
优先队列代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
int disx[]={,-,,};
int disy[]={,,,-};
int map[][];
int w,h;
struct Node{
int x,y;
int ph,step;
friend bool operator < (Node a,Node b){
return a.step>b.step;
}
};
void bfs(int sx,int sy){
Node a,b;
priority_queue<Node>dl;
a.x=sx;a.y=sy;
a.ph=;
a.step=;
dl.push(a);
int ans=;
while(!dl.empty()){
a=dl.top();
dl.pop();
for(int i=;i<;i++){
b.x=a.x+disx[i];
b.y=a.y+disy[i];
b.ph=a.ph-;
b.step=a.step+;
if(b.x<||b.y<||b.x>=w||b.y>=h||map[b.x][b.y]==)continue;
if(b.ph<=)continue;
if(map[b.x][b.y]==){
ans=;
printf("%d\n",b.step);
return;
}
if(map[b.x][b.y]==)b.ph=,map[b.x][b.y]=;
dl.push(b);
}
}
//printf("%d\n",step);
if(!ans)puts("-1");
}
int main(){
while(scanf("%d%d",&w,&h),w|h){
int sx,sy;
mem(map,);
for(int y=;y<h;y++)
for(int x=;x<w;x++){
scanf("%d",&map[x][y]);
if(map[x][y]==)sx=x,sy=y;
}
bfs(sx,sy);
}
return ;
}
没用优先队列:
#include<stdio.h>
#include<string.h>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
int disx[]={,-,,};
int disy[]={,,,-};
int map[][];
int w,h,step;
struct Node{
int x,y;
int ph;
};
void bfs(int sx,int sy){
Node a,b;
queue<Node>dl;
a.x=sx;a.y=sy;
a.ph=;
step=;
dl.push(a);
int ans=;
while(!dl.empty()){
step++;
int t=dl.size();
while(t--){
a=dl.front();
dl.pop();
for(int i=;i<;i++){
b.x=a.x+disx[i];
b.y=a.y+disy[i];
b.ph=a.ph-;
if(b.x<||b.y<||b.x>=w||b.y>=h||map[b.x][b.y]==)continue;
if(b.ph<=)continue;
if(map[b.x][b.y]==){
ans=;
printf("%d\n",step);
return;
}
if(map[b.x][b.y]==)b.ph=,map[b.x][b.y]=;
dl.push(b);
}
}
}
//printf("%d\n",step);
if(!ans)puts("-1");
}
int main(){
while(scanf("%d%d",&w,&h),w|h){
int sx,sy;
mem(map,);
for(int y=;y<h;y++)
for(int x=;x<w;x++){
scanf("%d",&map[x][y]);
if(map[x][y]==)sx=x,sy=y;
}
bfs(sx,sy);
}
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 ...
- POJ 2110 Mountain Walking 二分+bfs
传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, low ...
- hdoj-- Walking Ant
Walking Ant Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total S ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- [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 ...
随机推荐
- ApiDemos示例学习(2)——App->Activity->Animation
现在介绍一下com.example.android.app包下的Animation示例. 关键类及函数: ActivityOption overridePendingTransition() make ...
- Android APN配置
APN概念 APN(Access Point Name),即“接入点名称”,用来标识GPRS的业务种类,目前分为两大类:CMWAP(通过GPRS访问WAP业务).CMNET(除了WAP以外的服务目前都 ...
- CC++初学者编程教程(13) 基于Oracle linux 的Oracle12c环境搭建
1设置虚拟机选项 2 设置文件夹共享 3启动文件夹共享向导 4 设置共享文件夹 5 启用共享 6 关闭虚拟机设置 7 开启虚拟机 8 登陆帐户 9 看见虚拟机桌面 10 安装vmwaretools 1 ...
- mysql主从同步错误解决和Slave_IO_Running: NO
1.出现错误提示. Slave I/O: error connecting to master 'backup@192.168.1.x:3306' - retry-time: 60 retries: ...
- Android4.2中Phone的P-sensor的应用的分析。
先说现象,现象就是来电话,接通电话,把手机屏幕靠近脸部,遮挡住P-sensor,屏幕变黑了,不遮挡住P-sensor,屏幕就点亮了.接着我们来看看代码流程. 步骤一: 在PhoneGlobals.ja ...
- Linux下实现视频读取(二)---camera參数设定
Camera的可设置项极多,V4L2 支持了不少.但Sam之前对这些设置的使用方法和涵义都是在看videodev2.h中边看边理解.感觉很生涩. 直到写这篇blog时,才发现v4l2有专门的SPEC来 ...
- jQuery源码笔记——二
jQuery选择这样返回对象 var jQuery = function( selector, context ) { return new jQuery.fn.init( selector, con ...
- ORA-01950: 对表空间 'NAMETABLESPACE' 无权限
只要将Role下的Resource权限赋予给当前用户即可解决上述问题.
- Oracle的表连接方式
Oracle的表连接方式: 1.Nl Join连接(嵌套连接) 2.Hash Join(哈希连接) 3.Merge Sort Join(排序合并连接) 各种连接的使用场景: 1. 排序合并连接是偏向于 ...
- getopt()函数
在讨论这个函数之前我们先理解两个概念:选项及其参数 gcc -o program program.c 在上述命令中 -o 就是其选项 program就是参数. getopt(): 头文件: #incl ...