题目来源:

https://www.luogu.org/problemnew/show/P1606

题目描述:

题目描述

FJ has installed a beautiful pond for his cows' aesthetic enjoyment and exercise.

The rectangular pond has been partitioned into square cells of M rows and N columns (1 ≤ M ≤ 30; 1 ≤ N ≤ 30). Some of the cells have astonishingly sturdy lilypads; others have rocks; the remainder are just beautiful, cool, blue water.

Bessie is practicing her ballet moves by jumping from one lilypad to another and is currently located at one of the lilypads. She wants to travel to another lilypad in the pond by jumping from one lilypad to another.

Surprising only to the uninitiated, Bessie's jumps between lilypads always appear as a chess-knight's move: one move in one direction and then two more in the orthogonal direction (or perhaps two in one direction and then one in the orthogonal direction).

Farmer John is observing Bessie's ballet drill and realizes that sometimes she might not be able to jump to her destination lilypad because intermediary lilypads are missing.

Ever thrifty, he wants to place additional lilypads so she can complete her quest (perhaps quickly, perhaps by using a large number of intermediate lilypads). Of course, lilypads cannot be placed where rocks already intrude on a cell.

Help Farmer John determine the minimum number of additional lilypads he has to place, and in how many ways he can place that minimum number.

为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘。这个长方形的池子被分成了M行N列个方格(1≤M,N≤30)。一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是美丽、纯净、湛蓝的水。

贝西正在练习芭蕾舞,她站在一朵莲花上,想跳到另一朵莲花上去,她只能从一朵莲花跳到另一朵莲花上,既不能跳到水里,也不能跳到岩石上。

贝西的舞步很像象棋中的马步:每次总是先横向移动一格,再纵向移动两格,或先纵向移动两格,再横向移动一格。最多时,贝西会有八个移动方向可供选择。

约翰一直在观看贝西的芭蕾练习,发现她有时候不能跳到终点,因为中间缺了一些荷叶。于是他想要添加几朵莲花来帮助贝西完成任务。一贯节俭的约翰只想添加最少数量的莲花。当然,莲花不能放在石头上。

请帮助约翰确定必须要添加的莲花的最少数量,以及有多少种放置这些莲花的方法。

输入输出格式

输入格式:

【输入】

第一行:两个用空格分开的整数:M和N

第二行到M+1行:第i+1行有N个用空格分开的整数,描述了池塘第i行的状态:

0为水,1为莲花,2为岩石,3为贝西所在的起点,4为贝西想去的终点。

输出格式:

【输出】

第一行:一个整数,需要增加的最少莲花数;如果无解,输出-1。

第二行:放置这些莲花的方案数量,保证这个数字不会超过一个64位的有符号整数,

如果第一行是-1,不要输出第二行。

输入输出样例

输入样例#1: 复制

4 5
1 0 0 0 0
3 0 0 0 0
0 0 2 0 0
0 0 0 4 0

输出样例#1: 复制

2
3

说明

【样例说明】

池塘分成四行五列,贝西的起点在第二行第一列,想去的终点在第四行第四列,池

塘里一共有三朵莲花和一块石头。

最少需要两朵莲花,有三种方式可以放置,

页6 如下X所示:

10000 10X00 10X00

30X00 30000 3000X

00200 0X200 00200

0X040 00040 00040

解题思路:

感觉这题的思路十分巧妙,第一个问是比较简单的,我们只要每个不是2的点,能到的点连一条边,如果那点是1,边权就是0,否则边权是1,然后跑一遍最短路就行,但是第二问就很难办了,因为本题是求放莲花的种数,不是最短路条数,比如如果不用莲花就能到达,可能有好几条最短路,可是答案应该是1,所以我们应该要把原图中的边权为0的边删去,我们可以在遇到已经是莲花的点,在递归的找这点能到的点,直到找到不是莲花的点,然后连一条边权为1的边,就相当于用了那朵莲花,那样答案就是最短路和最短路条数。。。。

代码:

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
const int N =40;
const int M = 40*40;
using namespace std;
int n,m,map[N][N],S,T,dis[M];
long long ans[M];
bool vis[M],jl[N][N];
int head[200005],nxt[100010],ver[100010],edge[100010],tot;
int dir[8][2]={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
void addedge(int x,int y,int z)
{ver[++tot]=y;edge[tot]=z;nxt[tot]=head[x];head[x]=tot;
}//邻接表存边
void add(int xy,int x,int y)
{jl[x][y]=1;for(int i=0;i<8;i++){int xx=x+dir[i][0],yy=y+dir[i][1];if(xx<1||xx>n||yy<1||yy>m||map[xx][yy]==2||jl[xx][yy])continue;jl[xx][yy]=1;//标记以免重复 if(map[xx][yy]==1)add(xy,xx,yy);//是荷叶就继续建边; else addedge(xy,(xx-1)*m+yy,1);}
}
queue<int>q;
int main()
{std::ios::sync_with_stdio(false);//怕超时关闭读入同步 memset(dis,inf,sizeof(dis));cin>>n>>m;tot=0;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){cin>>map[i][j];if(map[i][j]==3){S=(i-1)*m+j;}if(map[i][j]==4){T=(i-1)*m+j;}}for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){if(map[i][j]!=1&&map[i][j]!=2){memset(jl,0,sizeof(jl));//每次清空标记 add((i-1)*m+j,i,j);       }}q.push(S);dis[S]=0;ans[S]=1;vis[S]=1;while(!q.empty()){int now=q.front();q.pop();vis[now]=0;for(int i=head[now];i;i=nxt[i]){int v=ver[i],val=edge[i];if(dis[v]>dis[now]+val){dis[v]=dis[now]+val;ans[v]=ans[now];//最短路计数 if(!vis[v]){vis[v]=1;q.push(v);}}else if(dis[v]==dis[now]+val){ans[v]+=ans[now];//最短路计数 }}}if(dis[T]==inf)cout<<"-1"<<endl;else cout<<dis[T]-1<<endl<<ans[T]<<endl;return 0;
}

洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond(spfa+最短路计数) 题解相关推荐

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

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

  2. 【洛谷】P8787 [蓝桥杯 2022 省 B] 砍竹子 的题解

    [洛谷]P8787 [蓝桥杯 2022 省 B] 砍竹子 的题解 题目传送门 思路 这个题有两个做法,一种是用 set 或者堆来维护一个高度到区间的映射,另一个用并查集维护区间. 这个题本质是一个最长 ...

  3. 洛谷 1373 dp 小a和uim之大逃离 良心题解

    洛谷 1373 dp 这题还不算太难,,当初看的时候不是很理解题意,以为他们会选择两条不同的路径,导致整体思路混乱 传送门 其实理解题意和思路之后还是敲了不短的时间,一部分身体原因再加上中午休息不太好 ...

  4. 洛谷P1606 Lilypad Pond G

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

  5. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  6. 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  7. 洛谷P1027 Car的旅行路线 计算几何 图论最短路

    题意 求某城到某城的最小花费 一个城中有四个机场,一个城中的机场相互可达,用公路到达,但是不同城的公路的单位路程的 费不同,两个不同城的机场(我不知道相同城可不可以)可以通过机场到达,且飞机单位路程价 ...

  8. 洛谷 - P4009 汽车加油行驶问题(分层图最短路/最小费用最大流)

    题目链接:点击查看 题目大意:给出一个n*n的矩阵表示道路,途中有一些加油站,现在要从点(1,1)到达点(n,n),问最小花费,其中的一些规则如下: 汽车只能沿着网格边行驶,装满油后可以行驶K条边,出 ...

  9. 洛谷 P2921 在农场万圣节Trick or Treat on the Farm题解

    题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定 ...

最新文章

  1. 【说人话】真正意义上讲清楚了如何用$emit()在Vue.js的自定义组件中实现v-model=“”双向绑定
  2. 【划分树】 POJ 2104 HDU 2665 K-th Number 裸题
  3. 【AI白身境】学AI必备的python基础​​​​​​​
  4. 如何做SEO项目管理?
  5. 网页设计布局选择:固定,流行和弹性布局 (2010-12-14 13:07:35)
  6. [渝粤教育] 西南科技大学 电子产品制造工艺 在线考试复习资料
  7. Ajax Control Toolkit 出新版了
  8. centos linux7修改主机名,CentOS7操作系统下永久修改主机名
  9. xlwt设置excel字体、对齐方式、边框、颜色、背景色
  10. 缓冲区提前释放,导致H264保存及播放错误
  11. 3DSmax里的nurms toggle命令中文版是什么意思
  12. SCTF-2019 Misc wp
  13. NumberFormat去掉千分位
  14. c语言16qam,基于SIMULINK的OFDM-16QAM系统仿真与分析
  15. JsBarcode条形码组件封装
  16. 新媒体运营必备的实用工具
  17. 一款基于RFID的固定资产管理系统
  18. L1-049. 天梯赛座位分配
  19. cortex m3/m4处理器的复位设计
  20. 《信息检索》课程大作业 实现一个本地搜索引擎

热门文章

  1. li-poly_GitHub - kinglisky/lowpoly: low poly图片风格化工具
  2. Eclipse中使用Maven搭建SSM框架
  3. 冰与火之歌:Python的三元表达式
  4. 432 4.3.2 STOREDRV.Deliver; recipient thread limit exceeded
  5. RF自动化-RIDE(跑自动化注意事项)和(配置环境注意事项)
  6. 我的物联网项目(十八) 城市合伙人战略
  7. RISC-V架构生态及相关学习记录
  8. 考古中怎么判断化石的年代?碳14的半衰期?然后呢?//2021-2-5 知其然,知其所以然。
  9. 明日服务器中断,明日之后服务器断开连接怎么办
  10. python字典键盘添加元素_对python字典元素的添加与修改方法详解