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

题意:2为起点,3为终点,1为空地,4为食物,0为水池不可通过,蚂蚁起始的HP为6,每走一步HP减少1,如果蚂蚁

可以吃到食物则HP恢复为6(到达食物所在地时最少HP要求为1)问蚂蚁需要几步可以从起点走到终点

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m;
int map[10][10];
int x1,x2,y1,y2;
struct node
{
	int x,y;
	int step;
	int time;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;
	}
};
void getmap()
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			scanf("%d",&map[i][j]);
			if(map[i][j]==2)
			{
				x1=i;y1=j;
			}
			else if(map[i][j]==3)
			{
				x2=i;y2=j;
			}
		}
	}
}
int judge(int r,int c)
{
	if(r < 0||r >= n)
	    return 0;
	if(c < 0||c >= m)
	    return 0;
	if(map[r][c]==0)
	    return 0;
	return 1;
}
void bfs()
{
	int i,j;
	int move[4][2]={0,1,0,-1,1,0,-1,0};
	node beg,end;
	priority_queue<node>q;
	beg.x=x1;
	beg.y=y1;
	beg.step=0;
	beg.time=6;
	q.push(beg);
	while(!q.empty())
	{
		end=q.top();
		q.pop();
		if(end.x==x2&&end.y==y2)
		{
			printf("%d\n",end.step);
			return ;
		}
		if(end.time==1)//当到达食物的前一步时血量为1则就会死
		continue;
		for(i=0;i<4;i++)
		{
			beg.x=end.x+move[i][0];
			beg.y=end.y+move[i][1];
			if(judge(beg.x,beg.y))
			{
				if(map[beg.x][beg.y]==4)
				{
					beg.time=6;
					map[beg.x][beg.y]=1;//标记吃过的食物不能再吃
				}
				else
				beg.time=end.time-1;
				beg.step=end.step+1;
				q.push(beg);
			}
		}
	}
	printf("-1\n");
}
int main()
{
	int i,j;
	while(scanf("%d%d",&m,&n),n|m)
	{
		getmap();
		bfs();
	}
	return 0;
}

  

时间: 2024-10-08 23:35:04

zoj 1671 Walking Ant【简单bfs】的相关文章

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 h

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

zoj 1671 Walking Ant

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 h

Zoj1671--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 h

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(int n) { BigInteger c; BigInteger a = BigInteger.valueOf(1); BigInteger b = BigIn

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

LightOJ 1012 简单bfs,水

1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define F(i,a,b) for (int i=a;i<=b;i++) using names