1. 程式人生 > >百練 4115 鳴人和佐助

百練 4115 鳴人和佐助

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=200+10;
int m,n,t,xm,ym,xz,yz;
char s[maxn][maxn];
int vis[maxn][maxn][maxn];
struct node{
    int x,y,t,steps;
    node(int x,int y,int t,int steps) :x(x),y(y),t(t),steps(steps) {};
};
bool inside(int x,int y)
{
    return x>=1&&x<=m&&y>=1&&y<=n;
}
void BFS()
{
    int x,y,l,steps;
    queue<node> q;
    vis[xm][ym][t]=1;
    q.push(node(xm,ym,t,0));
    while(!q.empty())
    {
        node ans=q.front();
        x=ans.x,y=ans.y,l=ans.t,steps=ans.steps;q.pop();
        if(x==xz&&y==yz) {printf("%d\n",steps); return ;}
        for(int dx=-1;dx<=1;dx++)
        {
            for(int dy=-1;dy<=1;dy++)
            {
                if((dx&&dy)||(dx&&dy)) continue;
                int a=x+dx,b=y+dy;
                if(!inside(a,b)) continue;
                if(s[a][b]=='#'&&l!=0&&!vis[a][b][l-1])
                {q.push(node(a,b,l-1,steps+1));vis[a][b][l-1]=1;}
                else if((s[a][b]=='*'||s[a][b]=='+')&&!vis[a][b][l])
                {q.push(node(a,b,l,steps+1));vis[a][b][l]=1;}
            }
        }
    }
    printf("-1\n");
}
int main()
{
    scanf("%d%d%d",&m,&n,&t);
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='@') xm=i,ym=j;
            if(s[i][j]=='+') xz=i,yz=j;
        }
    }
    BFS();
    return 0;
}