In the following question I am getting only 2 correct answers for the code given below. Please help me with what cases are going wrong.
https://www.codechef.com/problems/FENCE
The idea is to account for every fence encountered when we travel row and column wise.
Once a plant is encountered 2 edges are counted (in both row-wise and column-wise traversal), the previous is checked for adjacency in the row or column. If adjacent, the 2 common edges are subtracted.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
return (a.second < b.second);
}
int main() {
ll t;
cin>>t;
while(t--){
ll n,m,k;
cin>>n>>m>>k;
vector<pair<ll,ll>> a(k,make_pair(0,0));
ll count=4;
if(k==0) count=0;
else{
ll x,y;
int l=1;
cin>>x>>y;
a[0]=make_pair(x,y);
for(ll i=1;i<k;i++){
ll x,y;
cin>>x>>y;
a[l]=make_pair(x,y);
if(a[l]!=a[l-1]) l++;
}
sort(a.begin(),a.end());
for(ll i=1;i<k;i++){
if((a[i-1].first==a[i].first && a[i-1].second+1==a[i].second)) count-=2;
count+=2;
}
sort(a.begin(),a.end(),sortbysec);
for(ll i=1;i<k;i++){
if((a[i-1].second==a[i].second && a[i-1].first+1==a[i].first)) count-=2;
count+=2;
}
}
cout<<count<<endl;
}
return 0;
}