计算湖泊(岛屿)
题目: 输入x、y,生成一个二维的网格,网格中全部由H和S组成,H代表陆地,S代表湖泊。每一个湖泊跟相邻的湖泊组成大湖泊,大湖泊总是被陆地包围。要求计算该二维网格内大湖泊的数量。
思路: S与其相邻的S共同组成大湖泊,因此遍历时相邻的S只计入一次。算法采用递归的思路,用坐标的形式,记录大湖泊内所有的坐标。
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
string s; 用来保存大湖泊的所有坐标
void collect_s(char** map, int i, int j , int m , int n)
递归将湖泊周围的湖泊全部加入字符串s
{
if (s.find(to_string(i) + ',' + to_string(j)) == -1)
如果s中不存在该坐标,则加入该坐标。
{
s = s + '[' + to_string(i) + ',' + to_string(j) + ']';
}
if (i > 0)
{
if (map[i - 1][j] == 'S' && s.find(to_string(i - 1) + ',' + to_string