一笔画,不能走斜线,
一笔画,不能走斜线,不能经过红色的点,能不能一笔画完。
我用程序跑的结果,是不存在这样的一笔画。
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); } } }
posted on 2018-12-14 22:19 angelshelter 阅读(717) 评论(0) 编辑 收藏 举报