四色定理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 – 四色定理的递归算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分段线性变换函数——比特平面分层
- 下一篇: iOS移动开发1x 、2x 、3x图片介