# Fencing Problem

Fencing Problem: Here, we are going to find the solution to this problem which is based on Hashing, Implementation, STL. Submitted by Divyansh Jaipuriyar, on June 24, 2020

## Problem statement

There is a field with plants — a grid with N rows (numbered 1 through N) and M columns (numbered 1 through M); out of its NM cells, K cells contain plants, while the rest contain weeds. Two cells are adjacent if they have a common side.

You want to build fences in the field in such a way that the following conditions hold for each cell that contains a plant:

It is possible to move from this cell to each adjacent cell containing a plant without crossing any fences.

It is impossible to move from this cell to any cell containing weeds or to leave the grid without crossing any fences.

The fences can only be built between cells or on the boundary of the grid, i.e. on the sides of cells. The total length of the built fences is the number of pairs of side-adjacent cells such that there is a fence built on their common side plus the number of sides of cells on the boundary of the grid which have fences built on them. Find the minimum required total length of fences that need to be built.

### Input

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains three space-separated integers N, M, and K. K lines follow. Each of these lines contains two space-separated integers r and c denoting that the cell in row r and column c contains a plant.

### Output

For each test case, print a single line containing one integer — the minimum required length of fences.

## Examples

```INPUT:
T=1
N=9,M=9,K=1
1 1

Output:
4
As it is the only cell having plant and
it need to be fenced from all four sides.

INPUT:
T=1
N=4,M=4,k=9
1 4
2 1
2 2
2 3
3 1
3 3
4 1
4 2
4 3

Output:
20
...x
xxx.
x.x.
xxx.

As we need fenced to be built around the 4 walls
of the top right plant  and 2 fence for each of
the remaining 8 plants .
```

Problem source: https://www.codechef.com/problems/FENCE

## Solution Approach

We will use the STL properties to solve this kind of problems, initially, we will consider making fence all around the four sides of the plant then we check its contact with the other plant if the considered plant have any common edge with other plants then we remove the fence from that side thus it decreases by one, we keep doing this by checking all four sides of the plant. If there is no side with have any common edge with any plant then it will cost us 4 fences.

To store the pair of indexes that plant we will use the vector of pair, to check if the four sides of it are plant or not we will initialize all the plant with -1 in a map with key as indexes and value as -1.

We will use a count variable which will be 4 initially for each plant and it will either decrease or remain the same according to our checking. Then we will sum the entire count variable that will be our final answer.

## C++ Implementation

```#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{

cout << "Enter number of test cases: ";
ll t;

cin >> t;

while (t--) {
cout << "Enter N,M and k: ";
ll n, m, k;
cin >> n >> m >> k;

// initialize vector of pair to store
// indexes of plants.
vector<pair<ll, ll> > vp;
// map is used to check if the four sides
// of considered plants as plant or not
map<pair<ll, ll>, ll> ma;
cout << "Enter plant cordinates:\n";
for (ll i = 0; i < k; i++) {
ll x, y;
cin >> x >> y;
vp.push_back({ x, y });
// we store -1 in map for checking purpose.
ma[{ x, y }] = -1;
}
ll sum = 0;
// four sides of x coordinates.
ll dx[] = { 0, 0, 1, -1 };
// four sides of y coordinates.
ll dy[] = { 1, -1, 0, 0 };
for (auto it = vp.begin(); it != vp.end(); it++) {
// first coordinate of given plant.
ll x = it->first;
// second coordinate of given plant.
ll y = it->second;
// initializes count as 4 since we assume it as surrounded
// by weeds from all sides then we check its side and decrease its count.
ll cnt = 4;

// check its four sides for plants.
for (ll i = 0; i < 4; i++) {
ll u, v;
u = x + dx[i];
v = y + dy[i];
// if this side is plant then reduce count of fence by one.
if (ma[{ u, v }] == -1)
cnt--;
}
// add the required amount of fence
// for  current plant to sum.
sum += cnt;
}
cout << "Required amount of fence: ";
cout << sum << "\n";
}

return 0;
}
```

### Output

```Enter number of test cases: 3
Enter N,M and k: 9 9 1
Enter plant cordinates:
1 1
Required amount of fence: 4
Enter N,M and k: 5 5 3
Enter plant cordinates:
1 2
2 3
3 3
Required amount of fence: 10
Enter N,M and k: 4 4 9
Enter plant cordinates:
1 4
2 1
2 2
2 3
3 1
3 3
4 1
4 2
4 3
Required amount of fence: 20
```
• Time Complexity for above approach: O(k*log(k))
• Space Complexity for above approach: O(k)