/*
Given a matrix, find the value area which correponde to the given value.
For example;
0 0 0 0 1 1 1 1
1 0 0 0 1 0 0 0
0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 0
if the given position is (0, 4), the value is 1, return area 9.
if the given position is (0, 0), the value is 0, return 11.
*/
// Requirement: using two ways
// first using dfs.
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
struct pos {
int x;
int y;
pos(int i, int j) : x(i), y(j) {}
};
int surroundAreaII(vector< vector<int> >& matrix, int i, int j) {
int count = 0;
queue<pos> position;
int target = matrix[i][j];
int cnt = 1;
matrix[i][j] = -1;
pos curr(i, j);
position.push(curr);
vector< vector<int> > dir {
{0, 1},
{0, -1},
{-1, 0},
{1, 0}};
while(!position.empty()) {
pos tmp = position.front();
position.pop();
for(auto kir : dir) {
int next_x = kir[0] + tmp.x;
int next_y = kir[1] + tmp.y;
if(next_x >= 0 && next_x < matrix.size() && next_y >= 0 && next_y < matrix[0].size() && (matrix[next_x][next_y] == target)) {
pos nextPos(next_x, next_y);
position.push(nextPos);
cnt++;
matrix[next_x][next_y] = -1;
}
}
}
return cnt;
}
// use -1 to mark the node as visited.
void dfs(vector< vector<int> >& matrix, int i, int j, int& count, int value) {
if(i < 0 || i >= matrix.size() || j < 0 || j >= matrix[i].size() || (matrix[i][j] == -1)) {
return;
} else {
if(matrix[i][j] == value) {
count = count + 1;
matrix[i][j] = -1;
dfs(matrix, i - 1, j, count, value);
dfs(matrix, i + 1, j, count, value);
dfs(matrix, i, j + 1, count, value);
dfs(matrix, i, j - 1, count, value);
}
}
}
int surroundArea(vector< vector<int> >& matrix, int i, int j) {
int count = 0;
dfs(matrix, i, j, count, matrix[i][j]);
return count;
}
int main(void) {
vector< vector<int> > matrix {
{0, 0, 0, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 0}};
cout << surroundAreaII(matrix, 0, 0) << endl;
}
2: Largest Area the island has:
#include "header.h"
using namespace std;
/*
1, 1, 0, 0, 1
1, 1, 1, 0, 0
1, 1, 0, 0, 0
0, 0, 0, 0, 1
Find the largest area of the island.
*/
void dfs(vector< vector<int> >& matrix, int i, int j, int& maxArea, int leftX, int rightX, int upY, int downY) {
if(i < 0 || i >= matrix.size() || j < 0 || j >= matrix[0].size() || matrix[i][j] == -1 || matrix[i][j] == 0) return;
leftX = min(leftX, i);
rightX = max(rightX, i);
upY = min(upY, j);
downY = max(downY, j);
maxArea = max(maxArea, (abs(rightX - leftX) + 1) * (abs(upY - downY) + 1));
matrix[i][j] = -1;
dfs(matrix, i - 1, j , maxArea, leftX, rightX, upY, downY);
dfs(matrix, i + 1, j, maxArea, leftX, rightX, upY, downY);
dfs(matrix, i, j - 1, maxArea, leftX, rightX, upY, downY);
dfs(matrix, i, j + 1, maxArea, leftX, rightX, upY, downY);
}
int largestArea(vector< vector<int> > matrix) {
if(matrix.size() == 0 || matrix[0].size() == 0) return 0;
int maxArea = 1;
for(int i = 0; i < matrix.size(); ++i) {
for(int j = 0; j < matrix[0].size(); ++j) {
if(matrix[i][j] == 1) {
int leftX = i, rightX = i, upY = j, downY = j;
dfs(matrix, i, j, maxArea, leftX, rightX, upY, downY);
}
}
}
return maxArea;
}
int main(void) {
vector< vector<int> > matrix {
{1, 1, 0, 0, 1},
{1, 1, 1, 0, 0},
{1, 1, 0, 0, 0},
{0, 0, 0, 0, 1}};
cout << largestArea(matrix) << endl;
}