zoukankan      html  css  js  c++  java
  • 一笔画,不能走斜线,

    一笔画,不能走斜线,不能经过红色的点,能不能一笔画完。

     我用程序跑的结果,是不存在这样的一笔画。

    package com.dr.iptv.apiutil;
    
    import java.util.Arrays;
    
    import org.apache.commons.lang.StringUtils;
    
    import com.dr.iptv.util.ArrayUtil;
    
    public class Main10 {
        
        public static int c = 0;
        public static String spath = "";
        
        public static void main(String[] args) {
            //Integer[][] sk = new Integer[5][5];
            Integer[][] sk = newIntArray();
            print(sk);
            //System.out.println(sk);
            //ArrayUtils.cl
            //Arrays.copyOf(sk);
            
            
            for(int i=0;i<5;i++) {
                for(int j=0;j<5;j++) {
                    next("", new Point(i, j), sk);
                }
            }
    
    /*
            next("", new Point(0, 0), sk);
            System.out.println(spath);
    */
        }
        
        public static Integer[][] clone(Integer[][] map){
            Integer[][] ret = new Integer[5][5];
            for(int i=0;i<5;i++) {
                for(int j=0;j<5;j++) {
                    ret[i][j] = map[i][j];
                }
            }
            return ret;
        }
        
        public static Integer[][] newIntArray() {
            Integer[][] ret = new Integer[5][5];
            for(int i=0;i<5;i++) {
                for(int j=0;j<5;j++) {
                    ret[i][j] = 0;
                }
            }
            ret[0][1] = 1;
            return ret;
        }
        
        public static void print(Integer[][] sk) {
            for(Integer[] k : sk) {
                System.out.println(StringUtils.join(k, ','));
            }
        }
        
        public static void next(String path, Point point, Integer[][] map) {
            boolean canRun = false;
            //向上走
            int up = point.y - 1;
            if(up>=0) {
                if(map[point.x][up]!=1) {
                    canRun = true;
                    Integer[][] newMap = clone(map);
                    newMap[point.x][up] = 1;
                    next(path + point, new Point(point.x, up), newMap);
                }
            }
            //向下走
            int down = point.y + 1;
            if(down<5) {
                if(map[point.x][down]!=1) {
                    canRun = true;
                    Integer[][] newMap = clone(map);
                    newMap[point.x][down] = 1;
                    next(path + point, new Point(point.x, down), newMap);
                }
            }
            //向左走
            int left = point.x - 1;
            if(left>=0) {
                if(map[left][point.y]!=1) {
                    canRun = true;
                    Integer[][] newMap = clone(map);
                    newMap[left][point.y] = 1;
                    next(path + point, new Point(left, point.y), newMap);
                }
            }
            //向右走
            int right = point.x + 1;
            if(right<5) {
                if(map[right][point.y]!=1) {
                    canRun = true;
                    Integer[][] newMap = clone(map);
                    newMap[right][point.y] = 1;
                    next(path + point, new Point(right, point.y), newMap);
                }
            }
            if(!canRun) {
                //System.out.println(path);
                if(checkOK(map)) {
                    System.out.println(path);
                }
                if(path.length()>c) {
                    c = path.length();
                    spath = path;
                }
            }
        }
        
        public static boolean checkOK(Integer[][] map) {
            for(int i=0;i<5;i++) {
                for(int j=0;j<5;j++) {
                    if(map[i][j] == 0) {
                        return false;
                    }
                }
            }
            return true;
        }
        
        public static class Point{
            public int x;
            public int y;
            
            public Point(int x, int y) {
                this.x = x;
                this.y = y;
            }
            @Override
            public String toString() {
                return String.format("【%s,%s】->", x, y);
            }
        }
    }
  • 相关阅读:
    mysql基础-01
    Delphi 和键盘有关的API函数(Keyboard Input)
    Delphi System单元-Odd- 判断是否是奇数
    Delphi 键盘API GetKeyState、GetAsyncKeyState -获取键盘 / 按键值Key的状态
    Delphi 全局热键KeyPress 和 热键 API(RegisterHotKey、UnRegisterHotKey、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom)
    Delphi XE Android platform uses-permission[2] AndroidManifest.xml 配置
    Delphi XE Android platform uses-permission[1] 权限列表
    Delphi XE Andriod 文件后缀对应MIME类型
    Delphi XE RTL Androidapi 单元
    Delphi XE FMX TFmxObject 类 和 单元
  • 原文地址:https://www.cnblogs.com/angelshelter/p/10121983.html
Copyright © 2011-2022 走看看