检查给定标志是否被删除的 C++ 代码


假设我们有一个大小为 n x m 的矩阵。每个单元格都将容纳一个 0 到 9 的值。应该剥离一个标志:标志的每一行都应包含同色的方块,并且相邻行的颜色应不同。我们必须检查给定的矩阵是否为有效标志。

因此,如果输入如下

000
111
333

步骤

要解决此问题,我们将遵循以下步骤 −

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)

示例

让我们看以下实现,以便更好地理解 −

Open Compiler
#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; }

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输入

{ { 0, 0, 0 }, { 1, 1, 1 }, { 3, 3, 3 } }

输出

1

更新时间:2022 年 3 月 29 日

243 次浏览

开启你的 事业

通过完成课程获得认证

开始吧
广告