四色定理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;

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值