P1606 [USACO07FEB]荷叶塘Lilypad Pond

题目描述

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

/*
首先你直接建出图后会发现最短路是对的但是方案数是错的
因为有荷叶的存在图中存在很多0边
那么怎么办呢 既然是荷叶的锅不要荷叶不就得了
每个点对于它能一步到达的所有点连边 通过BFSBFS完成
直接跑最短路计数即可
*/
#include<iostream>
#include<cstdio>
#include<queue>using namespace std;
const int N=35,dx[]={-1,-1,1,1,-2,-2,2,2},dy[]={-2,2,-2,2,-1,1,-1,1};
int n,m,a[N][N],id[N][N],tot,h[N*N],cnt,s,t,dis[N*N],vis[N][N],ti;
long long b[N*N];
bool v[N*N];
struct qwe
{int ne,to;
}e[500005];void add(int u,int v)
{cnt++;e[cnt].ne=h[u];e[cnt].to=v;h[u]=cnt;
}inline bool ok(int x,int y)
{return x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]!=2&&vis[x][y]!=ti;
}void dfs(int u,int x,int y,int f)
{vis[x][y]=ti;if((a[x][y]==4||a[x][y]==0)&&!f){add(u,id[x][y]);return;}for(int i=0;i<8;i++)if(ok(x+dx[i],y+dy[i]))dfs(u,x+dx[i],y+dy[i],0);
}int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){scanf("%d",&a[i][j]);id[i][j]=++tot;if(a[i][j]==3)s=id[i][j];if(a[i][j]==4)t=id[i][j];}for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)if(a[i][j]!=2&&a[i][j]!=1) ti++,dfs(id[i][j],i,j,1);queue<int>q;for(int i=1;i<=tot;i++)dis[i]=1e9;dis[s]=0;b[s]=1;v[s]=1;q.push(s);while(!q.empty()){int u=q.front();q.pop();v[u]=0;for(int i=h[u];i;i=e[i].ne){if(dis[e[i].to]>dis[u]+1){dis[e[i].to]=dis[u]+1;b[e[i].to]=b[u];if(!v[e[i].to]){v[e[i].to]=1;q.push(e[i].to);}}else if(dis[e[i].to]==dis[u]+1){b[e[i].to]+=b[u];if(!v[e[i].to]){v[e[i].to]=1;q.push(e[i].to);}}}}if(dis[t]==1e9)puts("-1");elseprintf("%d\n%lld\n",dis[t]-1,b[t]);return 0;
}

转载于:https://www.cnblogs.com/L-Memory/p/9754282.html

P1606 [USACO07FEB]荷叶塘Lilypad Pond(最短路计数)相关推荐

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

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

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

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

  3. [USACO07FEB]Lilypad Pond

    Link 考场上把我送走的一道题 考试时,晃眼一看,这不是裸的最短路计数吗?!赶忙写好了代码,自信地关闭了文件.然后,0分-- 那么回过头来,到底错在哪里?只有新加的荷叶才会被统计,而原来就有的荷叶不 ...

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

  5. Problem : [usaco2007 Feb]Lilypad Pond

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

  6. [ USACO 2007 FEB ] Lilypad Pond (Silver)

    \(\\\) \(Description\) 一张\(N\times M\)的网格,已知起点和终点,其中有一些地方是落脚点,有一些地方是空地,还有一些地方是坏点. 现在要从起点到终点,每次移动走日字\ ...

  7. BZOJ1491: [NOI2007]社交网络(Floyd 最短路计数)

    Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 2343  Solved: 1266 [Submit][Status][Discuss] Descrip ...

  8. 1491. [NOI2007]社交网络【最短路计数】

    Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之间有不同程度的关系.我们将这 ...

  9. 2018.11.05 NOIP模拟 规避(最短路计数)

    传送门 正难则反. 考虑计算两人相遇的方案数. 先正反跑一遍最短路计数. 然后对于一条在最短路上的边(u,v)(u,v)(u,v),如果(dis(s,u)*2<total&&di ...

最新文章

  1. 自动化系导航与控制研究所
  2. JavaScript要点 (二) 使用误区
  3. Java不要放弃之路
  4. 路径搜索 – Dijkstra 算法 (MATLAB实现)
  5. Qt工作笔记-使用qrand与QTime产生随机数
  6. 安卓手机上微信无法打开Https网址的完美解决方案
  7. paip.输入法编程--词频调整原则--发音长度优先
  8. BS与CS的联系与区别【简】
  9. 武汉理工大学刷课,刷在线作业程序,做作业脚本
  10. 07-figma-钢笔工具
  11. 游戏《我的世界》马怎么繁殖?如何驯化?
  12. 基于区块链Baas平台的跨链实践
  13. Oracle随机抽样sample使用说明
  14. 超简单禁止迅雷下载!(分析+方法)
  15. 案例:三个和尚(升级版)
  16. 软件测试:黑盒测试、白盒测试和灰盒测试
  17. python xlwt安装linux_Linux安装Python xlrd、xlwt、xlutils模块
  18. 网页设计项目5穿搭速递_小个子穿搭指南,学以下六种服装的穿搭超时髦,轻松驾驭各种风格...
  19. Android模拟器检测体系梳理,kotlin入门
  20. 银行案例启示:莫把客户投诉当小事

热门文章

  1. Poj 2965 The Pilots Brothers‘ refrigerator
  2. 基于Autocad 2016,与 CASS10 无缝整合的地下管线成图系统
  3. 2020联发科技笔试面试经验
  4. jmeter逻辑控制器之如果(if)控制器实战(二)
  5. Supermap iDesktop处理导入CAD文件存在线型风格显示缺失问题
  6. BUUCTF-刷题记录-3
  7. python 高斯白噪声-python高斯白噪声
  8. HowTo如何制作一个文字冒险游戏-里篇(1)
  9. CentOS6 安装gcc编译器,解决【configure: error: no acceptable C compiler found in $PATH】问题
  10. 哈哈~ 开心死了 厚厚