I tried solving the question and recieved WA in 4 test cases. I might be missing some boundary cases.Here is my solution:
CodeChef: Practical coding for everyone.
What could possibly be wrong … ??
I tried solving the question and recieved WA in 4 test cases. I might be missing some boundary cases.Here is my solution:
CodeChef: Practical coding for everyone.
What could possibly be wrong … ??
Solutions for this contest are not available yet…
Paste the code here
Here is my code…
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#define IL(a,b,c) for(a=b;a<c;a++)
#define DL(a,b,c) for(a=b;a>=c;a--)
#define ll long long
#define ld double
#define N 1000000007
#define MAX 1000
using namespace std;
int arr[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int bfs(pair<int,int> p){
int cnt=1;
vis[p.first][p.second]=1;
queue<pair<int,int> > q;
q.push(p);
while(!q.empty()){
pair<int,int> curr = q.front();
q.pop();
if(curr.first!=0 && vis[curr.first-1][curr.second]==0 && arr[curr.first][curr.second]==arr[curr.first-1][curr.second]){
q.push({curr.first-1,curr.second});
cnt++;
vis[curr.first-1][curr.second]=1;
}
if(curr.first!=n-1 && vis[curr.first+1][curr.second]==0 && arr[curr.first][curr.second]==arr[curr.first+1][curr.second]){
q.push({curr.first+1,curr.second});
cnt++;
vis[curr.first+1][curr.second]=1;
}
if(curr.second!=0 && vis[curr.first][curr.second-1]==0 && arr[curr.first][curr.second]==arr[curr.first][curr.second-1]){
q.push({curr.first,curr.second-1});
cnt++;
vis[curr.first][curr.second-1]=1;
}
if(curr.second!=m-1 && vis[curr.first][curr.second+1]==0 && arr[curr.first][curr.second]==arr[curr.first][curr.second+1]){
q.push({curr.first,curr.second+1});
cnt++;
vis[curr.first][curr.second+1]=1;
}
}
return cnt;
}
int main(){
int i,j;
cin >> n >> m;
IL(i,0,n){
IL(j,0,m){
cin >> arr[i][j];
}
}
map<int,int> grid;
IL(i,0,n){
IL(j,0,m){
if(vis[i][j]==0){
grid[arr[i][j]]=bfs({i,j});
}
}
}
int high=INT_MIN;
int strength;
for(map<int,int>::iterator it=grid.begin() ; it!=grid.end() ; ++it){
if(it->second>high){
high=it->second;
strength=it->first;
}
}
cout << strength << " " << high << endl ;
return
Im using a map to store these values…it would have been already sorted by their strength and thus wil automatically satisfy this condition.
this test case fails
5 5
1 1 5 1 1
1 2 2 2 1
3 2 1 1 4
1 2 1 2 1
1 1 6 1 1
Since you are using a map, some values are overridden…
ohh thanks…using multimap worked… 
Thanks man helped me a lot!!!