我正在编写一个 C++ 代码,我想知道以字符形式编写的最大矩形区域的大小和位置。 看看这里:
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
bool checknextrow(int l, vector<vector<char>>& matrix, vector<vector<bool>>& flag, int is, int ie, int currrow) {
bool rflag = true;
for (int i = is; i <= ie; i++) {
if (matrix[currrow + 1][i] != matrix[currrow][is] && flag[currrow + 1][i] != false) {
rflag = false;
goto exit;
}
}
exit:
return rflag;
}
int main() {
cout << endl;
ofstream output("output.txt");
ifstream input("input.txt");
input >> noskipws;
int l = 1;
input >> l;
vector<vector<char>> matrix(l, vector<char> (l));
vector<vector<bool>> flag(l, vector<bool>(l));
struct land {
int s1 = 0, s2 = 0, e1 = 0, e2 = 0;
int size = 0;
};
int tempLsize = pow(l, 2);
vector<land> L(tempLsize);
int Lnum = 0;
for (int j = 0; j < l; j++) {
for (int i = 0; i < l; i++) {
input.get(matrix[j][i]);
flag[j][i] = false;
}
}
for (int j = 0; j < l; j++) {
for (int i = 0; i < l; i++) {
if (matrix[j][i] = false) {
flag[j][i] = true;
L[Lnum].size++;
int tj = j, ti = i;
while (matrix[j][ti] == matrix[j][i]) {
ti++;
L[Lnum].size++;
flag[j][ti] = true;
}
while (checknextrow(l, matrix, flag, i, ti, j)) {
tj++;
for (int fi = i; fi <= ti; fi++) {
L[Lnum].size++;
flag[tj][fi] = true;
}
}
L[Lnum].s1 = j; L[Lnum].s2 = i; L[Lnum].e1 = tj; L[Lnum].e2 = ti;
Lnum++;
}
}
}
//int tempmin = L[0].size; int tempmax = L[Lnum].size;
int ans = 0;
//ans = max_element(tempmin, tempmax);
int tempans = 0;
int tempmax = L[0].size;
for (int temp = 0; temp <= Lnum; temp++) {
if (tempmax <= L[temp].size) {
tempans = tempmax;
tempans = temp;
}
}
output << " Size: " << ans;
output << endl << endl << " Start: [" << L[tempans].s1 << "] [" << L[tempans].s2 << "]";
output << endl << " End: [" << L[tempans].e1 << "] [" << L[tempans].e2 << "]";
cout << l << endl;
for (int j = 0; j < l; j++) {
for (int i = 0; i < l; i++) {
cout << matrix[j][i];
}
cout << endl;
}
cout << endl << " Size: " << ans;
cout << endl << endl << " Start: [" << L[tempans].s1 << "] [" << L[tempans].s2 << "]";
cout << endl << " End: [" << L[tempans].e1 << "] [" << L[tempans].e2 << "]";
output.close();
input.close();
cout << endl;
return 0;
}
由于某种原因,我的问题是无法从输入文件中读取字符向量矩阵。
比如说,这是我放入文件中的内容: 5 阿布 CCBBD bbbb 巴巴 哎呀
我已经查看了其他人与此相关的问题,但修复“input.get(matrix[j][i])”对我不起作用。 cout 调试中的矩阵只是显示为空白。 请帮忙。
由于某种原因,我的问题是无法读取字符向量 来自输入文件的矩阵。
有一个错误:
if (matrix[j][i] = false)
-> if (matrix[j][i] == false)