直接bfs,在过程中更新方案数即可

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=55,inf=1e9,dx[]={1,1,-1,-1,2,2,-2,-2},dy[]={2,-2,2,-2,1,-1,1,-1};
int n,m,a[N][N],b[N][N],dis[N][N];
long long f[N][N];
bool v[N][N];
struct qwe
{
int x,y;
qwe(int X=0,int Y=0)
{
x=X,y=Y;
}
}s,t;
queue<qwe>q;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
b[i][j]=dis[i][j]=inf;
if(a[i][j]==3)
s=qwe(i,j);
if(a[i][j]==4)
t=qwe(i,j);
}
q.push(s);
v[s.x][s.y]=1;
b[s.x][s.y]=dis[s.x][s.y]=0;
f[s.x][s.y]=1;
while(!q.empty())
{
qwe u=q.front();
q.pop();
v[u.x][u.y]=0;
for(int k=0;k<8;k++)
{
int x=u.x+dx[k],y=u.y+dy[k];
if(x<1||y<1||x>n||y>m||a[x][y]==2)
continue;
int nw=b[u.x][u.y]+(a[x][y]==0);
if(nw<b[x][y])
{
b[x][y]=nw;
dis[x][y]=dis[u.x][u.y]+1;
f[x][y]=f[u.x][u.y];
if(!v[x][y])
{
v[x][y]=1;
q.push(qwe(x,y));
}
}
else if(nw==b[x][y])
{
if(dis[u.x][u.y]+1<dis[x][y])
{
dis[x][y]=dis[u.x][u.y]+1;
f[x][y]=f[u.x][u.y];
if(!v[x][y])
{
v[x][y]=1;
q.push(qwe(x,y));
}
}
else if(dis[u.x][u.y]+1==dis[x][y])
{
f[x][y]+=f[u.x][u.y];
if(!v[x][y])
{
v[x][y]=1;
q.push(qwe(x,y));
}
}
}
}
}
if(b[t.x][t.y]==inf)
puts("-1");
else
printf("%d\n%d\n%lld\n",b[t.x][t.y],dis[t.x][t.y],f[t.x][t.y]);
return 0;
}

bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】的更多相关文章

  1. BZOJ 1632: [Usaco2007 Feb]Lilypad Pond

    题目 1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 390  Solved: 109[ ...

  2. 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...

  3. BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...

  4. 1632: [Usaco2007 Feb]Lilypad Pond

    1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 404  Solved: 118[Sub ...

  5. bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】

    正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案 #include<iostream> #include<cstdio> #include<queue&g ...

  6. bzoj1632 [Usaco2007 Feb]Lilypad Pond

    Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼.这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方 ...

  7. BZOJ1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘

    一傻逼题调了两天.. n<=30 * m<=30的地图,0表示可以放平台,1表示本来有平台,2表示不能走,3起点4终点,走路方式为象棋的日字,求:从起点走到终点,至少要放多少平台,以及放平 ...

  8. BZOJ1632: [Usaco2007 Feb]Lilypad Pond SPFA+最短路计数

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

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

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

随机推荐

  1. django学习之- session

    session和cookie关系:session依赖于cookie基于cookie做用户验证时,敏感信息不适合放在cookie中原理:cookie定义:保存在用户游览器端的键值对session定义:保 ...

  2. HDU 1669 二分图多重匹配+二分

    Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  3. Intersecting Lines--POJ1269(判断两条直线的关系 && 求两条直线的交点)

    http://poj.org/problem?id=1269 我今天才知道原来标准的浮点输出用%.2f   并不是%.2lf  所以wa了好几次 题目大意:   就给你两个线段 然后求这两个线段所在的 ...

  4. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  5. CPU 天梯图

  6. centos、mac的grafana安装和简单使用

    1.安装: 参考官方文档安装说明:https://grafana.com/grafana/download Redhat & Centos(64 Bit): wget https://s3-u ...

  7. java的计时:毫秒、纳秒

    System.currentTimeMillis()获取毫秒值,但是其精度依赖操作系统 想实现较为精确的毫秒,可以采用 System.nanoTime()/1000000L System.nanoTi ...

  8. [React] Use the Fragment Short Syntax in Create React App 2.0

    create-react-app version 2.0 added a lot of new features. One of the new features is upgrading to Ba ...

  9. Solidworks drwdot文件如何打开,如何制作Solidworks工程图模板

    1 直接把这个文件拖放进Solidworks窗口   2 文件-保存图纸格式,另存为模板(slddrt文件)   3 搜索"Solidworks工程图如何使用,替换图纸格式模板文件.doc& ...

  10. Linux的基本优化

    归结成口诀: 一清.一精.一增.两优.四设.七其他 一清: 定时清理日志/var/spool/clientsqueue 一精: 精简开机启动服务 一增: 增大文件描述符 两优: linux内核参数的优 ...