• 路径之谜


    /*
    路径之谜
    
    小明冒充X星球的骑士,进入了一个奇怪的城堡。
    城堡里边什么都没有,只有方形石头铺成的地面。
    
    假设城堡地面是 n x n 个方格。【如图1.png】所示。
    
    按习俗,骑士要从西北角走到东南角。
    可以横向或纵向移动,但不能斜着走,也不能跳跃。
    每走到一个新方格,就要向正北方和正西方各射一箭。
    (城堡的西墙和北墙内各有 n 个靶子)
    
    
    同一个方格只允许经过一次。但不必做完所有的方格。
    
    如果只给出靶子上箭的数目,你能推断出骑士的行走路线吗?
    
    有时是可以的,比如图1.png中的例子。
    
    本题的要求就是已知箭靶数字,求骑士的行走路径(测试数据保证路径唯一)
    
    输入:
    第一行一个整数N(0<N<20),表示地面有 N x N 个方格
    第二行N个整数,空格分开,表示北边的箭靶上的数字(自西向东)
    第三行N个整数,空格分开,表示西边的箭靶上的数字(自北向南)
    
    输出:
    一行若干个整数,表示骑士路径。
    
    为了方便表示,我们约定每个小格子用一个数字代表,从西北角开始编号: 0,1,2,3....
    比如,图1.png中的方块编号为:
    
    0  1  2  3
    4  5  6  7
    8  9  10 11
    12 13 14 15
    
    
    示例:
    用户输入:
    4
    2 4 3 4
    4 3 3 3
    
    程序应该输出:
    0 4 5 1 2 3 7 11 10 9 13 14 15
    
    
    
    资源约定:
    峰值内存消耗 < 256M
    CPU消耗  < 1000ms
    
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    
    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。
    
    
    */
    package test;
    import java.util.*;
    public class 路径之谜 {
        static int n,a[][],nor[],eas[],path[];
        public static int[][] move = {{-1,0},{1,0},{0,-1},{0,1}};
        public static void main(String arg[]){
            long sta=System.currentTimeMillis();
            Scanner input=new Scanner(System.in);
            n=input.nextInt();
            a=new int[n][n];
            nor=new int[n];
            eas=new int[n];
            path=new int[n*n+1];
            int[] nor1=new int[n];
            int[] eas1=new int[n];
            a[0][0]=1;
            nor1[0]++;
            eas1[0]++;
            for(int i=0;i<n;i++){
                nor[i]=input.nextInt();
            }
            for(int i=0;i<n;i++){
                eas[i]=input.nextInt();
            }
            dfa(nor1,eas1,0,0,0);//nor1和eas1记录当前路径下的射箭数
            long end=System.currentTimeMillis();
            System.out.print("时间"+(end-sta));
        }
        public static void dfa(int[] nor1,int[] eas1,int x,int y,int num){
            if(x==n-1&&y==n-1){//如果骑士走到终点处
                boolean checkX=true,checkY=true;//用来标记在这条路径下射箭数量是否相等
                for(int i=0;i<n;i++){
                    if(nor[i]!=nor1[i])
                        checkX=false;
                    if(eas[i]!=eas[i])
                        checkY=false;
                }
                if(checkX&&checkY){
                    System.out.print("0 ");
                    for(int i=0;i<num;i++)
                        System.out.print(path[i]+" ");
                }
                return;
            }
            for(int i=0;i<4;i++){//一个位置有四个方向
                int tempx=x+move[i][0];
                int tempy=y+move[i][1];
                if(tempx>=0&&tempx<n&&tempy>=0&&tempy<n&&a[tempx][tempy]!=1){//在边框范围内,a数组用来标记是否走过该格
                    a[tempx][tempy]=1;
                    nor1[tempx]++;
                    eas1[tempy]++;
                    path[num]=tempx*n+tempy;
                    dfa(nor1,eas1,tempx,tempy,num+1);
                    a[tempx][tempy]=0;//回溯
                    nor1[tempx]--;
                    eas1[tempy]--;
                }
            }
        }
        
    
    }
  • 相关阅读:
    Orac and Medians
    牛牛的揠苗助长
    Color Graph
    Spanning Tree Removal【构造】
    A Simple Problem On A Tree
    Spring源码学习笔记(六、Spring启动流程解析:BeanFactory后置处理)
    Spring源码学习笔记(五、Spring启动流程解析:准备BeanFactory)
    一、求最大公约数
    Spring源码学习笔记(四、Spring启动流程解析:概述与准备上下文、获取BeanFactory)
    Spring源码学习笔记(三、路径和占位符,Spring容器如何解析配置信息)
  • 原文地址:https://www.cnblogs.com/ljs-666/p/8595613.html
Copyright © 2020-2023  润新知