HDOJ2102 A计划


本题涉及搜索,可用DFS(深搜)解决

博主有一篇关于 深搜原理 的博客,不知道原理的可以去看看,懂深搜看下面代码(java):

import java.util.Scanner;

 class Main{

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int C=sc.nextInt();
            while(C-->0){
                int n=sc.nextInt();
                int m=sc.nextInt();
                int t=sc.nextInt();
                time=-1;
                Plot[][][] plots=new Plot[2][n][m];
                Plot p = null;
                for (int i = 0; i < plots.length; i++) {
                    for (int j = 0; j < plots[i].length; j++) {
                        String str=sc.next();
                        for (int k = 0; k < plots[i][j].length; k++) {
                            char c=str.charAt(k);
                            plots[i][j][k]=new Plot(i,j,k,c);
                            if(c=='P')
                                p=plots[i][j][k];
                        }
                    }
                }
                searchDFS(plots,plots[0][0][0]);
                if(p.time!=0&&p.time<=t)
                    System.out.println("YES");
                else
                    System.out.println("NO");
            }
        }
    }

    static int[][] dir={        {0,-1},
                         {-1,0},        {1,0},
                                 {0,1}      };
    static int time=-1;
    private static void searchDFS(Plot[][][] plots,Plot u) {
        time++;//进来+1秒
        u.isVisit=true;//涂黑
        if (u.time==0||u.time > time) 
            u.time = time;//记录第一次时间及最短访问时间
        if(u.c=='P')//找到,此次访问结束
            return;
        for (int i = 0; i < dir.length; i++) {//对前后左右访问
            int x=u.x;
            int y=u.y+dir[i][0];
            int z=u.z+dir[i][1];
            //可访问,递归
            if(y>=0&&y<plots[x].length && z>=0&&z<plots[x][y].length
                    && (plots[x][y][z].c=='.'||plots[x][y][z].c=='P' )&& !plots[x][y][z].isVisit){
                searchDFS(plots, plots[x][y][z]);
                //还原
                plots[x][y][z].isVisit=false;
                time--;
            }else if(y>=0&&y<plots[x].length && z>=0&&z<plots[x][y].length
                    && plots[x][y][z].c=='#' && !plots[x][y][z].isVisit){
                //穿越层次
                x=(1-x);
                //可穿,递归
                if((plots[x][y][z].c=='.'||plots[x][y][z].c=='P' )&& !plots[x][y][z].isVisit){
                    searchDFS(plots, plots[x][y][z]);
                    plots[x][y][z].isVisit=false;
                    time--;
                }
            }
        }
    }
}
class Plot{
    int x,y,z;
    int time;
    char c;
    boolean isVisit;
    public Plot(int x,int y,int z,char c) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.c = c;
    }
    Plot parent;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值