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,不要输出第二行。


很不错的题目

第一问很容易,每个点向8个方向连边遇到石头连无穷,水连1,叶子连0就可以了,然后直接跑最短路

第二问想到最短路计数,但是有0权边是没法最短路计数的

发现0权边都是叶子贡献的

我们可以先把叶子的边拿bfs跑出来连好,然后就只剩1的边啦,直接跑最短路计数


Code:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N=920;
const int M=200000;
const int inf=0x3f3f3f3f;
int head[N],to[M],Next[M],edge[M],cnt;
void add(int u,int v,int w)
{
to[++cnt]=v,Next[cnt]=head[u],edge[cnt]=w,head[u]=cnt;
}
int n,m,s,t,cost[N];
int cal(int i,int j)
{
return (i-1)*m+j;
}
const int dx[9]={0,-2,-1,1,2,2,1,-1,-2};
const int dy[9]={0,1,2,2,1,-1,-2,-2,-1};
#define P pair <int,int >
int used[N];
void bfs(int x,int y)
{
queue <P> q;
q.push(make_pair(x,y));
int u=cal(x,y);
memset(used,0,sizeof(used));
used[u]=1;
while(!q.empty())
{
int i=q.front().first,j=q.front().second;
q.pop();
for(int k=1;k<=8;k++)
{
int ti=dx[k]+i,tj=dy[k]+j;
if(ti>0&&tj>0&&ti<=n&&tj<=m)
{
int v=cal(ti,tj);
if(used[v]) continue;
used[v]=1;
if(cost[v]==1)
add(u,v,1);//连边边
else if(!cost[v])//走花花
q.push(make_pair(ti,tj));
}
}
}
}
void init()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int id=cal(i,j);
scanf("%d",cost+id);
if(cost[id]==0||cost[id]==1) cost[id]^=1;
else if(cost[id]==2) cost[id]=inf;
else if(cost[id]==3) s=id,cost[id]=1;//特判
else if(cost[id]==4) t=id,cost[id]=1;
}
//0花 1水 2石头
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(cost[cal(i,j)]==1)
bfs(i,j);
}
#define ll long long
int dis[N];ll ans[N];
struct node
{
int id,w;
node(){}
node(int id,int w){this->id=id,this->w=w;}
bool friend operator <(node n1,node n2){return n1.w>n2.w;}
};
std::priority_queue <node> q;
void disj()
{
memset(dis,0x3f,sizeof(dis));
memset(used,0,sizeof(used));
dis[s]=0;
ans[s]=1;
node t(s,0);
q.push(t);
while(!q.empty())
{
int u=q.top().id;
q.pop();
if(used[u]) continue;
used[u]=1;
for(int i=head[u];i;i=Next[i])
{
int v=to[i];
if(dis[v]>dis[u]+edge[i])
{
dis[v]=dis[u]+edge[i];
ans[v]=ans[u];
t=node(v,dis[v]);
q.push(t);
}
else if(dis[v]==dis[u]+edge[i])
ans[v]+=ans[u];
}
}
}
int main()
{
init();
disj();
if(dis[t]==inf) printf("-1\n");
else printf("%d\n%lld\n",dis[t]-1,ans[t]);
return 0;
}

2018.9.5

洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告的更多相关文章

  1. [洛谷P1606] [USACO07FEB] 荷叶塘Lilypad Pond

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

  2. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond【spfa】

    和bzoj同名题不一样! 起点和水点向花费一个荷花能到的第一个点连一条边权为1的有向边,然后跑计数spfa即可 #include<iostream> #include<cstdio& ...

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

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

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

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

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

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

  6. Dfs+Spfa【p1606】[USACO07FEB]荷叶塘Lilypad Pond

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

  7. LuoguP1606 [USACO07FEB]荷叶塘Lilypad Pond 【最短路】By cellur925

    最短路好题!] 参考资料:学长 https://blog.csdn.net/TSOI_Vergil/article/details/52975779 学长太强了!!!%%% 题目传送门 ======= ...

  8. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...

  9. 【洛谷】CYJian的水题大赛 解题报告

    点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...

随机推荐

  1. Struts2基础学习2

    Struts2基础学习2 项目结构,测试页面与实体类 <%@ page language="java" contentType="text/html; charse ...

  2. cmd中编译java脚本 (2013-05-02-bd 写的日志迁移

    此前提是已经搭建好了jdk的编译环境! 先写一个java脚本如:建立一个HelloWord.java //public : 表示此类是公共的 一个java文件中只能有一个public类 //class ...

  3. Nginx安装,目录结构与配置文件详解

    1.Nginx简介 Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设 ...

  4. Nginx模块详解

    Nginx模块介绍 核心模块:core module 标准模块: HTTP modules: Standard HTTP modules Optional HTTP modules Mail modu ...

  5. 大数据江湖之即席查询与分析(下篇)--手把手教你搭建即席查询与分析Demo

    上篇小弟分享了几个“即席查询与分析”的典型案例,引起了不少共鸣,好多小伙伴迫不及待地追问我们:说好的“手把手教你搭建即席查询与分析Demo”啥时候能出?说到就得做到,差啥不能差人品,本篇只分享技术干货 ...

  6. async/await 实现协程

    2. 基本了解 在了解异步协程之前,我们首先得了解一些基础概念,如阻塞和非阻塞.同步和异步.多进程和协程. 2.1 阻塞 阻塞状态指程序未得到所需计算资源时被挂起的状态.程序在等待某个操作完成期间,自 ...

  7. build dynamic libraries for iOS and load them at runtime

    编译了libmt.dylib, 和 test 程序调用,均正常.在xcode中显示调用正常,隐式调用则出现问题. 提示 dyld: Library not loaded. 即使存在在/usr/lib/ ...

  8. 编译器错误消息: CS1617: 选项“6”对 /langversion 无效

    编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS1617: 选项“6”对 /langversion 无效:必须是 ...

  9. C#正则表达式引发的CPU跑高问题以及解决方法

    3月23日(周日)下午16:30左右,博客园主站负载均衡中的2台Web服务器CPU玩起了爬楼梯的游戏(见上图),一直爬到了接近100%.发现这个状况后,我们立即将这2台阿里云临时磁盘云服务器从负载均衡 ...

  10. iOS系统信息集合

    在项目开发的时候,经常需要用到某些系统信息,比如手机型号(5s,6,6p), 操作系统版本(8.0 or 9.3), 当前网络类型(3/4g, wifi)等信息. 有了这些信息, 可以在出了某些bug ...