栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3271 Lilypad Pond

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3271 Lilypad Pond

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=30+9;int dist[maxn][maxn],a[maxn][maxn];int n,m;bool d[maxn][maxn][maxn][maxn];int quex[maxn*maxn],quey[maxn*maxn];long long ans[maxn][maxn];bool text[maxn][maxn];void bfs2(int t,int s){    int front=1,end=0;    quex[++end]=t;    quey[end]=s;    memset(dist,50,sizeof(dist));    memset(ans,0,sizeof(ans));    dist[t][s]=0;    ans[t][s]=1;    while(front<=end)    {        int x=quex[front],y=quey[front++];        if(a[x][y]==2) continue;        for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        if(d[x][y][i][j])        { if(dist[i][j]==dist[x][y]+1) ans[i][j]+=ans[x][y]; else if(dist[i][j]>dist[x][y]+1) {     dist[i][j]=dist[x][y]+1;     ans[i][j]=ans[x][y];     quex[++end]=i;     quey[end]=j; }        }    }}void bfs(int t,int s){    memset(text,0,sizeof(text));    int front=1,end=0;    quex[++end]=t;    quey[end]=s;    text[t][s]=1;    while(front<=end)    {        int x=quex[front],y=quey[front++];        for(int i=-1;i<=1;i+=2)        for(int j=-1;j<=1;j+=2)        for(int k=1;k<=2;k++)        { int tmp=1; if(k==1) tmp=2; int tox=x+i*k,toy=j*tmp+y; if(tox>=1&&tox<=n&&toy>=1&&toy<=m) if(!text[tox][toy]) {     text[tox][toy]=1;     if(a[tox][toy]==1)     {         quex[++end]=tox;         quey[end]=toy;     }     else     {         d[t][s][tox][toy]=1;     } }        }    }}int main(){    while(scanf("%d %d",&n,&m)!=EOF)    {        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("-1n");        }        else        { printf("%dn%lldn",dist[tox][toy]-1,ans[tox][toy]);        }    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377271.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号