POJ3271/BZOJ 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘
来源:互联网 发布:sql server 收缩日志 编辑:程序博客网 时间:2024/05/17 03:41
这道题“真的”好玩 手动笑cry
一开始 你可能会想到直接bfs 但是 因为原本荷叶的存在 你会很烦
第一问还好说 统计方案数就GG
然后 你可能会想到把这些荷叶缩成连通块。。。
可是!!!! 你又会发现有问题
比如这种
0 0 y 0 0
1 0 0 0 1
0 0 x 0 0
x和y都是清水 从x去到y 可以经过左边的联通块去 可以经过右边的联通块去 答案有两种但其实就只有x,y放荷叶一种
是不是很TM爽!
最后 我来告诉你这个世界的真相
因为它是统计放荷叶的方案数啊
我们可以去避免遇到那些原本存在的嘛 我们就预处理出每个起点以及水位置的花费1个荷叶能到的地方 然后建边 跑spfa就可以了~
为什么呢...因为这样就肯定没有环了 而且上面那种情况其实就是从x直接到y 就不怕算多情况了
觉得这个代码还是挺好看的(毕竟挺快的)
#include<cmath>#include<queue>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#define me(a,x) memset(a,x,sizeof a)using namespace std;typedef long long LL;const int N=31,inf=1e9;const int dx[8]={2,2,-2,-2,1,-1,1,-1};const int dy[8]={-1,1,-1,1,2,2,-2,-2};inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f;}struct node{int y,next;}a[500010]; int len,first[N*N];void ins(int x,int y){ a[++len].y=y,a[len].next=first[x],first[x]=len;}int mp[N][N],d[N*N],p[N][N],n,m; LL f[N*N];bool b[N][N];void dfs(int o,int x,int y){ b[x][y]=1; for(int k=0;k<8;k++) { int xx=x+dx[k],yy=y+dy[k]; if(xx<1 || yy<1 || xx>n || yy>m || b[xx][yy])continue; if(mp[xx][yy]==1)dfs(o,xx,yy); else b[xx][yy]=1,ins(o,p[xx][yy]); }}queue<int>q; bool v[N*N];int main(){ int i,j,x,y,k,st,ed; n=read(),m=read(); for(i=1;i<=n;i++)for(j=1;j<=m;j++)p[i][j]=(i-1)*m+j; for(i=1;i<=n;i++)for(j=1;j<=m;j++) { mp[i][j]=read(); if(mp[i][j]==3)st=p[i][j]; if(mp[i][j]==4)ed=p[i][j]; } for(i=1;i<=n;i++)for(j=1;j<=m;j++) if(mp[i][j]==0||mp[i][j]==3)me(b,0),dfs(p[i][j],i,j); me(d,63); d[st]=0,v[st]=f[st]=1; q.push(st); while(!q.empty()) { x=q.front(); q.pop(); v[x]=0; for(k=first[x];k;k=a[k].next) { y=a[k].y; if(d[y]>d[x]+1) { d[y]=d[x]+1,f[y]=f[x]; if(!v[y]){v[y]=1; q.push(y);} } else if(d[y]==d[x]+1) f[y]+=f[x]; } } if(d[ed]<inf)printf("%d\n%lld\n",d[ed]-1,f[ed]); else printf("-1\n"); return 0;}
2 0
- POJ3271/BZOJ 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘
- bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘 spfa+bfs
- bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘(BFS)
- 【bzoj1698】[Usaco2007 Feb]Lilypad Pond 荷叶池塘
- BZOJ 1698 [Usaco2007 Feb]Lilypad Pond 荷叶池塘 BFS+最短路
- bzoj1698 [Usaco2007 Feb]Lilypad Pond 荷叶池塘 [BFS]
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond bfs
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond SPFA
- BZOJ 1698 (USACO 2007 Feb)Liypad Pond (黄金)荷叶池塘
- bzoj1632 [Usaco2007 Feb]Lilypad Pond
- 1632: [Usaco2007 Feb]Lilypad Pond
- 1632: [Usaco2007 Feb]Lilypad Pond
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond (spfa)
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond(BFS)
- poj3271 Lilypad Pond bfs
- [BZOJ1632][Usaco2007 Feb]Lilypad Pond(spfa)
- [BZOJ1632][Usaco2007 Feb]Lilypad Pond(spfa)
- BFS妙题1632: [Usaco2007 Feb]Lilypad Pond
- android项目中values中ids.xml的作用
- linux初学
- Dashboard部署
- centos 6.5 安装weixin
- Linux 基本操作
- POJ3271/BZOJ 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘
- 对话框界面风格的问题
- java中的==、equals()、hashCode()源码分析
- android 分页数据 LinkedHashSet及ArrayList两种去重方法
- mybatis笔记---03
- Java 泛型类实现继承
- java:使用匿名类直接new接口
- 初学者对const的认识(常类型变量)
- Douglas-Peucker算法 抽稀曲线上的点