什么是寻找最大岛屿问题
可以参见如下链接 寻找最大岛屿问题
由此可以看出,这个岛屿问题只有0和1两种情况,即1是岛屿而0是海洋。如果岛屿有许多种/许多个,或者是——岛屿有许多种颜色应该怎么办呢?作者经过思考后认为可以通过一个Map(字典)来存储不同的颜色与不同的颜色对应的数量。如果要求输出面积最大的小块,可新建一个数据结构(这里是Stack)来解决这一问题。
使用EasyX VS2019 on Windows 11 21H2 x64 Debug
main.cpp
#include"lagest_land.h"
#include <graphics.h> // 引用图形库头文件
#include <conio.h>
#include<fstream>
#include<iostream>
#include<Windows.h>
#include<winnt.h>
int main() {
std::vector<std::string> inputFiles;
std::vector<std::vector<std::string>> inputChess;
std::ifstream inChess("Chess_data.txt");
std::string c;
int i = 0;
while (inChess>>c) {
inputFiles.push_back(c);
std::cout << inputFiles.back() << std::endl;
}
int row = atoi(inputFiles[0].data());
int col = atoi(inputFiles[1].data());
for (i = 2; i < inputFiles.size(); i++) {
std::vector<std::string> temp;
for (int j = 0; j < col; j++) {
string s;
s.push_back(inputFiles[i][j]);
temp.push_back(s);
}
inputChess.push_back(temp);
}
//以上是将文件读入数组的方式
//以下是算法
Solution algo;
map<string,int> ans = algo.maxAreaOfIsland(inputChess);
cout << ans["W"] << endl;
cout << ans["R"] << endl;
cout << ans["G"] << endl;
cout << ans["B"] << endl;
stack<vector<int>> maxC = algo.getCooridinates();
cout << maxC.size() << endl;
//while (!maxC.empty())
//{
// cout << " " << maxC.top()[0]+1 << " " << maxC.top()[1]+1 << endl;
// maxC.pop();
//}
// 以下是界面函数
initgraph(640, 480);
int startX = 10;
int startY = 10;
int size = 20;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (inputChess[i][j] == "B") {
setfillcolor(BLUE);
fillrectangle(startX + size * j, startY + size * i, startX + size * (j + 1), startY + size * (i + 1));
}
if (inputChess[i][j] == "W") {
setfillcolor(WHITE);
fillrectangle(startX + size * j, startY + size * i, startX + size * (j + 1), startY + size * (i + 1));
}
if (inputChess[i][j] == "R") {
setfillcolor(RED);
fillrectangle(startX + size * j, startY + size * i, startX + size * (j + 1), startY + size * (i + 1));
}
if (inputChess[i][j] == "G") {
setfillcolor(GREEN);
fillrectangle(startX + size * j, startY + size * i, startX + size * (j + 1), startY + size * (i + 1));
}
}
}
_getch(); // 按任意键继续
closegraph(); // 关闭绘图窗口
initgraph(640, 480);
while (!maxC.empty())
{
if (inputChess[maxC.top()[0]][maxC.top()[1]] == "W") {
setfillcolor(WHITE);
};
if (inputChess[maxC.top()[0]][maxC.top()[1]] == "R") {
setfillcolor(RED);
};
if (inputChess[maxC.top()[0]][maxC.top()[1]] == "G") {
setfillcolor(GREEN);
};
if (inputChess[maxC.top()[0]][maxC.top()[1]] == "B") {
setfillcolor(BLUE);
};
fillrectangle(startX + size * maxC.top()[1], startY + size * maxC.top()[0], startX + size * (maxC.top()[1] + 1), startY + size * (maxC.top()[0] + 1));
maxC.pop();
}
_getch(); // 按任意键继续
closegraph(); // 关闭绘图窗口
return 0;
}
largest_land.h
#pragma once
#include<vector>
#include<map>
#include<string>
#include<stack>
using namespace std;
class Solution {
stack<vector<int>> maxCoordinate;
public:
map<string, int> maxAreaOfIsland(vector<vector<string>> grid) {
map<string, int> ans = {
{"W",0},
{"B",0},
{"R",0},
{"G",0}
};
for (int i = 0; i != grid.size(); ++i) {
for (int j = 0; j != grid[0].size(); ++j) {
stack<vector<int>> currCoordinate;
map<string, int> currans = {
{"W",0},
{"B",0},
{"R",0},
{"G",0}
};
stack<int> stacki;
stack<int> stackj;
stacki.push(i);
stackj.push(j);
string currColor = grid[i][j];
while (!stacki.empty()) {
int cur_i = stacki.top(), cur_j = stackj.top();
stacki.pop();
stackj.pop();
if (cur_i < 0 || cur_j < 0 || cur_i == grid.size() || cur_j == grid[0].size() || grid[cur_i][cur_j] != currColor||grid[cur_i][cur_j]=="0") {
continue;
}
++currans[currColor];
grid[cur_i][cur_j] = "0";
currCoordinate.push({ cur_i,cur_j });
int di[4] = { 0, 0, 1, -1 };
int dj[4] = { 1, -1, 0, 0 };
for (int index = 0; index != 4; ++index) {
int next_i = cur_i + di[index], next_j = cur_j + dj[index];
stacki.push(next_i);
stackj.push(next_j);
}
}
if (ans[currColor] < currans[currColor]) {
ans[currColor] = currans[currColor];
maxCoordinate = currCoordinate;
}
else {
}
}
}
return ans;
}
stack<vector<int>> getCooridinates() {
return maxCoordinate;
}
};
采用文件读写方式 下载棋盘