欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

四色定理java_java – 四色定理的递归算法

发布时间:2023/12/20 编程问答 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 四色定理java_java – 四色定理的递归算法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

手头的问题是将地图分成区域,如邻接矩阵和

使用四种颜色,为地图着色,使得没有两个连续区域共享相同的颜色.我们会用的

邻接矩阵,用于编码哪个区域与哪个其他区域接壤.列和行

矩阵是区域,而如果两个区域不相邻,则单元格包含0,如果是,则单元格包含1

边界.创建一个递归回溯解决方案,该解决方案接受来自用户的交互式输入

地图中的区域数量和表示地图构成的邻接矩阵的文件名.

我遇到的问题是countryColor中的第一个值被更改,但数组中的许多值永远不会更改.

private static final int[] color = {1,2,3,4};

//this color array is meant to represent 4 colors like red, blue, green, orange etc.

private static int[][] map = {{0,1,1,0,1,1,0},{1,0,0,1,1,0,1},{1,0,0,1,1,1,0},{0,1,1,0,1,0,1},{1,1,1,1,0,0,0},{1,0,1,0,0,0,1},{0,1,0,1,0,1,0}};

//this is the adjacency matrix showing which countries are next to each other

private static int[] countryColor = new int[7];

//this is the array that holds the color values for each country

private static boolean colorMap(int country ){

System.out.println("Checking Country "+ country);

boolean check;

for(int j= 0;j< countryColor.length; j++){

if(useColor(country,color[j]) == true)

countryColor[country] = color[j];

if(country == countryColor.length-1)

return true;

check = colorMap(country+1);

System.out.println(check);

if(check == true)

return true;

countryColor[country]=0;

}

return false;

}

private static boolean useColor(int country, int color){

for(int i = 0; i < map.length;i++){

if(map[country][i] == 1&& countryColor[i]==color){

System.out.println("Nah country " + country +" cant be "+color );

return false;

}

}

return true;

}

总结

以上是生活随笔为你收集整理的四色定理java_java – 四色定理的递归算法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。