BZOJ1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘
一傻逼题调了两天。。
n<=30 * m<=30的地图,0表示可以放平台,1表示本来有平台,2表示不能走,3起点4终点,走路方式为象棋的日字,求:从起点走到终点,至少要放多少平台,以及放平台的方案数,无解-1。
方法一:其实能走直接平台的就可以直接走来走去,也就是算一个联通块。类似于tarjan,先把一大块缩成一点,然后连边走最短路。
错误!存在边权为0的边,会导致统计方案出现重复。比如:
圆圈走到三角形,直接走和绕一圈是一样的,但算了两次。
方法二:把0边去掉就行了。由于数据小,开个数组[a][b][c][d]表示a,b到c,d是否能通过填一块到达,这个数组只需对每个点做一次搜索就能出来。最后就是最短路啦。
不过边权为1,谁最短路会写迪杰呢?肯定bfs啦!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
//#include<iostream>
using namespace std; int n,m;
int a[][],can[][][][];
#define maxn 1011
#define maxm 10011
struct Edge{int tx,ty,next;}edge[maxn*maxn];int first[][],le=;
#define LL long long
void in(int sx,int sy,int tx,int ty)
{
Edge &e=edge[le];
e.tx=tx;e.ty=ty;
e.next=first[sx][sy];
first[sx][sy]=le++;
}
int nx,ny;
const int dx[]={,,,,-,-,-,-},dy[]={,-,,-,,-,,-};
bool vis[][];
void dfs(int x,int y)
{
vis[x][y]=;
for (int i=;i<;i++)
{
const int xx=x+dx[i],yy=y+dy[i];
if (xx< || xx>n || yy< || yy>m || can[nx][ny][xx][yy]
|| a[xx][yy]== || vis[xx][yy]) continue;
if (a[xx][yy]== || a[xx][yy]==)
{
can[nx][ny][xx][yy]=;
continue;
}
dfs(xx,yy);
}
}
const int inf=0x3f3f3f3f;
int dis[][];LL way[][];
struct qnode{int x,y;}q[maxn];int head,tail;
void bfs(int sx,int sy)
{
head=;tail=;q[].x=sx;q[].y=sy;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
dis[i][j]=inf;
dis[sx][sy]=;way[sx][sy]=;
while (head!=tail)
{
const int nx=q[head].x,ny=q[head++].y;
for (int i=first[nx][ny];i;i=edge[i].next)
{
const Edge &e=edge[i];
if (dis[e.tx][e.ty]>dis[nx][ny]+)
{
dis[e.tx][e.ty]=dis[nx][ny]+;
way[e.tx][e.ty]=way[nx][ny];
q[tail].x=e.tx,q[tail++].y=e.ty;
}
else if (dis[e.tx][e.ty]==dis[nx][ny]+)
way[e.tx][e.ty]+=way[nx][ny];
}
}
}
int main()
{
scanf("%d%d",&n,&m);
int sx,sy,tx,ty;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
if (a[i][j]==) sx=i,sy=j;
if (a[i][j]==) tx=i,ty=j;
}
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
{
memset(can[i][j],,sizeof(can[i][j]));
memset(vis,,sizeof(vis));
if (a[i][j]==) continue;
nx=i;ny=j;
dfs(i,j);
}
memset(first,,sizeof(first));
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
for (int k=;k<=n;k++)
for (int l=;l<=m;l++)
{
if (can[i][j][k][l])
{
in(i,j,k,l);
}
}
bfs(sx,sy);
if (dis[tx][ty]==inf) puts("-1");
else printf("%d\n%lld\n",dis[tx][ty]-,way[tx][ty]);
return ;
}
BZOJ1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘的更多相关文章
- BZOJ 1632: [Usaco2007 Feb]Lilypad Pond
题目 1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 390 Solved: 109[ ...
- 1632: [Usaco2007 Feb]Lilypad Pond
1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 404 Solved: 118[Sub ...
- 「BZOJ 1698」「USACO 2007 Feb」Lilypad Pond 荷叶池塘「最短路」
题解 从一个点P可以跳到另一个点Q,如果Q是水这条边就是1,如果Q是荷叶这条边权值是0.可以跑最短路并计数 问题是边权为0的最短路计数没有意义(只是荷叶的跳法不同),所以我们两个能通过荷叶间接连通的点 ...
- bzoj1632 [Usaco2007 Feb]Lilypad Pond
Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼.这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方 ...
- BZOJ1632: [Usaco2007 Feb]Lilypad Pond SPFA+最短路计数
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...
- 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】
直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...
- [ USACO 2007 FEB ] Lilypad Pond (Silver)
\(\\\) \(Description\) 一张\(N\times M\)的网格,已知起点和终点,其中有一些地方是落脚点,有一些地方是空地,还有一些地方是坏点. 现在要从起点到终点,每次移动走日字\ ...
随机推荐
- 【学习笔记】深入理解js原型和闭包(15)——闭包
前面提到的上下文环境和作用域的知识,除了了解这些知识之外,还是理解闭包的基础. 至于“闭包”这个词的概念的文字描述,确实不好解释,我看过很多遍,但是现在还是记不住. 但是你只需要知道应用的两种情况即可 ...
- Java JDK装配置
1- 介绍 本文章介绍JAVA开发环境安装是基于: Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...
- Django展示第一个网页
展示一个网页需要三部分组成: urls.py -- 指定网址与对应的视图 views.py -- 创建试图以及指定对应的模板 template/*.html -- 对应的模板 一.urls.py ur ...
- MVC之在实例中的应用
MVC模式在Java Web应用程序中的实例分析 1. 结合六个基本质量属性 1)可用性 2)可修改性 3)性能 4)安全性 5)可测试性 6)易用性 2. 分析具体功能模块的MVC设计实现(例如登录 ...
- QT 学习笔记概述
以下笔记为在看书和实践的过程中的部分记录总结: 0. 窗口布局 1) 支持绝对布局和布局管理器布局; 2) 绝对布局不够灵活.无法自动调整大小,需要手动编写代码调整: 3) 布局管理器管理布局比较灵活 ...
- 在Eclipse中设置自动补全
在Eclipse中菜单中,Window->Preferences->Java->Editor->Content Assist中的Auto activation triggers ...
- stay hungry stay foolish.
I am honored to be with you today at your commencement from one of the finest universities in the wo ...
- CPP-基础:模板
// template.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> #include &l ...
- mysql的sql语句练习的2个网址
sql语句练习: https://blog.csdn.net/mrbcy/article/details/68965271 完成. https://blog.csdn.net/flycat296/ar ...
- 配置maven报错 the java_home environment variable is not defined correctly ......
the java_home environment variable is not defined correctly This environment variable is needed to r ...