zoukankan      html  css  js  c++  java
  • 【BFS】鸣人与佐助

    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述

    佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?

     

    已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?

    输入
    输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
    后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
    输出
    输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。
    样例输入
    样例输入1
    4 4 1
    #@##
    **##
    ###+
    ****
    
    样例输入2
    4 4 2
    #@##
    **##
    ###+
    ****
    样例输出
    样例输出1
    6
    
    样例输出2
    4
    参考代码
    ///最短路用广搜,全部解用深搜
    #include <iostream>
    #include <cstdio>
    #include <queue>
    using namespace std;
    int mx,my;
    char c[202][202];
    int vis[202][202][11];
    int dir1[4]={-1,1,0,0};
    int dir2[4]={0,0,-1,1};
    
    struct naruto{//鸣人的状态
    int x,y;
    int ch;//查克拉
    int time;
    };
    
    queue <naruto> que;
    
    int bfs(int sx,int sy,int ch);
    int main()
    {
    
        int M,N,T;
    
        scanf("%d%d%d",&M,&N,&T);
        getchar();///!!!!!!!!!
        for(int i=1;i<=M;i++){
            for(int j=1;j<=N;j++){
                    scanf("%c",&c[i][j]);
                if(c[i][j]=='@'){
                    mx=i;
                    my=j;
                    vis[i][j][T]=1;
                }
    
            }
            getchar();
        }
    ///把边界当作墙
    for(int i=0;i<=N+1;++i)
    
    c[0][i]=c[M+1][i]='!';
    
    for(int i=0;i<=M+1;++i)
    
    c[i][0]=c[i][N+1]='!';
    
        printf("%d
    ",bfs(mx,my,T));
        return 0;
    }
    int bfs(int sx,int sy,int ch){
    //往队列里塞第一个元素
    naruto tm;
    tm.x=sx;
    tm.y=sy;
    tm.ch=ch;
    tm.time=0;
    que.push(tm);
    while(!que.empty()){
        tm=que.front();
        if(c[tm.x][tm.y]=='+'){
            return tm.time;
        }
            que.pop();
            //遍历四个方向
            for(int i=0;i<4;i++){
                int nx=tm.x+dir1[i];
                int ny=tm.y+dir2[i];
                    if((c[nx][ny]=='#'&&tm.ch>0)||c[nx][ny]=='*'||c[nx][ny]=='+'){
                            naruto tmp;
                        tmp.x=nx;
                        tmp.y=ny;
    
                        if(c[nx][ny]=='#')
                           tmp.ch=tm.ch-1;
                        else
                        tmp.ch=tm.ch;
    
                        tmp.time=tm.time+1;
                        if(vis[nx][ny][tmp.ch]==0){
                            que.push(tmp);
                            vis[nx][ny][tmp.ch]=1;
                        }
    
                    }
            }
    }
    //找不到佐助就返回-1
    return -1;
    }
  • 相关阅读:
    网络测量中基于Sketch方法的简单介绍
    Reading SBAR SDN flow-Based monitoring and Application Recognition
    Reading Meticulous Measurement of Control Packets in SDN
    Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing
    ovs加dpdk在日志中查看更多运行细节的方法
    后缀数组
    (转载)LCA问题的Tarjan算法
    Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
    Vijos 1816统计数字(计数排序)
    卡特兰数
  • 原文地址:https://www.cnblogs.com/LuRenJiang/p/7460208.html
Copyright © 2011-2022 走看看