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


Source: Asia 1999, Kyoto (Japan)

题意:

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

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

题目分析:

一开始我以为是普通搜索,后来才知道需要记忆化搜索,否则就出不来。既然记忆化搜索,那么你就不能随便的标记了,注意边界,注意题目给的条件

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
using namespace std;
int w,h,sx,sy,ex,ey;
int map[8][8];
int dp[8][8];
int fx[4]={1,0,-1,0};
int fy[4]={0,1,0,-1};

struct node
{
  int x,y;
  int hp;
  int step;
};

void input()
{
	for(int i=0;i<h;i++)//注意行和列,一不小心就错了
 	 for(int j=0;j<w;j++)
 	 {
 	   scanf("%d",&map[i][j]);
		if(map[i][j]==2)
		{
		 sx=i;
		 sy=j;
		}
		if(map[i][j]==3)
		{
		 ex=i;
		 ey=j;
		}
		dp[i][j]=0;
	 }
}

int valid(node p)
{
  if(p.x>=0&&p.x<h&&p.y>=0&&p.y<w)
   return 1;
   else
   return 0;
}

queue<node>q;
void BFS()
{
  node p1,p2;
  p1.x=sx;
  p1.y=sy;
  p1.hp=6;
  p1.step=0;

  dp[sx][sy]=6;//注意这儿

  while(!q.empty())
   q.pop();

   q.push(p1);
   while(!q.empty())
   {
   	 p1=q.front();
   	 q.pop();

   	 if(p1.x==ex&&p1.y==ey&&p1.hp>0)
   	 {
   	  printf("%d\n",p1.step);
	  return ;
	 }
	   if(p1.hp==1)
	    continue ;
	 	for(int i=0;i<4;i++)
	 	{
	 	  p2.x=p1.x+fx[i];
		  p2.y=p1.y+fy[i];
		  if(map[p2.x][p2.y]&&valid(p2)&&dp[p2.x][p2.y]<dp[p1.x][p1.y])
		  {
			p2.step=p1.step+1;
			if(p2.x==ex&&p2.y==ey)
			{
				printf("%d\n",p2.step);
				return ;
			}
			if(map[p2.x][p2.y]==4)//食物杯吃后应该是能走的路
			{
				 dp[p2.x][p2.y]=6;
				 map[p2.x][p2.y]=1;
			}
			else
			dp[p2.x][p2.y]=dp[p1.x][p1.y]-1;
		        p2.hp=dp[p2.x][p2.y];
		  	q.push(p2);
		  }
		}
   }
   printf("-1\n");
}

int main()
{
 while(scanf("%d%d",&w,&h),(w||h))
 {
   input();
   BFS();
 }
 return 0;
} 

看到人家的另外一种思路:

直接用优先队列就OK了

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
using namespace std;
int w,h,sx,sy,ex,ey;
int map[8][8];
int fx[4]={1,0,-1,0};
int fy[4]={0,1,0,-1};

struct node
{
  int x,y;
  int hp;
  int step;
  friend bool operator< (node a,node b)
  {
  	return a.step>b.step;
  }
};

void input()
{
	for(int i=0;i<h;i++)
 	 for(int j=0;j<w;j++)
 	 {
 	   scanf("%d",&map[i][j]);
		if(map[i][j]==2)
		{
		 sx=i;
		 sy=j;
		}
		if(map[i][j]==3)
		{
		 ex=i;
		 ey=j;
		}
	 }
}

int valid(node p)
{
  if(p.x>=0&&p.x<h&&p.y>=0&&p.y<w)
   return 1;
   else
   return 0;
}

priority_queue<node>q;
void BFS()
{
  node p1,p2;
  p1.x=sx;
  p1.y=sy;
  p1.hp=6;
  p1.step=0;

  while(!q.empty())
   q.pop();

   q.push(p1);
   while(!q.empty())
   {
   	 p1=q.top();
   	 q.pop();

   	 if(p1.x==ex&&p1.y==ey&&p1.hp>0)
   	 {
   	  printf("%d\n",p1.step);
	  return ;
	 }
	  if(p1.hp==1)
	   continue ;
	 	for(int i=0;i<4;i++)
	 	{
	 	  p2.x=p1.x+fx[i];
		  p2.y=p1.y+fy[i];
		  if(map[p2.x][p2.y]&&valid(p2))
		  {
			p2.step=p1.step+1;
			if(p2.x==ex&&p2.y==ey)
			{
				printf("%d\n",p2.step);
				return ;
			}
			if(map[p2.x][p2.y]==4)
			{
				 p2.hp=6;
				 map[p2.x][p2.y]=1;
			}
			else
			p2.hp=p1.hp-1;

		  	q.push(p2);
		  }
		}
   }
   printf("-1\n");
}

int main()
{
 while(scanf("%d%d",&w,&h),(w||h))
 {
   input();
   BFS();
 }
 return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 15:51:05

Zoj 1671 Walking Ant(BFS+优先队列||记忆化搜索)的相关文章

HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

题目地址:HDU 1428 先用BFS+优先队列求出所有点到机房的最短距离,然后用记忆化搜索去搜. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #incl

ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案

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 h

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

搜索分析(DFS、BFS、递归、记忆化搜索)

搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2)递归(从中间向两边) 1 //递归一定要写成记忆化递归 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool vis[11]; 5 int count1=0; 6 7 void search(int n){ 8 count1++; 9

【BZOJ 1415】 1415: [Noi2005]聪聪和可可 (bfs+记忆化搜索+期望)

1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1640  Solved: 962 Description Input 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行,每行两个整数,第i+2行的两个整数Ai和Bi表示景点Ai和景点Bi之间有一条路. 所有的路都是无向的,即

FZU 2092 bfs+记忆化搜索

晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分别改变位置 结果超内存 分析了一下应该是队列超了内存 毕竟如果每个点都存入的话一个点最多可以衍生出25个node 然后t最大为200s 一定会超 之间还发生了一些并不能理解的bug 被逼到最后重构才拿到了一个超内存 名次也不好 急需一个ac来赶上去 简直要烧起来了 侧面反映心理素质还是差一些 当时未

FZU 2092 收集水晶 BFS记忆化搜索

用了两个pii代码有点长…… 记忆化搜索主要还是用用dp数组去记录并更新状态 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<string> 7 #include<ctime> 8 #include<map> 9 #include<s