1.假设可以用color种颜色,如果不满足则增大color的量
import java.util.*; public class Main{ static int[] state; static int color=3; static int len; public static void main(String[] args) { int[][] e={{0,0,0,0,0},{0,0,1,1,1},{0,1,0,1,0},{0,1,1,0,0},{0,1,0,0,0}}; len=e.length; state=new int[len]; if(deepFun(0,e)){ System.out.println("OK"); for(int i=1;i<len;i++){ System.out.println(state[i]); } }else{ System.out.println("NOT OK"); } } public static boolean deepFun(int index,int[][] e){ if(isOk(index,e)){ if(index==len-1){ return true; }else{ for(int i=1;i<=color;i++){ state[index+1]=i; if(deepFun(index+1,e)){ return true; } state[index+1]=0; } return false; } }else{ return false; } } public static boolean isOk(int index,int[][] e){ for(int i=1;i<index;i++){ if(e[index][i]==1 && state[i]==state[index]){ return false; } } return true; } }