提交时间:2024-08-21 08:40:33

运行 ID: 174562

//四色地图简单样例代码 #include <bits/stdc++.h> using namespace std; bool Map[13][13]= {0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,1,1,1,1,1,0,0,0,0,0,0, 0,1,1,1,0,0,1,1,1,0,0,0,0, 0,1,1,1,1,0,0,0,1,1,0,0,0, 0,1,0,1,1,1,0,0,0,1,1,0,0, 0,1,0,0,1,1,1,0,0,0,1,1,0, 0,1,1,0,0,1,1,1,0,0,0,1,0, 0,0,1,0,0,0,1,1,1,0,1,1,1, 0,0,1,1,0,0,0,1,1,1,0,0,1, 0,0,0,1,1,0,0,0,1,1,1,0,1, 0,0,0,0,1,1,0,0,0,1,1,1,1, 0,0,0,0,0,1,1,1,0,0,1,1,1, 0,0,0,0,0,0,0,1,1,1,1,1,1}; //此处为图9.1所示的地图 int Color[13]; //存放各区域的颜色值,初始颜色均为0 bool Try(int k) //检查相邻区域是否有颜色冲突 { for(int i=1; i<=12; i++) if(Map[k][i] && Color[i]==Color[k] && k!=i) //如果相邻区域颜色相同 return 0; //颜色冲突,返回0 return 1; } int main() { int k=1; //k为要涂色区域的编号 while(k<=12) { Color[k]++; //颜色值加1 while((Color[k]<=4) && (!Try(k))) //颜色还没尝试完且当前颜色不合适 Color[k]++; //换成下一种颜色尝试 if(Color[k]>4) //如果4种颜色都不能选择 k--; //回溯到上一区域 else //如还有区域未涂色,则继续涂下一区域 Color[++k]=0; //k指向下一区域,颜色初始为0 } for(int i=1; i<=12; i++) printf("%d ",Color[i]); return 0; }