当前位置:网站首页>Checkerboard Coloring Problem
Checkerboard Coloring Problem
2022-08-08 04:55:00 【Xiao Lu wants to brush the force and deduct the question】
前言
N * M的棋盘
The number of squares for each color must be the same
The upper, lower, left and right grids are considered adjacent
Adjacent squares must be dyed in different colors
All lattices must be dyed
Returns at least how many colors to complete the task
解题思路
Write the law of violence method observation
尝试1color can be completed
尝试2color can be completed
最多有n*m种
public static int minColors(int N, int M) {
// The number of colors isi
for (int i = 2; i < N * M; i++) {
int[][] matrix = new int[N][M];
// The following sentence is known,The minimum number of colors requiredi,一定是N*M的某个因子
if ((N * M) % i == 0 && can(matrix, N, M, i)) {
return i;
}
}
return N * M;
}
// 在matrixdyeing,Return onlypNumWhether the color can meet the requirements
public static boolean can(int[][] matrix, int N, int M, int pNum) {
int all = N * M;
int every = all / pNum;
ArrayList<Integer> rest = new ArrayList<>();
rest.add(0);
for (int i = 1; i <= pNum; i++) {
rest.add(every);
}
return process(matrix, N, M, pNum, 0, 0, rest);
}
public static boolean process(int[][] matrix, int N, int M, int pNum, int row, int col, List<Integer> rest) {
if (row == N) {
return true;
}
if (col == M) {
return process(matrix, N, M, pNum, row + 1, 0, rest);
}
int left = col == 0 ? 0 : matrix[row][col - 1];
int up = row == 0 ? 0 : matrix[row - 1][col];
for (int color = 1; color <= pNum; color++) {
if (color != left && color != up && rest.get(color) > 0) {
int count = rest.get(color);
rest.set(color, count - 1);
matrix[row][col] = color;
if (process(matrix, N, M, pNum, row, col + 1, rest)) {
return true;
}
rest.set(color, count);
matrix[row][col] = 0;
}
}
return false;
}


According to the output law,It is not difficult for us to deduce that the minimum color isN*M最小的质数因子
边栏推荐
- C language force to deduct the length of the last word of the 58th question.Traverse from back to front
- B. Reverse Binary Strings
- Open3D 基于颜色的ICP配准
- TSF微服务治理实战系列(二)——服务路由
- 基于扰动观察法的光伏mppt最大功率控制matlab仿真
- 2022-08-07 mysql/stonedb慢SQL-子查询-半连接
- NetCore uses Dapper to query data
- 【着色器实现Tricolor三原色型变效果_Shader效果第十八篇】
- 关于如何做选择
- Shell 脚本 — 多行注释、开启子/不开启子进程执行、转义带颜色输出、读取键盘输入、输入输出重定向、单双引号、命令替换、读取变量、系统变量、正则过滤、算术运算、一行多条命令、字符串比较
猜你喜欢

C language - score and loop statement

TSF微服务治理实战系列(二)——服务路由

数据库分库分表,何时分?怎样分?

leetcode 112. Path sum recursion

Young freshmen who yearn for open source | The guide to avoiding pits from open source to employment is here!

内修昇思MindSpore AI框架,外重行业汇聚,华为大模型的不平凡之路

【冷启动】快手《POSO: Personalized Cold Start Modules for Large-scale Recommender Systems》

【OAuth2】十八、OIDC的认识应用

MindFusion.WPF Pack 2022.R1

Shell 脚本 — 多行注释、开启子/不开启子进程执行、转义带颜色输出、读取键盘输入、输入输出重定向、单双引号、命令替换、读取变量、系统变量、正则过滤、算术运算、一行多条命令、字符串比较
随机推荐
Strong Net Cup 2019 - Casual Bet (Stacked Injection)
The storage principle of NorFlash
Servlet---ServletConfig类使用介绍
Some excellent blog recommendations for Qt event learning reference
Qt 事件学习参考的一些优秀博客推荐
如何保存页面的当前的状态
Filter 过滤器的使用
[opencv] Introduction to opencv development kit
76. 最小覆盖子串
leetcode-isomorphic string judgment
MindFusion.WPF Pack 2022.R1
【Win10】Several sleep problems and countermeasures
Cube - studio deployment process
postgresql中连接两张表更新第三张表(updata)
KMP和EXKMP(Z函数)
Spark entry learning-3-SparkSQL data abstraction
L3-005 Litter box distribution
【js基础】闭包的几种情况(代码)
spark入门学习-3-SparkSQL数据抽象
leetcode 112.路经总和 递归