广搜之鸣人与佐助

#include<iostream>
#include <stdio.h>
#include <memory.h>
#include <queue>
using namespace std;
int M,N,T;
char Map[205][205];
int visited[205][205][15];
int dx[]={1,-1,0,0};
int dy[] = {0,0,1,-1};
struct node
{
    int x,y,time,ckl;
    node(int xx,int yy,int tt,int cc):x(xx),y(yy),time(tt),ckl(cc) {};
    node() {}
};
queue<node> q;
int main()
{
    cin >> M >> N >> T;
    node mingren,zuozhu;
    memset(visited,1,sizeof(visited));
    for(int i=1; i<=M; i++)
        for(int j=1; j<=N; j++)
        {
            cin >> Map[i][j];
            if(Map[i][j]=='@')
            {
                mingren=node(i,j,0,T);
            }
            else if(Map[i][j]=='+')
            {
                Map[i][j]='*';
                zuozhu=node(i,j,-1,-1);
            }
            for(int t=0; t<=T; t++)
                visited[i][j][t]=0;
        }
    q.push(mingren);
    visited[mingren.x][mingren.y][mingren.ckl]=1;
    while(!q.empty())
    {
        node now = q.front();
        if(now.x==zuozhu.x && now.y==zuozhu.y)
        {
            cout<<now.time<<endl;
            return 0;
        }
        q.pop();
        for(int i=0; i<4; i++)
        {
            int endx = now.x+dx[i];
            int endy = now.y+dy[i];
            if(endx>0&&endx<=M&&endy>0&&endy<=N && Map[endx][endy]=='*' &&!visited[endx][endy][now.ckl])
            {
                q.push(node(endx,endy,now.time+1,now.ckl));
                visited[endx][endy][now.ckl] = 1;
            }
            if(endx>0&&endx<=M&&endy>0&&endy<=N&&Map[endx][endy]=='#' && (now.ckl>0) &&(!visited[endx][endy][now.ckl-1]))
            {
                q.push(node(endx,endy,now.time+1,now.ckl-1));
                visited[endx][endy][now.ckl-1] = 1;
            }
        }

    }
    cout<<-1<<endl;
    return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页