The question is to have Cops and Robbers on a grid N x N. One cop can catch only 1 robber, and only if he is within the same row, and the distance between them should be less than s. My idea was to take each row, and check if a cop can catch. Once that happens I'll break, and move on to the next row. I don't know what is going wrong.
Edit : I realized I had not considered the existence of multiple police officers and thieves in the same row. I also should add that I did not get any compile errors. Only the output is wrong. And I don't know any other language except C. I have been coding only for a few months.
#include <stdio.h>
int main ()
{
int test, n, s ;
printf ("Type the number of test cases, size, and separation limit of Cop and Robber, separated by space.\n") ;
scanf ("%d %d %d", &test, &n, &s) ;
char grid [n][n] ;
for (int i = 0 ; i < test ; i ++)
{
printf ("Start filling the grid. Type P for Cop and T for Robber.\n") ;
for (int j = 0 ; j < n ; j ++)
for (int k = 0 ; k < n ; k ++)
scanf ("%1c", &grid [j][k]) ;
int check = 0, result = 0 ;
for (int j = 0 ; j < n ; j ++)
for (int k = 0 ; k < n ; k ++)
{
for (int l = k + 1 ; (l - k <= s) && (l < n) ; l ++)
{
if ((grid [j][k] == 'p' && grid [j][l] == 't') || (grid [j][k] == 't' && grid [j][l] == 'p'))
{
result ++ ;
check ++ ;
break ;
}
}
check = 0 ;
break ;
}
printf ("The number of robbers caught is : %d\n", result) ;
}
}