poj 3271 Lilypad Pond bfs
因为有了1的存在,使得问题变得比较难搞了,所以比较简单的做法就是把1去掉,先做一次bfs,处理出每个点能够一步到达的点(一定是1步).
然后就可以在新图上用bfs算出两个点之间的最短路,和最短路的个数。(至于原题问的为什么是这个,很简单,因为建造的香蒲要最少,所以不会重复建造,不会多建造,所以就是求最短路,至于路径数,因为现在路径长度是简单递增的,所以直接累加就可以了)。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=30+9;
int dist[maxn][maxn],a[maxn][maxn];
int n,m;
bool d[maxn][maxn][maxn][maxn];
int quex[maxn*maxn],quey[maxn*maxn];
long long ans[maxn][maxn];
bool text[maxn][maxn];
void bfs2(int t,int s)
{
int front=1,end=0;
quex[++end]=t;
quey[end]=s;
memset(dist,50,sizeof(dist));
memset(ans,0,sizeof(ans));
dist[t][s]=0;
ans[t][s]=1;
while(front<=end)
{
int x=quex[front],y=quey[front++];
if(a[x][y]==2) continue;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(d[x][y][i][j])
{
if(dist[i][j]==dist[x][y]+1)
ans[i][j]+=ans[x][y];
else if(dist[i][j]>dist[x][y]+1)
{
dist[i][j]=dist[x][y]+1;
ans[i][j]=ans[x][y];
quex[++end]=i;
quey[end]=j;
}
}
}
} void bfs(int t,int s)
{
memset(text,0,sizeof(text));
int front=1,end=0;
quex[++end]=t;
quey[end]=s;
text[t][s]=1;
while(front<=end)
{
int x=quex[front],y=quey[front++];
for(int i=-1;i<=1;i+=2)
for(int j=-1;j<=1;j+=2)
for(int k=1;k<=2;k++)
{
int tmp=1;
if(k==1) tmp=2;
int tox=x+i*k,toy=j*tmp+y;
if(tox>=1&&tox<=n&&toy>=1&&toy<=m)
if(!text[tox][toy])
{
text[tox][toy]=1;
if(a[tox][toy]==1)
{
quex[++end]=tox;
quey[end]=toy;
}
else
{
d[t][s][tox][toy]=1;
}
}
}
}
} int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
int t,s,tox,toy;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==3)
{
t=i;
s=j;
}
else if(a[i][j]==4)
{
tox=i;
toy=j;
}
}
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
bfs(i,j);
bfs2(t,s);
if(dist[tox][toy]>1e3)
{
printf("-1\n");
}
else
{
printf("%d\n%lld\n",dist[tox][toy]-1,ans[tox][toy]);
}
}
return 0;
}
poj 3271 Lilypad Pond bfs的更多相关文章
- BZOJ 1632: [Usaco2007 Feb]Lilypad Pond
题目 1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 390 Solved: 109[ ...
- 1632: [Usaco2007 Feb]Lilypad Pond
1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 404 Solved: 118[Sub ...
- 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告
P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...
- bzoj1698 / P1606 [USACO07FEB]白银莲花池Lilypad Pond
P1606 [USACO07FEB]白银莲花池Lilypad Pond 转化为最短路求解 放置莲花的方法如果直接算会有重复情况. 于是我们可以先预处理和已有莲花之间直接互相可达的点,将它们连边(对,忽 ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond
P1606 [USACO07FEB]荷叶塘Lilypad Pond 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令 ...
- P1606 [USACO07FEB]荷叶塘Lilypad Pond(最短路计数)
P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...
- 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】
直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...
随机推荐
- 关于Core Data的一些整理(三)
关于Core Data的一些整理(三) 关于Core Data Stack的四种类与它们的关系如下: NSManagedObjectModel NSPersistentStore NSPersiste ...
- pod install后出现的错误
[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use T ...
- 对C#泛型实例化对像--转
最近在编写一套开发框架结构主要应用.Net 3.5以上的框架开发与应用.在此框架中应用了较多的泛型.下面来讲讲对泛型的实例化,以代码为例,如: public class A { } public cl ...
- 解决jQuery插件sliderjs, 点击插件分页,导航按钮后不能重新开始.
jQuery SlidesJS - Can't restart animation after clicking on navigation or pagination <!DOCTYPE ht ...
- Qt信号槽中槽函数为虚函数的一些感想
有时候,在写connect的时候会去犹豫一个问题----我的槽函数到底需不需要为虚函数.这个问题在我每次写connect的时候我都会反问自己,因为确实,如果你不去深究里面的moc,你发现不了太多问题. ...
- [标签] action的使用
1.描述 This tag enables developers to call actions directly from a JSP page by specifying the action n ...
- php 数组 类对象 值传递 引用传递 区别
一般的数据类型(int, float, bool)不做这方面的解说了 这里详细介绍一下数组和的类的对象作为参数进行值传递的区别 数组值传递 实例代码: <?php function main() ...
- window.onload() 等待所有的数据加载都完成之后才会触发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- kafka+storm连接
本项目为maven项目,需要添加必要的storm库,以及kafka依赖,使用storm自带的storm-kafka进行连接,根据自己集群环境 <dependency> <groupI ...
- 转:微博CacheService架构浅析
文章来自于:http://www.infoq.com/cn/articles/weibo-cacheservice-architecture 微博作为国内最大的社交媒体网站之一,每天承载着亿万用户的服 ...