处理出每个点一步能到的点再统计路径。

#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相关推荐

  1. poj 3271 Lilypad Pond bfs

    因为有了1的存在,使得问题变得比较难搞了,所以比较简单的做法就是把1去掉,先做一次bfs,处理出每个点能够一步到达的点(一定是1步). 然后就可以在新图上用bfs算出两个点之间的最短路,和最短路的个数 ...

  2. 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 ...

  3. bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘(BFS)

    1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 550  Solved: 1 ...

  4. Problem : [usaco2007 Feb]Lilypad Pond

    Problem : [usaco2007 Feb]Lilypad Pond Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼. 这个长方形的池子被分割成了 ...

  5. P1606 [USACO07FEB]荷叶塘Lilypad Pond(最短路计数)

    P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...

  6. bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】

    直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...

  7. 洛谷P1606 Lilypad Pond G

    传送门 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enjoyment and exercise. The recta ...

  8. [USACO07FEB] Lilypad Pond

    https://www.luogu.org/problem/show?pid=1606 题目描述 FJ has installed a beautiful pond for his cows' aes ...

  9. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond(spfa+最短路计数) 题解

    题目来源: https://www.luogu.org/problemnew/show/P1606 题目描述: 题目描述 FJ has installed a beautiful pond for h ...

最新文章

  1. svm rbf人脸识别 yale_实操课——机器学习之人脸识别
  2. c语言二进制long,C语言填空题 把从键盘上输入的十进制数(long型)以二进制~十六进制任一制式的形式输出#include main(){...
  3. android标题返回,【Android开发】自定义控件——带返回键标题栏
  4. C与java通讯小结
  5. 数据库DBA(3年以内需求)
  6. 安卓 原生okhttp使用get与post获取网络数据
  7. 差点令金融世界崩塌的数学公式
  8. 关于STL的一些笔记
  9. shell下将文件中的分隔符从‘\t‘转换成‘,‘
  10. Java 初始化 代码块_Java中初始化块详解及实例代码
  11. python求圆柱体的体积_Python 常见优化技巧,让你的程序溜溜的跑起来!
  12. VMware workstation9 Virtual Machine Network
  13. linux系统监控、诊断工具摘录top IO wait lsof
  14. 金税盘、税控盘、税务UKey快速批量抄税清卡的一种方法分享
  15. 支付宝小程序中使用阿里字体图标
  16. html简单实现自动轮播代码怎么写,HTML图片轮播代码怎么写?
  17. Qt程序运行时出现:0xc000007b错误参考解决方法
  18. [HAL]STM32F1光照度测量BH1750 串口输出
  19. SpringBoot微服务技术中进程间通信原理
  20. 数据库 流量切分_ABTEST平行流量切分和分层流量切分高效实现及优缺点分析

热门文章

  1. 根据“雷石安全实验室”《一周网安动态》自动生成安全通告
  2. 1的k次方到n的k次方
  3. 【龙芯1c库】封装硬件定时器接口和使用示例
  4. 【翻译自HVR官方手册】HVR数据复制软件介绍与术语解释
  5. python爬虫爬商品库存_python爬虫实践——爬取京东商品信息
  6. Week1 Assignment - Princeton-Algorithms-PartI
  7. 3招让你轻松找到高质量外链平台
  8. python网络爬虫实战3——抓取新闻内文相关信息
  9. 信号发生器的作用和组成(功率放大器和信号发生器搭配)
  10. Python画置信区间图