# Incompatible Crops

Did you know that certain crops cannot grow next to each other? Given the layout of a field as a gri…

If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

This problem has a bad dataset. My code checked if the safe place was at first a ‘free space’. The problem clearly specifies that only a free space can be safe to plant! But I got WA

So I stopped checking if the place was free space and only checked if there was any `*` around the candidate spaces and voila! AC

I think Toph should look into this

1 Like

@shantanu404 Thank you for pointing this out. I will look into it and get back to you.

@shantanu404 You are absolutely right.

The dataset has been updated and all submissions have been rejudged.

1 Like

You’re welcome

1 Like

@shantanu404 I have already solved the problem.
However, I am interested in your code.
Can I get both of your codes? (after Updating TC and before)
Can you please Pastebin them to me?

It was super fun to solve this problem.

2 Likes

Is it only who who is having issues with this problem.
I can’t point out my faults in the code and thus I need help.
here is my code:
field = []
free_spots = 0
for i in range(length):
row = input()
field.append(row)
for i in field:
for j in i:
# for the middle ones
if length > field.index(i) >= 1 and breadth > i.index(j) >= 1:
if i[i.index(j) - 1] == ‘.’ and i[i.index(j) + 1] == ‘.’ and field[field.index(i) + 1][i.index(j)] == ‘.’ and field[field.index(i) - 1][i.index(j)] == ‘.’:
free_spots += 1

``````    # for the top left corner
elif field.index(i) == 0 and i.index(j) == 0:
if i[i.index(j) + 1] == '.' and field[field.index(i) + 1][i.index(j)] == '.':
free_spots += 1

# for the bottom right corner
elif field.index(i) == length and i.index(j) == breadth:
if i[i.index(j) - 1] == '.' and field[field.index(i) - 1][i.index(j)] == '.':
free_spots += 1

# for the top right corner
if field.index(i) == 0 and i.index(j) == breadth:
if i[i.index(j) - 1] == '.' and field[field.index(i) + 1][i.index(j)] == '.':
free_spots += 1

# for the bottom left corner
if field.index(i) == length and i.index(j) == 0:
if i[i.index(j) + 1] == '.' and field[field.index(i) - 1][i.index(j)] == '.':
free_spots += 1

# for the top side
elif field.index(i) == 0 and breadth > i.index(j) >= 1:
if i[i.index(j) - 1] == '.' and i[i.index(j) + 1] == '.' and field[field.index(i) + 1][i.index(j)] == '.':
free_spots += 1

# for the bottom side
elif field.index(i) == length and 1 <= i.index(j) < breadth:
if i[i.index(j) - 1] == '.' and i[i.index(j) + 1] == '.' and field[field.index(i) - 1][i.index(j)] == '.':
free_spots += 1

# for the right side
elif length > field.index(i) >= 1 and i.index(j) == breadth:
if i[i.index(j) - 1] == '.' and field[field.index(i) + 1][i.index(j)] == '.' and field[field.index(i) - 1][i.index(j)] == '.':
free_spots += 1
``````

print(free_spots)

I am getting too many runtime errors.
You should check if your code runs properly before sharing to the community.

I know there is a problem with my code but I couldn’t pinpoint it. It’s showing index out of range in some problems thus making a runtime error. It would be a great help if you would hlp me figure out why I am getting index out of range.

``````  Scanner in=new Scanner(System.in);
int r,c;
int count=0;
char a[][]=new char[576][576];
r=in.nextInt();
c=in.nextInt();
for (int i = 0; i <r; i++) {
for (int j = 0; j <c; j++) {
a[i][j]=in.next().charAt(0);
}
}
for (int i = 0; i <r; i++) {
for (int j = 0; j <c; j++) {

if((1<=i)&&(i<r)&&(1<=j)&&(j<c)){
if(((a[i][j-1]!='*')) && ((a[i-1][j])!='*')&&((a[i][j+1])!='*') && ((a[i+1][j])!='*')){
count++;
}
}else if((i==0)&&(j==0)){
if((a[i][j+1]!='*')&&(a[i+1][j]!='*')){
count++;
}
}else if((i==0)&&(j==c)){
if((a[i][j-1]!='*')&&(a[i+1][j]!='*')){
count++;
}
}else if((i==r)&&(j==0)){
if((a[i-1][j]!='*')&&(a[i][j+1]!='*')){
count++;
}
}else if((i==r)&&(j==c)){
if((a[i-1][j]!='*')&&(a[i][j-1]!='*')){
count++;
}
}else if((i==0)&&(1<=j)&&(j<c)){
if((a[i][j-1]!='*')&&(a[i][j+1]!='*')&&(a[i+1][j]!='*')){
count++;
}
}else if((j==0)&&(1<=i)&&(i<r)){
if((a[i-1][j]!='*')&&(a[i+1][j]!='*')&&(a[i][j+1]!='*')){
count++;
}
}else if((i==r)&&(1<=j)&&(j<c)){
if((a[i][j-1]!='*')&&(a[i][j+1]!='*')&&(a[i-1][j]!='*')){
count++;
}
}else if((j==c)&&(1<=i)&&(i<r)){
if((a[i-1][j]!='*')&&(a[i+1][j]!='*')&&(a[i][j-1]!='*')){
count++;
}
}
}
}
System.out.println(count);
``````

please tell me the problem in this code . runtime error

What’s the problem with this code? It runs perfectly on my pc but here it is showing
a compilation error

``````package com.company;
import java.util.Arrays;
import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int c = sc.nextInt();

String upDownDots = ".".repeat(c+2);

String[] field;
field = new String[r + 2];
field[0] = upDownDots;
field[r+1] = upDownDots;

for (int i = 0; i < r; i++){
String sca = sc.next();
sca = '.' + sca + '.';
field[i+1] = sca;
}

int count = 0;

for (int i = 1; i < r + 1; i++){
for (int j = 1; j < c + 1; j++){
String arounds = field[i].charAt(j) + Character.toString(field[i-1].charAt(j)) + field[i + 1].charAt(j) + field[i].charAt(j - 1) + field[i].charAt(j + 1);
if (arounds.equals(".....")){
count += 1;
}
}
}
System.out.println(count);
}
}
``````

Compilation error

``````Solution.java:14: error: cannot find symbol
String upDownDots = ".".repeat(c+2);
^
symbol:   method repeat(int)
location: class String
1 error
``````

Hey brother I try this problem many times But faild. Can you please give me the solution of this problem or find out the error of my code.
here is my code :

``````#include <stdio.h>

// #include <math.h>

#include <string.h>

// #include <stdlib.h>

int main()

{

int row, col, count = 0;

char ar[25][25];

scanf("%d %d", &row, &col);

for (int i = 0; i < row; i++)

{

for (int j = 0; j < col; j++)

{

scanf("%c", &ar[i][j]);

}

// getchar();

// printf("\n");

}

// for (int i = 0; i < row; i++)

// {

//     for (int j = 0; j < col; j++)

//     {

//         printf("%c", &ar[i][j]);

//     }

//     // getchar();

//     printf("\n");

// }

for (int i = 0; i < row; i++)

{

for (int j = 0; j < col; j++)

{

if (ar[i][j] == '.')

{

if ((ar[i + 1][j] != '*') && (ar[i - 1][j] != '*') && (ar[i][j + 1] != '*') && (ar[i][j - 1] != '*'))

{

count++;

// printf("row = %d \n coloum =  %d  =  %c\n", i, j, ar[i][j+1]);

}

}

}

}

printf("%d\n", count);

return 0;

}
``````

A post was split to a new topic: Incompatible Crops Help

A post was merged into an existing topic: Why it is showing queued for long time?

Facing issue with the logic
Well, So I have coded a little bit different and added free spaces in right, left, up and below… here is my main logic…

``````freeSpace = 0

for l in range(1, len(ground)):
for l1 in range(1, (len(ground2[l])-1)):
if ground2[l][l1] == ".":
if (ground2[l][l1-1] and ground2[l][l1+1] and ground2[l-1][l1] and ground2[l+1][l1]) == '.':
freeSpace += 1
else:
l1 += 1
``````

Where the the lists is…

``````ground = [['.', '*', '*', '*', '*', '.', '.', '.'],['.', '*', '*', '*', '*', '*', '.', '.'],['.', '.', '.', '*', '.', '*', '*', '.'],['.', '.', '*', '.', '.', '.', '*', '.'],['.', '.', '*', '.', '*', '*', '.', '.'],['.', '.', '.', '*', '*', '.', '.', '.']]
ground2 = [['.', '.', '.', '.', '.', '.', '.', '.'],['.', '*', '*', '*', '*', '.', '.', '.'],['.', '*', '*', '*', '*', '*', '.', '.'],['.', '.', '.', '*', '.', '*', '*', '.'],['.', '.', '*', '.', '.', '.', '*', '.'],['.', '.', '*', '.', '*', '*', '.', '.'],['.', '.', '.', '*', '*', '.', '.', '.'],['.', '.', '.', '.', '.', '.', '.', '.']]

``````

Full Code

``````

x, y = 6, 6

ground = []

for i in range(x):
usr = input("")
ground.append(usr)

ground2 = []

# all list in for every abstract
for k in ground:
ground2.append(list(k))

ground2.insert(0, ['.', '.', '.', '.', '.', '.'])
ground2.insert((len(ground2)), ['.', '.', '.', '.', '.', '.'])
for o in range(len(ground2)):
ground2[o].insert(0, ".")
ground2[o].insert(len(ground2)+1, ".")

freeSpace = 0

for l in range(1, len(ground)):
for l1 in range(1, (len(ground2[l])-1)):
if ground2[l][l1] == ".":
if (ground2[l][l1-1] and ground2[l][l1+1] and ground2[l-1][l1] and ground2[l+1][l1]) == '.':
freeSpace += 1
else:
l1 += 1

print(freeSpace)

``````

**Output comes 7 while the output could be 3 **

@hjr265