P3818 小A和uim之大逃离 II

来源:互联网 发布:删除数组中重复元素 编辑:程序博客网 时间:2024/06/05 19:24

luogu 传送门

bfs拓展嘛。
不过这里有一点特别之处,就是记录状态时要三维,f[i][j][0/1],代表到了(i , j)这个点是否使用过向量。
在bfs中往四面走,如果没有使用过向量,就再拓展一下使用向量的。(还是比较容易啦)

#include<cstdio>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<queue>#define LL long longusing namespace std;int n,m,D,R;int a[1001][1001];bool f[1001][1001][2];struct H{int x,y,flag,step;};int dx[]={0,0,1,-1},dy[]={1,-1,0,0}; int bfs(){    queue <H> q;    q.push((H){1,1,0,0});    f[1][1][0]=1;    while(!q.empty())    {        H k=q.front();q.pop();        if(k.x==n&&k.y==m) return k.step;        for(int i=0;i<4;i++)        {            int x=k.x+dx[i],y=k.y+dy[i];            if(x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]&&!f[x][y][k.flag])            {                q.push((H){x,y,k.flag,k.step+1});                f[x][y][k.flag]=1;            }         }        if(!k.flag){            int x=k.x+D,y=k.y+R;            if(x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]&&!f[x][y][1])             {                q.push((H){x,y,1,k.step+1});                f[x][y][1]=1;            }         }    }    return -1; }int main(){    scanf("%d%d%d%d",&n,&m,&D,&R);    for(int i=1;i<=n;i++)      for(int j=1;j<=m;j++){        char c;cin>>c;         if(c=='.') a[i][j]=1;    }    int ans=bfs();    printf("%d",ans);    return 0;}