BZOJ 1632: [Usaco2007 Feb]Lilypad Pond
题目
1632: [Usaco2007 Feb]Lilypad Pond
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 390 Solved: 109
[Submit][Status]
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
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
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
题解
这道题就是一趟SPFA只不过松弛的时候判断多了一点而已。我有错误的估计了答案的大小没有开long long,Wa了一次QAQ。
代码
/*Author:WNJXYK*/
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; const int Maxn=;
int n,m;
int map[Maxn+][Maxn+]; queue<pair<int,int> > que;
int cost[Maxn+][Maxn+];
int dist[Maxn+][Maxn+];
long long ways[Maxn+][Maxn+];
bool inque[Maxn+][Maxn+];
int dx[]={,,,,,-,-,-,-};
int dy[]={,-,-,,,,,-,-}; inline void BFS(int s_x,int s_y,int e_x,int e_y){
que.push(make_pair(s_x,s_y));
memset(cost,,sizeof(cost));
cost[s_x][s_y]=;
dist[s_x][s_y]=;
ways[s_x][s_y]=;
inque[s_x][s_y]=true;
while(!que.empty()){
int nowx=que.front().first,nowy=que.front().second;
que.pop();
if (nowx==e_x && nowy==e_y) continue;
for (int k=;k<=;k++){
int x=nowx+dx[k],y=nowy+dy[k];
if (!(<=x && x<=n && <=y && y<=m)) continue;
if (map[x][y]==) continue;
int w=;if (map[x][y]==) w=;
if (cost[x][y]>cost[nowx][nowy]+w){
cost[x][y]=cost[nowx][nowy]+w;
dist[x][y]=dist[nowx][nowy]+;
ways[x][y]=ways[nowx][nowy];
if (inque[x][y]==false){
inque[x][y]=true;
que.push(make_pair(x,y));
}
}else if (cost[x][y]==cost[nowx][nowy]+w){
if(dist[x][y]>dist[nowx][nowy]+){
dist[x][y]=dist[nowx][nowy]+;
ways[x][y]=ways[nowx][nowy];
if (inque[x][y]==false){
inque[x][y]=true;
que.push(make_pair(x,y));
}
}else if (dist[x][y]==dist[nowx][nowy]+){
ways[x][y]+=ways[nowx][nowy];
if (inque[x][y]==false){
inque[x][y]=true;
que.push(make_pair(x,y));
}
}
}
}
inque[nowx][nowy]=false;
}
} int main(){
scanf("%d%d",&n,&m);
int sx,sy,ex,ey;
for (int i=;i<=n;i++){
for (int j=;j<=m;j++){
scanf("%d",&map[i][j]);
if (map[i][j]==){
sx=i;sy=j;
}
if (map[i][j]==){
ex=i;ey=j;
}
}
}
BFS(sx,sy,ex,ey);
if (cost[ex][ey]==cost[][] || ways[ex][ey]==){
printf("-1\n");
}else{
printf("%d\n%d\n%lld\n",cost[ex][ey],dist[ex][ey],ways[ex][ey]);
}
return ;
}
BZOJ 1632: [Usaco2007 Feb]Lilypad Pond的更多相关文章
- BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】
直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...
- 1632: [Usaco2007 Feb]Lilypad Pond
1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 404 Solved: 118[Sub ...
- 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...
- bzoj1632 [Usaco2007 Feb]Lilypad Pond
Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼.这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方 ...
- BZOJ1632: [Usaco2007 Feb]Lilypad Pond SPFA+最短路计数
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- BZOJ1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘
一傻逼题调了两天.. n<=30 * m<=30的地图,0表示可以放平台,1表示本来有平台,2表示不能走,3起点4终点,走路方式为象棋的日字,求:从起点走到终点,至少要放多少平台,以及放平 ...
- Bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 中位数,数学
1696: [Usaco2007 Feb]Building A New Barn新牛舍 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 394 Solve ...
- BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )
这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...
随机推荐
- centos6.5设备mysql5.6
1. 首先检查版本号number # uname -a 要么 # cat /etc/redhat-release CentOS release 6.6 (Final) 2. 下载并安装Mysql的yu ...
- 在SSIS包中使用 Checkpoint从失败处重新启动包
使用SSIS做ETL的过程中会遇到各种各样的错误,对于一些大数据量的Job失败以后我们不希望重新运行,因为重新运行的时间开销是非常大的,我们只希望从失败的部分开始运行,这样可以省去很多的时间. SSI ...
- Nhibernate初入门基本配置(一)
文章出处:http://www.cnblogs.com/GoodHelper/archive/2011/02/14/nhiberante_01.html 一.NHibernate简介 什么是?NHib ...
- NSFileManager的应用
单例,是在 一个文件中只创建一次就能够全部一起共享,多创建的地址是相同的 NSFileManager *manager=[NSFileManager defaultManager]; 是一个单例 ...
- WPF多线程下载文件,有进度条
//打开对话框选择文件 private void OpenDialogBox_Click(object sender, RoutedEventArgs e) { ...
- 虚拟机ping不通主机
centos ping不通主机 首先检查网络设备 ifconfig -a 如果有eth0 , 又存在 eth1 . 那么service eth1 stop 然后在ping主机.(以上前提是网络地址设 ...
- jQuery selector 选择器
基本选择器 1. id选择器(指定id元素)将id="one"的元素背景色设置为黑色.(id选择器返单个元素) $(document).ready(function () { $( ...
- spoj 375 QTREE - Query on a tree 树链剖分
题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...
- Java Socket 入门2 Java与C# Socket 通信
参考http://www.cnblogs.com/cdtarena/p/3184313.html 这里以C#作为服务端 其实不论C#是服务端还是客户端都不是主要问题 毕竟不论客户端还是服务端 都包括 ...
- ASP.NET MVC进阶之路:深入理解依赖注入(DI)和控制反转(IOC)
0X1 什么是依赖注入 依赖注入(Dependency Injection),是这样一个过程:某客户类只依赖于服务类的一个接口,而不依赖于具体服务类,所以客户类只定义一个注入点.在程序运行过程中,客户 ...