poj3271 Lilypad Pond bfs
处理出每个点一步能到的点再统计路径。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int dx[9]={0,-2,-2,-1,-1,1,1,2,2};
int dy[9]={0,-1,1,-2,2,-2,2,-1,1};
const int maxn=30+9;
int dist[maxn][maxn],a[maxn][maxn];
int n,m;
bool d[maxn][maxn][maxn][maxn];
long long ans[maxn][maxn];
bool text[maxn][maxn];
bool ok(int x,int y)
{if(x>0&&x<=n&&y>0&&y<=m)return true;return false;
}
void bfs2(int x,int y)
{queue<int>qx,qy;qx.push(x);qy.push(y);memset(dist,0x3f3f3f3f,sizeof(dist));memset(ans,0,sizeof(ans));dist[x][y]=0;ans[x][y]=1;while(!qx.empty()){int tx=qx.front();int ty=qy.front();qx.pop();qy.pop();if(a[tx][ty]==2) continue;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(d[tx][ty][i][j]){if(dist[i][j]==dist[tx][ty]+1)ans[i][j]+=ans[tx][ty];else if(dist[i][j]>dist[tx][ty]+1){dist[i][j]=dist[tx][ty]+1;ans[i][j]=ans[tx][ty];qx.push(i);qy.push(j);}}}}}
}
void bfs(int x,int y)
{memset(text,0,sizeof(text));queue<int>qx,qy;qx.push(x);qy.push(y);text[x][y]=1;while(!qx.empty()){int tx=qx.front();int ty=qy.front();qx.pop();qy.pop();for(int i=1;i<=8;i++){int xx=tx+dx[i];int yy=ty+dy[i];if(!ok(xx,yy)) continue;if(text[xx][yy]) continue;text[xx][yy]=1;if(a[xx][yy]==1){qx.push(xx);qy.push(yy);}else{d[x][y][xx][yy]=1;}}}
}
int main()
{scanf("%d %d",&n,&m);{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;
}
poj3271 Lilypad Pond bfs相关推荐
- poj 3271 Lilypad Pond bfs
因为有了1的存在,使得问题变得比较难搞了,所以比较简单的做法就是把1去掉,先做一次bfs,处理出每个点能够一步到达的点(一定是1步). 然后就可以在新图上用bfs算出两个点之间的最短路,和最短路的个数 ...
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond(BFS)
1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MB Submit: 688 Solved: 230 [S ...
- bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘(BFS)
1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 550 Solved: 1 ...
- Problem : [usaco2007 Feb]Lilypad Pond
Problem : [usaco2007 Feb]Lilypad Pond Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼. 这个长方形的池子被分割成了 ...
- 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】
直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...
- 洛谷P1606 Lilypad Pond G
传送门 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enjoyment and exercise. The recta ...
- [USACO07FEB] Lilypad Pond
https://www.luogu.org/problem/show?pid=1606 题目描述 FJ has installed a beautiful pond for his cows' aes ...
- 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond(spfa+最短路计数) 题解
题目来源: https://www.luogu.org/problemnew/show/P1606 题目描述: 题目描述 FJ has installed a beautiful pond for h ...
最新文章
- svm rbf人脸识别 yale_实操课——机器学习之人脸识别
- c语言二进制long,C语言填空题 把从键盘上输入的十进制数(long型)以二进制~十六进制任一制式的形式输出#include main(){...
- android标题返回,【Android开发】自定义控件——带返回键标题栏
- C与java通讯小结
- 数据库DBA(3年以内需求)
- 安卓 原生okhttp使用get与post获取网络数据
- 差点令金融世界崩塌的数学公式
- 关于STL的一些笔记
- shell下将文件中的分隔符从‘\t‘转换成‘,‘
- Java 初始化 代码块_Java中初始化块详解及实例代码
- python求圆柱体的体积_Python 常见优化技巧,让你的程序溜溜的跑起来!
- VMware workstation9 Virtual Machine Network
- linux系统监控、诊断工具摘录top IO wait lsof
- 金税盘、税控盘、税务UKey快速批量抄税清卡的一种方法分享
- 支付宝小程序中使用阿里字体图标
- html简单实现自动轮播代码怎么写,HTML图片轮播代码怎么写?
- Qt程序运行时出现:0xc000007b错误参考解决方法
- [HAL]STM32F1光照度测量BH1750 串口输出
- SpringBoot微服务技术中进程间通信原理
- 数据库 流量切分_ABTEST平行流量切分和分层流量切分高效实现及优缺点分析