检查给定标志是否被删除的 C++ 代码
假设我们有一个大小为 n x m 的矩阵。每个单元格都将容纳一个 0 到 9 的值。应该剥离一个标志:标志的每一行都应包含同色的方块,并且相邻行的颜色应不同。我们必须检查给定的矩阵是否为有效标志。
因此,如果输入如下
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| 3 | 3 | 3 |
步骤
要解决此问题,我们将遵循以下步骤 −
n := row count of matrix m := column count of matrix l := 'm' res := 1 for initialize i := 0, when i < n, update (increase i by 1), do: f := matrix[i, 0] for initialize j := 0, when j < m, update (increase j by 1), do: if matrix[i, j] is not equal to f, then: res := 0 if l is same as f, then: res := 0 l := f return (if res is non-zero, then true, otherwise false)
示例
让我们看以下实现,以便更好地理解 −
#include <bits/stdc++.h>
using namespace std;
bool solve(vector<vector<int>> matrix){
int n = matrix.size();
int m = matrix[0].size();
char l = 'm';
bool res = 1;
for (int i = 0; i < n; i++){
char f = matrix[i][0];
for (int j = 0; j < m; j++){
if (matrix[i][j] != f)
res = 0;
}
if (l == f)
res = 0;
l = f;
}
return res ? true : false;
}
int main(){
vector<vector<int>> matrix = { { 0, 0, 0 }, { 1, 1, 1 }, { 3, 3, 3 } };
cout << solve(matrix) << endl;
}输入
{ { 0, 0, 0 }, { 1, 1, 1 }, { 3, 3, 3 } }输出
1
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP