[Community Puzzle] Pirate's treasure

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Hi all. I am trying to get 100% on this one, but even though I easily pass all test cases, I fail two of the Submit cases, the “Fully surrounded” and the “Large island”. This is no timeout error, there seems to be some error in my logic.
My language is JS and my logic is:
Store the island into a 2d array, then, starting from the top left cell all the way to the bottom-right, do this test for every cell (say coordinates i,j): set some flag to false, then start scanning every neighbor of i,j taking care to be within the bounds of the island and excluding i,j itself. If I find any such neighbor not marked as 1, set the flag to true, abort the search and move on. Otherwise I return i,j as the treasure cell.
Works like a charm in all 5 test cases. What is wrong then?

Test cases are indeed missing something crucial. I updated Test case 2 to reflect a challenge which were previously only in the validators. You should fail Test case 2 now. Good luck :wink:

1 Like

Thank you, the puzzle is now solved 100%. Your update indeed pointed me in the right direction.

D.

How come I pass 100% of the TC in the IDE and only 1 of them when I submit?
I can pm you my code if you want but I have no clue why I would pass all the TC and fail almost most of them when I submit.

Answered in PM.
Also:
There will only be one valid spot on the map where there is a treasure. Even if you looped every spot, there should only be one result. If you break early, you possibly find the correct spot in Test Cases, but not in the Validators.

Hello,

If it is not to much to ask, could someone please tell me why it is ignoring my if statement with all of the conditions? I know this is ugly but not really sure of another way to do it.

import java.util.*;
import java.io.*;
import java.math.*;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Solution {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int W = in.nextInt();
        int H = in.nextInt();
        if (in.hasNextLine()){
            in.nextLine();
        }
        String[] node = new String[H];
        for (int i = 0; i < H; i++){
            node[i] = in.nextLine();
        }
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (node[i].charAt(j) == '1'){
                
                    try{
                        if (node[i].charAt(j) == '0' && node[i + 1].charAt(j) == '0' || node[i - 1].charAt(j) == '0' || node[i + 1].charAt(j + 1) == '0' || 
                            node[i + 1].charAt(j - 1) == '0' || node[i - 1].charAt(j + 1) == '0' || node[i - 1].charAt(j - 1) == '0' || 
                            node[i].charAt(j + 1) == '0' && node[i].charAt(j - 1) == '0'){
                            break;  
                        }
                    }
                    catch(StringIndexOutOfBoundsException e){
                    continue;
                    }
                    catch(ArrayIndexOutOfBoundsException e){
                    continue;
                    }                    
                

                }
                if (node[i].charAt(j) == '0'){
                    out.printf(String.valueOf(j) + " " + String.valueOf(i));
                }  

                


            }
        }
        
    out.flush();
    }
}

when you have a statement like this:

A && B || C && D

Do you know which operator will be evaluated first?

2 Likes

I’m not sure this will be often true.

2 Likes

I believe the && operator is evaluated first. So in this instance A && B is first evaluated. I might be wrong, but I am just thinking that && is multiplicative and || is additive. I am basing this all off the order of operations for math. But again, I am still fairly new. Please correct me if I am wrong.

Advised to use parentheses to make your intention clear.
Some of the reasons:

  1. The precedence rule is long and complicated. Even if you can remember and master it, your reader cannot. Anyone who has to maintain your code will curse you - avoid it.
  2. The precedence rule of java is not guaranteed to be the same across all other languages. Some day if your code is ported to another languages, troubles come.
  3. Just to help yourself. Clear code reduces errors.
1 Like

Thank you!

bonjour,
le code passait 100% aux tests, mais 80% aux validateurs. j’ai fait plusieurs corrections sans pouvoir débloquer le problème. j’ai donc cherché un code dont l’algo me semblait identique sur le net dans un autre langage (javascript) pour passer les validateurs. Ça a fonctionné. Mais j’ai pris encore une bonne journée pour comprendre que de déclarer un mur englobant de 1s était sans doute l’origine. le code était plus simple avec ce mur, mais bon…

bref, j’ai écris un code qui finalement passe les validateurs, mais un autre que j’ai juste copié-collé. je me sens bête d’avoir soumis ce code. est-il possible de l’effacer et de décrémenter d’un point le succès javascript? merci.

I am completely blocked :crying_cat_face: (I use Javascript). I pass 100% of the tests but I only get validated by 20% by the IDE.
I did check that my code found only one correct answer. I can send my code by DM.
Could someone help me understand my mistake ?
Thanx :grinning:

Well, I have a code that seams to be logical, that succeed 100% of the test but not the IDE (for edge). I don’t get it and it’s not logical… What can I do to improve that?
I kind of start to be sad to code and to fail and to don’t understand what’s not good…
If someone have an idea that would be wonderful

It just took me an hour to get from 80% to 100% in the validators although all test cases went through correctly. In the end, i was missing to ensure that the spot i found itself was free (0).

So i would suggest to add/change a test case so that a completely surrounded 1 is included to catch this potential error.

Nice challenge though.

1 Like