[BZOJ 1698] 荷叶池塘
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1698
[算法]
最短路
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 35
const int dx[] = {,-,,-,,-,,-};
const int dy[] = {,,-,-,,,-,-}; struct edge
{
int to,nxt;
} e[MAXN * MAXN * MAXN * MAXN]; int n,m,sx,sy,tx,ty,tot;
int head[MAXN * MAXN];
int mp[MAXN][MAXN],dist[MAXN * MAXN];
long long cnt[MAXN * MAXN];
bool visited[MAXN][MAXN]; inline bool valid(int x,int y)
{
return x >= && x <= n && y >= && y <= m;
}
inline int id(int x,int y)
{
return (x - ) * m + y;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline void dfs(int nowx,int nowy,int x,int y)
{
for (int i = ; i < ; i++)
{
int nx = nowx + dx[i] , ny = nowy + dy[i];
if (valid(nx,ny) && !visited[nx][ny] && mp[nx][ny] != )
{
visited[nx][ny] = true;
if (!mp[nx][ny] || mp[nx][ny] == ) addedge(id(x,y),id(nx,ny));
else dfs(nx,ny,x,y);
}
}
}
inline void spfa(int x,int y)
{
queue< int > q;
static bool inq[MAXN * MAXN];
memset(inq,false,sizeof(inq));
memset(dist,0x3f,sizeof(dist));
dist[id(x,y)] = ;
cnt[id(x,y)] = ;
q.push(id(x,y));
while (!q.empty())
{
int cur = q.front();
q.pop();
if (cur == id(tx,ty)) continue;
inq[cur] = false;
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to;
if (dist[cur] + < dist[v])
{
dist[v] = dist[cur] + ;
cnt[v] = cnt[cur];
if (!inq[v])
{
inq[v] = true;
q.push(v);
}
} else if (dist[cur] + == dist[v]) cnt[v] += cnt[cur];
}
cnt[cur] = ;
}
} int main()
{ scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
scanf("%d",&mp[i][j]);
if (mp[i][j] == )
{
sx = i;
sy = j;
}
if (mp[i][j] == )
{
tx = i;
ty = j;
}
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
memset(visited,false,sizeof(visited));
visited[i][j] = true;
if (mp[i][j] != )
dfs(i,j,i,j);
}
}
spfa(sx,sy);
if (dist[id(tx,ty)] > 1e8)
{
printf("-1\n");
return ;
}
printf("%d\n%lld\n",dist[id(tx,ty)] - ,cnt[id(tx,ty)]); return ;
}
[BZOJ 1698] 荷叶池塘的更多相关文章
- 「BZOJ 1698」「USACO 2007 Feb」Lilypad Pond 荷叶池塘「最短路」
题解 从一个点P可以跳到另一个点Q,如果Q是水这条边就是1,如果Q是荷叶这条边权值是0.可以跑最短路并计数 问题是边权为0的最短路计数没有意义(只是荷叶的跳法不同),所以我们两个能通过荷叶间接连通的点 ...
- BZOJ1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘
一傻逼题调了两天.. n<=30 * m<=30的地图,0表示可以放平台,1表示本来有平台,2表示不能走,3起点4终点,走路方式为象棋的日字,求:从起点走到终点,至少要放多少平台,以及放平 ...
- bzoj usaco 金组水题题解(2.5)
bzoj 2197: [Usaco2011 Mar]Tree Decoration 树形dp..f[i]表示处理完以i为根的子树的最小时间. 因为一个点上可以挂无数个,所以在点i上挂东西的单位花费就是 ...
- bzoj Usaco补完计划(优先级 Gold>Silver>资格赛)
听说KPM初二暑假就补完了啊%%% 先刷Gold再刷Silver(因为目测没那么多时间刷Silver,方便以后TJ2333(雾 按AC数降序刷 ---------------------------- ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘
题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MB Description 农夫 ...
- 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond
P1606 [USACO07FEB]荷叶塘Lilypad Pond 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令 ...
- 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告
P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...
随机推荐
- Vue2.0 —生命周期和钩子函数
vue生命周期简介 咱们从上图可以很明显的看出现在vue2.0都包括了哪些生命周期的函数了. 生命周期探究 对于执行顺序和什么时候执行,看上面两个图基本有个了解了.下面我们将结合代码去看看钩子函数的执 ...
- 【转载】jQuery.extend 函数详解
转载自:http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729563.html jQuery.extend 函数详解 JQuery的e ...
- 软件工程师需要了解的网络知识:从铜线到HTTP(一)—— 前言
转自:https://lvwenhan.com/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/485.html?hmsr=toutiao.io&utm_medium ...
- 洛谷 4172 [WC2006]水管局长
[题解] 我们把操作倒过来做,就变成了加边而不是删边.于是用LCT维护动态加边的最小生成树就好了.同样要注意把边权变为点权. #include<cstdio> #include<al ...
- textarea 高度调整
textarea 高度调整 通过 rows 属性调整 高度
- hdu 5023 线段树延迟更新+状态压缩
/* 线段树延迟更新+状态压缩 */ #include<stdio.h> #define N 1100000 struct node { int x,y,yanchi,sum; }a[N* ...
- 小a和uim之大逃离(洛谷 1373)
题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...
- 试来试去,WIN下最简单的WIN API开发工具,Pelles C就好啦
昨晚试过N个,不是太大,就是不容易和WIN API集成. 今早一试就灵了个.... Pelles C. Pelles C是一款windows下的C IDE,支持调试,且为免费.它有一个高效率的链接器, ...
- T1082 线段树练习3 codevs
http://codevs.cn/problem/1082/ 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 ...
- MYSQL中有关表的简单操作
#创建表 CREATE TABLE table02( tid INT, tname VARCHAR(20)); #查看所有表SHOW TABLES; #查看表的结构DESC table01; #修改表 ...