Possible Duplicate:
Algorithm to find the total number of connected sets in a matrix
THE QUESTION:
https://amazonindia.interviewstreet.com/challenges/dashboard/#problem/4fd6570bd51e1
My solution is not passing all test cases.But I am not able to point out the scenarios where
my code will fail.
Can anyone point out whats wrong with my code?
My algorithm is simple that when you find a 1 make the value of that position equal to a variable called set incremented by 1. Initially set will be 1.
Then checking the neighbors of that position and if any of them is 1 make it equal to set+1.
Repeat the same till you reach the right bottom of matrix. Now increment set and repeat the process till any 1 is left in the matrix(I am deciding this using boolean left).
MY SOLUTION
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
String no_of_tc;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
no_of_tc=br.readLine();
int[] result=new int[Integer.parseInt(no_of_tc)];
int mdim=0,maxset=0;
boolean left=false;
int i=0;
boolean first=true;
//boolean anyneighbour=false;
for(i=0;i<Integer.parseInt(no_of_tc);i++)
{
maxset=0;
left=false;
first=true;
mdim=Integer.parseInt(br.readLine());
int[][] matrix=new int[mdim][mdim];
String[] values=new String[mdim];
int valuecount=0,counter=0,countchar=0;
for(valuecount=0;valuecount<mdim;valuecount++)
{
countchar=0;
values[valuecount]=br.readLine();
for(counter=0;counter<mdim;counter++)
{
char temp=(values[valuecount].charAt(countchar));
countchar=countchar+2;
matrix[valuecount]counter]=Character.getNumericValue(temp);
}
}
int j=0,k=0,set=1;
while(j<mdim)
{
while(k<mdim)
{
if(first)
{
if(matrix[j][k]==1)
{
matrix[j][k]=set+1;
first=false;
maxset=set;
}
}
if(matrix[j][k]==set+1)
{
if((j-1>=0)&&(k+1<mdim)&&(matrix[j-1][k+1]==1))
{
matrix[j-1][k+1]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
if((j-1>=0)&&matrix[j-1][k]==1)
{
matrix[j-1][k]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
if((j-1>=0)&&(k-1>=0)&&matrix[j-1][k-1]==1)
{
matrix[j-1][k-1]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
if((k+1<mdim)&&matrix[j][k+1]==1)
{
matrix[j][k+1]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
if((k-1>=0)&&matrix[j][k-1]==1)
{
matrix[j][k-1]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
if((j+1<mdim)&& (k+1<mdim) && matrix[j+1][k+1]==1)
{
matrix[j+1][k+1]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
if((j+1<mdim)&&matrix[j+1][k]==1)
{
matrix[j+1][k]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
if((j+1<mdim)&&(k-1>=0)&&matrix[j+1][k-1]==1)
{
matrix[j+1][k-1]=set+1;
//anyneighbour=true;
//proceedwhole=proceedwhole+1;
}
// if(anyneighbour==false&&proceedwhole==1)
// {
// first=true;
// set=set+1;
//
// result[i]=result[i]+1;
// break;
// }
//
}
else
{
if(matrix[j][k]==1)
{
left=true;
}
}
k++;
}
k=0;
j++;
if(j==mdim)
{
if(left==true)
{
j=0;
first=true;
left=false;
set=set+1;
}
else
{
result[i]=maxset;
}
}
// if(anyneighbour==false&&left==true)
// {
// j=0;
// left=false;
//
// }
}
}
int counter2=0;
for(counter2=0;counter2<Integer.parseInt(no_of_tc);counter2++)
{
System.out.println(result[counter2]);
}
}
}
Here is one test case your code fails at:
There is only 1 component here.
Also, your algorithm is slow. At every row you’re resetting
jwhen there is a1not connected to the previous one. Try flood fill algorithm: http://en.wikipedia.org/wiki/Flood_fill