Incompatible Crops


This is a companion discussion topic for the original entry at https://toph.co/p/incompatible-crops

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 :frowning_face:

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

I think Toph should look into this :face_with_monocle:

1 Like

@hjr265 Please look into this :slight_smile:

@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 :wink:

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. :blush::blush::blush::yum:

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:
length, breadth = map(int, input().split())
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)

Please format your code first using the method I specified here.
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