poj3271 Lilypad Pond bfs

 处理出每个点一步能到的点再统计路径。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int dx[9]={0,-2,-2,-1,-1,1,1,2,2};
int dy[9]={0,-1,1,-2,2,-2,2,-1,1};
const int maxn=30+9;
int dist[maxn][maxn],a[maxn][maxn];
int n,m;
bool d[maxn][maxn][maxn][maxn];
long long ans[maxn][maxn];
bool text[maxn][maxn];
bool ok(int x,int y)
{
    if(x>0&&x<=n&&y>0&&y<=m)
        return  true;
    return false;
}
void bfs2(int x,int y)
{
    queue<int>qx,qy;
    qx.push(x);
    qy.push(y);
    memset(dist,0x3f3f3f3f,sizeof(dist));
    memset(ans,0,sizeof(ans));
    dist[x][y]=0;
    ans[x][y]=1;
    while(!qx.empty())
    {
        int tx=qx.front();
        int ty=qy.front();
        qx.pop();qy.pop();
        if(a[tx][ty]==2) continue;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(d[tx][ty][i][j])
                {
                    if(dist[i][j]==dist[tx][ty]+1)
                    ans[i][j]+=ans[tx][ty];
                    else if(dist[i][j]>dist[tx][ty]+1)
                    {
                        dist[i][j]=dist[tx][ty]+1;
                        ans[i][j]=ans[tx][ty];
                        qx.push(i);
                        qy.push(j);
                    }
                }
            }
        }
    }
}
void bfs(int x,int y)
{
    memset(text,0,sizeof(text));
    queue<int>qx,qy;
    qx.push(x);
    qy.push(y);
    text[x][y]=1;
    while(!qx.empty())
    {
        int tx=qx.front();
        int ty=qy.front();
        qx.pop();qy.pop();
        for(int i=1;i<=8;i++)
        {
            int xx=tx+dx[i];
            int yy=ty+dy[i];
            if(!ok(xx,yy)) continue;
            if(text[xx][yy]) continue;
            text[xx][yy]=1;
            if(a[xx][yy]==1)
            {
                qx.push(xx);
                qy.push(yy);
            }
            else
            {
                d[x][y][xx][yy]=1;
            }
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    {
        int t,s,tox,toy;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&a[i][j]);
            if(a[i][j]==3)
            {
                t=i;
                s=j;
            }
            else if(a[i][j]==4)
            {
                tox=i;
                toy=j;
            }
        }
        memset(d,0,sizeof(d));
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        bfs(i,j);
        bfs2(t,s);
        if(dist[tox][toy]>1e3)
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n%lld\n",dist[tox][toy]-1,ans[tox][toy]);
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值