bzoj 1632: [Usaco2007 Feb]Lilypad Pond (spfa)

1632: [Usaco2007 Feb]Lilypad Pond

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 600   Solved: 189
[ Submit][ Status][ Discuss]

Description

Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼。这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方形格子的 。某些格子上有惊人的坚固的莲花,还有一些岩石,其余的只是美丽,纯净,湛蓝的水。 贝茜正在练习芭蕾舞,她从一个莲花跳跃到另一个莲花,当前位于一个莲花。她希望在莲花上一个一个的跳,目标是另一个给定莲花。她能跳既不入水,也不到一个岩石上。 令门外汉惊讶的是,贝茜的每次的跳跃像中国象棋的马一样:横向移动1,纵向移动2,或纵向移动1,横向移动2。贝茜有时可能会有多达8个选择的跳跃。 Farmer John 在观察贝茜的芭蕾舞联系,他意识到有时候贝茜有可能跳不到她想去的目的地,因为路上有些地方没有莲花。于是他想要添加几个莲花使贝茜能够完成任务。一贯节俭的Farmer John想添加最少数量的莲花。当然,莲花不能放在石头上。 请帮助Farmer John确定必须要添加的莲花的最少数量。在添加的莲花最少基础上,算出贝茜从起始点跳到目标点需要的最少的步数。最后,还要算出满足添加的莲花的最少数量时,跳跃最少步数的跳跃路径的条数。

Input

第 1 行: 两个整数 M , N

第 2..M + 1 行:第 i + 1 行,第 i + 1 行 有 N 个整数,表示该位置的状态: 0 为水; 1 为莲花; 2 为岩石; 3 为贝茜开始的位置; 4 为贝茜要去的目标位置.

Output

第 1 行: 一个整数: 需要添加的最少的莲花数. 如果无论如何贝茜也无法跳到,输出 -1.

 第 2 行: 一个整数: 在添加的莲花最少基础上,贝茜从起始点跳到目标点需要的最少的步数。如果第1行输出-1,这行不输出。 第 3 行: 一个整数: 添加的莲花的最少数量时,跳跃步数为第2行输出的值的跳跃路径的条数 如果第1行输出-1,这行不输出。

Sample Input

4 8
0 0 0 1 0 0 0 0
0 0 0 0 0 2 0 1
0 0 0 0 0 4 0 0
3 0 0 0 0 0 1 0

Sample Output

2
6
2

输出说明

至少要添加2朵莲花,放在了'x'的位置。

0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0
0 x 0 0 0 2 0 1 0 0 0 0 0 2 0 1
0 0 0 0 x 4 0 0 0 0 x 0 x 4 0 0
3 0 0 0 0 0 1 0 3 0 0 0 0 0 1 0
贝茜至少要条6步,有以下两种方案

0 0 0 C 0 0 0 0 0 0 0 C 0 0 0 0
0 B 0 0 0 2 0 F 0 0 0 0 0 2 0 F
0 0 0 0 D G 0 0 0 0 B 0 D G 0 0
A 0 0 0 0 0 E 0 A 0 0 0 0 0 E 0

HINT

Source

[ Submit][ Status][ Discuss]

题解:spfa.

我进行了三遍spfa,第一遍保证添加的莲花数最少,第二遍在保证莲花数最小的情况下求最短路,再在保证前两个的条件下求方案。

其实可以一边spfa求解,将三种情况分优先级。。。果然我比较智障。。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define N 33
#define LL long long 
#define pa pair<int,int>
using namespace std;
int dis[N][N],map[N][N],dis1[N][N],inf,can[N][N];
int n,m,sx,sy,tx,ty;
int px[10]={1,1,-1,-1,2,2,-2,-2},py[10]={2,-2,2,-2,1,-1,1,-1};
LL dis2[N][N];
void spfa()
{
	queue<pa> p; 
	memset(dis,127,sizeof(dis)); inf=dis[0][0];
	p.push(make_pair(sx,sy)); can[sx][sy]=1; dis[sx][sy]=0;
	while (!p.empty()){
		pa a=p.front(); p.pop();
		int x=a.first; int y=a.second;
		for (int i=0;i<8;i++) 
		 {
		 	int nowx=x+px[i]; int nowy=y+py[i];
		 	if (nowx<=0||nowy<=0||nowx>n||nowy>m||map[nowx][nowy]==2) continue;
		 	int len;
			if (map[nowx][nowy]==0) len=1;
			else len=0;
			if (dis[nowx][nowy]>dis[x][y]+len) {
				dis[nowx][nowy]=dis[x][y]+len;
				if (can[nowx][nowy]==0) {
					can[nowx][nowy]=1;
					p.push(make_pair(nowx,nowy));
				}
			} 
			can[x][y]=0;
		 }
	}
}
void spfa1()
{
	queue<pa> p; 
	memset(dis1,127,sizeof(dis1));
	memset(can,0,sizeof(can));
	p.push(make_pair(sx,sy)); can[sx][sy]=1; dis1[sx][sy]=0;
	while (!p.empty()){
		pa a=p.front(); p.pop();
		int x=a.first; int y=a.second;
		for (int i=0;i<8;i++) 
		 {
		 	int nowx=x+px[i]; int nowy=y+py[i];
		 	if (nowx<=0||nowy<=0||nowx>n||nowy>m||map[nowx][nowy]==2) continue;
		 	int len;
			if (map[nowx][nowy]==0) len=1;
			else len=0;
		 	if (dis[nowx][nowy]==dis[x][y]+len&&dis1[nowx][nowy]>dis1[x][y]+1)
		    {
				dis1[nowx][nowy]=dis1[x][y]+1;
				if (can[nowx][nowy]==0) {
					can[nowx][nowy]=1;
					p.push(make_pair(nowx,nowy));
				}
			} 
		 }
		can[x][y]=0;
	}
}
void spfa2()
{
	queue<pa> p;
	memset(dis2,0,sizeof(dis2));
	memset(can,0,sizeof(can));
	p.push(make_pair(sx,sy)); can[sx][sy]=1; dis2[sx][sy]=1;
		while (!p.empty()){
		pa a=p.front(); p.pop();
		int x=a.first; int y=a.second;
		for (int i=0;i<8;i++) 
		 {
		 	int nowx=x+px[i]; int nowy=y+py[i];
		 	if (nowx<=0||nowy<=0||nowx>n||nowy>m||map[nowx][nowy]==2) continue;
		 	int len;
			if (map[nowx][nowy]==0) len=1;
			else len=0;
		 	if (dis1[nowx][nowy]==dis1[x][y]+1&&dis[nowx][nowy]==dis[x][y]+len)
		    {
				dis2[nowx][nowy]+=dis2[x][y];
				if (can[nowx][nowy]==0) {
					can[nowx][nowy]=1;
					p.push(make_pair(nowx,nowy));
				}
			} 
			can[x][y]=0;
		 }
	}
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	 for (int j=1;j<=m;j++) {
	 	scanf("%d",&map[i][j]);
	 	if (map[i][j]==3) sx=i,sy=j;
	 	else if(map[i][j]==4) tx=i,ty=j;
	 }
	spfa();
	if (dis[tx][ty]==inf) {
		cout<<"-1"<<endl;
		return 0;
	} 
	printf("%d\n",dis[tx][ty]);
	spfa1(); printf("%d\n",dis1[tx][ty]);
	spfa2(); 
	printf("%lld\n",dis2[tx][ty]);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值