檢查給定標誌是否被刪除的 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