【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632
我简直是个sb。。。
。。。
bfs都不会写。。
算方案还用2个bfs!
都不会整合到一个!
然后赤裸裸的wa了。
然后对拍。。。
噗
果然错的地方很。。。
。。
然后貌似自己想改bfs,只需一个就行了。。
而且方案也改对了????
然后不知哪里写挫了。。。然后样例过不了了。。
然后无奈看别人题解了。。。
QAQ
噗,方案和我想的一样,但是。。。。。。。。。。。。。。。我忘记了个东西,,标记啊。。
噗。。我尽然没标记。。。怪不得。。
吐槽完毕。。
这个bfs的时候更新很简单的。
自己看看就懂了的。。只要记住要标记!!!!要不然答案会大得离奇
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j] << '\t'; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=35, Q=10000, dx[]={-1, -2, -2, -1, 1, 2, 2, 1}, dy[]={-2, -1, 1, 2, 2, 1, -1, -2};
int mp[N][N], f[N][N], d[N][N], vis[N][N], n, m, X, Y, XX, YY, front, tail;
struct dat { int x, y; }q[Q];
long long p[N][N]; void bfs1() {
front=tail=0;
q[tail].x=X, q[tail].y=Y; ++tail;
CC(f, 0x3f); CC(d, 0x3f); f[X][Y]=d[X][Y]=0; vis[X][Y]=p[X][Y]=1;
while(front!=tail) {
dat &t=q[front++]; if(front==Q) front=0;
int x=t.x, y=t.y; vis[x][y]=0;
if(x==XX && y==YY) continue;
rep(i, 8) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>m || mp[fx][fy]==2) continue;
dat &t2=q[tail];
int add=mp[fx][fy]==0;
if(f[fx][fy]>f[x][y]+add) {
f[fx][fy]=f[x][y]+add;
d[fx][fy]=d[x][y]+1;
p[fx][fy]=p[x][y];
if(!vis[fx][fy]) t2.x=fx, t2.y=fy, vis[fx][fy]=1; if(++tail==Q) tail=0;
}
else if(f[fx][fy]==f[x][y]+add && d[fx][fy]>d[x][y]+1) {
d[fx][fy]=d[x][y]+1;
p[fx][fy]=p[x][y];
if(!vis[fx][fy]) t2.x=fx, t2.y=fy, vis[fx][fy]=1; if(++tail==Q) tail=0;
}
else if(f[fx][fy]==f[x][y]+add && d[fx][fy]==d[x][y]+1) {
p[fx][fy]+=p[x][y];
if(!vis[fx][fy]) t2.x=fx, t2.y=fy, vis[fx][fy]=1; if(++tail==Q) tail=0;
}
}
}
}
int main() {
read(n); read(m);
for1(i, 1, n) for1(j, 1, m) {
int t=getint(); mp[i][j]=t;
if(t==3) X=i, Y=j; else if(t==4) XX=i, YY=j;
}
bfs1();
if(f[XX][YY]==0x3f3f3f3f) puts("-1");
else printf("%d\n%d\n%lld\n", f[XX][YY], d[XX][YY], p[XX][YY]);
return 0;
}
【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)的更多相关文章
- 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)
http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...
- 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...
- 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2015 这种水题真没啥好说的.. #include <cstdio> #include & ...
- 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)
http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...
- 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 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...
- 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序
[算法]数学置换 [题意]给定n个数,要求通过若干次交换两个数的操作得到排序后的状态,每次交换代价为两数之和,求最小代价. [题解] 考虑置换的定义:置换就是把n个数做一个全排列. 从原数组到排序数组 ...
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
随机推荐
- Android笔记——UI开发
概述: 布局(Layout)的概念是针对Activity的,Activity就是布满整个Android设备的窗体或者悬浮于其它窗体上的交互界面.在一个应用程序中通常由多个Activity构成.每一个须 ...
- 【Firefly API文档】—— Package Netconnect
http://bbs.gameres.com/forum.php?mod=viewthread&tid=219655 package netconnect 该包中包含的服务端与客户端通信的一些 ...
- python网站
https://github.com/search?utf8=%E2%9C%93&q=python+sockethttps://github.com/search?p=2&q=pyth ...
- python小写转大写金额
python小写转大写金额 摘自:http://shine-it.net/index.php?topic=14575.0 def _rmb_upper(self, value): "&quo ...
- 基于Netty的RPC简易实现
代码地址如下:http://www.demodashi.com/demo/13448.html 可以给你提供思路 也可以让你学到Netty相关的知识 当然,这只是一种实现方式 需求 看下图,其实这个项 ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
- Python-PyQt4学习资料汇总
摘自:http://www.cnblogs.com/coderzh/archive/2009/06/28/1512654.html 官方文档: http://pyqt.sourceforge.net/ ...
- 解决Jquery Ajax提交 服务器端接收中文乱码问题
看到有朋友说到用post提交方式解决,我指定了methord="post",仍然解决不了, 说一下解决办法,客户端进行编码,服务器端解码, 客户端:var where = esca ...
- 将XML格式的字符串封装成DOM对象
在java端将字符串转化为xml对象可以使用DocumentHelper.parseText(xmlReturn).getRootElement(); 在js中同样有方法可以将字符串转化为xml对象, ...
- pandas数据处理基础——基础加减乘除的运算规则
上周公司对所有员工封闭培训了一个星期,期间没收手机,基本上博客的更新都停止了,尽管培训时间不长,但还是有些收获,不仅来自于培训讲师的,更多的是发现自己与别人的不足,一个优秀的人不仅仅是自己专业那块的精 ...