https://www.luogu.org/problem/show?pid=1606

题目描述

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

直接在30*30网格上跑spfa会TLE

因为计算荷叶放置方案总数时,如果dis相同也要入队,

导致刚出队的接着入队,TLE

AC做法:

预处理出花费1个荷叶能到达的地方再跑spfa

#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 810001
int n,m,qx,qy;
int dis[N];
long long sum[N];
int front[],nxt[N],to[N],tot;
int have[][];
bool vis[][];
int dx[]={-,-,,,,,-,-};
int dy[]={,,,,-,-,-,-};
queue<int>q;
int turn(int i,int j)
{
return (i-)*m+j;
}
void add(int u,int t)
{
to[++tot]=t; nxt[tot]=front[u]; front[u]=tot;
}
void dfs(int x,int y)
{
vis[x][y]=true;
if((x!=qx || y!=qy) && (!have[x][y] || have[x][y]==))
{
add(turn(qx,qy),turn(x,y));
return;
}
for(int i=;i<;i++)
{
if(x+dx[i]<= || x+dx[i]>n || y+dy[i]<= || y+dy[i]>m || have[x+dx[i]][y+dy[i]]== || have[x+dx[i]][y+dy[i]]==) continue;
if(!vis[x+dx[i]][y+dy[i]]) dfs(x+dx[i],y+dy[i]);
}
}
int main()
{
int sx,sy,tx,ty,x;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&have[i][j]);
if(have[i][j]==) sx=i,sy=j;
else if(have[i][j]==) tx=i,ty=j;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(have[i][j]!= && have[i][j]!=)
{
memset(vis,false,sizeof(vis));
qx=i,qy=j,dfs(i,j);
}
q.push(turn(sx,sy));
sum[turn(sx,sy)]=;
int now;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=front[now];i;i=nxt[i])
{
if(!dis[to[i]])
{
dis[to[i]]=dis[now]+;
sum[to[i]]=sum[now];
q.push(to[i]);
}
else if(dis[to[i]]==dis[now]+) sum[to[i]]+=sum[now];
}
}
if(dis[turn(tx,ty)]==) { printf("-1"); return ;}
printf("%d\n%lld",dis[turn(tx,ty)]-,sum[turn(tx,ty)]);
}

两个错误:

1、预处理的时候为了放置自己不向自己连边,

(x!=qx || y!=qy),中间用了&& ,导致同行不同列的也不能连边

2、空间 一个点8方向扩展,但不是只能连一条边,经过荷叶可以连更多的边

[USACO07FEB] Lilypad Pond的更多相关文章

  1. 【笔记】P1606 [USACO07FEB]Lilypad Pond G 及相关

    题目传送门 建图 首先,根据题目,可以判断出这是一道最短路计数问题. 但是要跑最短路,首先要用他给的信息建图,这是非常关键的一步. 根据题意,我们可以想出以下建图规则: 起点或是一个空白处可以花费 \ ...

  2. bzoj1698 / P1606 [USACO07FEB]白银莲花池Lilypad Pond

    P1606 [USACO07FEB]白银莲花池Lilypad Pond 转化为最短路求解 放置莲花的方法如果直接算会有重复情况. 于是我们可以先预处理和已有莲花之间直接互相可达的点,将它们连边(对,忽 ...

  3. 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond

    P1606 [USACO07FEB]荷叶塘Lilypad Pond 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令 ...

  4. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告

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

  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

    题目 1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 390  Solved: 109[ ...

  7. 1632: [Usaco2007 Feb]Lilypad Pond

    1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 404  Solved: 118[Sub ...

  8. 【luogu P1606 [USACO07FEB]荷叶塘Lilypad Pond】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1606 这个题..第一问很好想,但是第二问,如果要跑最短路计数的话,零边权的花怎么办? 不如这样想,如果这个点 ...

  9. Luogu 1606 [USACO07FEB]白银莲花池Lilypad Pond

    感觉应当挺简单的,但是弄了好久……菜死了 如果不考虑那些为$1$的点,直接跑个最短路计数就好了,但是我们现在有一些边可以不用付出代价,那么只要在连边的时候先预处理搜一下就好了. 原来的想法是拆点,但是 ...

随机推荐

  1. c# apache服务器请求得到数据(初级)

    1.代码: string data = new WebClient().DownloadString("http://localhost:81/123.txt");

  2. C++:构造函数1——普通构造函数

    前言:构造函数是C+中很重要的一个概念,这里对其知识进行一个简单的总结 一.构造函数的定义 1.类中的构造函数名与类名必须相同 2.构造函数没有函数的返回类值型说明符 [特别注意]: a.构造函数的返 ...

  3. Python:模块学习——os模块

    os模块提供了多个访问操作系统服务的功能 os模块中一些重要的函数和变量 os.name 显示当前使用平台 os.getcwd() 显示当前Python脚本工作路径 os.listdir('dirna ...

  4. lintcode-205-区间最小数

    205-区间最小数 给定一个整数数组(下标由 0 到 n-1,其中 n 表示数组的规模),以及一个查询列表.每一个查询列表有两个整数 [start, end]. 对于每个查询,计算出数组中从下标 st ...

  5. DB2的编目

    D在DB2数据库中,编目(catalog)这个单词很难理解,我自己当初在学习DB2的时候也常常被这个编目搞的很不明白,直到现在我个人也感觉到DB2中编目(catalog)这个术语用的不是很好,具体来说 ...

  6. linux桌面使用鼠标中间健粘帖

    使用linux桌面很久了,一直习惯鼠标左键选中,右健弹出菜单复制粘帖. 没想到linux使用鼠标中间健粘帖,很方便. 参考:Linux鼠标中键复制粘贴之谜[Felix蛋疼科普贴] 用鼠标左键单击待复制 ...

  7. 定时器应用-点击按钮,div向右移动

    需求是点击button,div就一直往右移动,给个条件left=800px就停止移动,通过定时器来控制. 代码如下: <!DOCTYPE html> <html> <he ...

  8. CentOS安装crontab及使用方法(转)

    CentOS安装crontab及使用方法(转)    安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum ins ...

  9. flink ha zk集群迁移实践

    flink为了保证线上作业的可用性,提供了ha机制,如果发现线上作业失败,则通过ha中存储的信息来实现作业的重新拉起. 我们在flink的线上环境使用了zk为flink的ha提供服务,但在初期,由于资 ...

  10. mysql中变量

    mysql中的变量: mysql中,有两种变量形式: 普通变量: 不带“@”符号: 定义形式: declare  变量名  类型名   [default  默认值]: //普通变量必须先这样定义 赋值 ...