package com.ds;
import java.util.Random;
/**
* Created by developer on 2016/5/9.
*/
public class DrawMapColor {
public static void main(String[] args) {
Node[] nodes = new Node[]{new Node(), new Node(), new Node(), new Node(), new Node(), new Node()};
// 关系表
int[][] nn = new int[][] {
//{A, B, C, D, E, F}
{0, 1, 1, 1, 1, 0}, // A
{1, 0, 1, 0, 1, 1}, // B
{1, 1, 0, 1, 0, 0}, // C
{1, 0, 1, 0, 1, 0}, // D
{1, 1, 0, 1, 0, 1}, // E
{0, 1, 0, 0, 1, 0} // F
};
Color[] colors = new Color[] {Color.BLACK, Color.BLUE, Color.GREEN, Color.RED};
Node A = nodes[0];
A.color = colors[0];
for (int i=1; i < nodes.length; ++i) {
Node next = nodes[i];
//Color colorTemp = colors[0];
int[] rels = nn[i];
for (;;) {
Random r = new Random(System.currentTimeMillis());
int rx = r.nextInt() % 3;
if (rx < 0) rx = -rx;
Color colorTemp = colors[rx];
int tag = 1;
for (int j = 0; j < rels.length; ++j) {
if (rels[j] == 1 && nodes[j].color == colorTemp) {
tag = 0;
break;
}
}
if (tag == 1) {
next.color = colorTemp;
break;
}
}
}
for (Node node: nodes) {
System.out.print(node.color + "\t");
}
}
}
地图着色问题
最新推荐文章于 2024-01-11 16:31:28 发布