Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

I need help with a problem I have to solve

This is the descrioption

Here is my solution

I'll try to explain my code:

First I'm reading the matrix from the console I know it can be mutch better and I have a few idias but thats not the point. After that I'm comparing the matrix element inside the two nested loops in checkBombFields. The idia here is to check all 8 elements around the selected element (like a cube, the selected element is in the middle) and skip the out of range exceptions. If there is a match do the same for the matching element with recursion and so one as I'm skiping the previous element because that's going to lead to infinite loop. At the end of the loop I set the matching elements as "o" so I can skip them next time. If there are no matches for that element I set it to "o" and ineacres the moves with one and here comes the problem.

The problem:

The problem seems to be with the recursion. For example:

If i have this matrix as input:

GGGB
GRGG
RGRB
RRRB

the program indicates only 3 moves while they should be 4 becous the last element on the first row is not connected to any other elements (the one market in red).

But if I change the matrix to:

BGGB
GRGG
RGRB
RRRB

now the program indicates 5 moves which is correct. So I'm thinking that there is a problem witht he recursion. Somewhere somehow the [0][3] elemten gets market as "o" in the previous example.

Also if I change the matrix to:

GGGB
GRGG
RGRB
BRRB

the program still indicates 3 moves when they should be 5.

About me and this problem:

I'm a student in a local academy. We haven't got to methods, recursia, matrices and exception handling in the academy yet. They are in the next modul. So I had to learn them today. This is the third problem out of three that I have to solve in order to get in a better academy if I pass the technical interview. I guess this can be solved in another way but i need to use my solution so I can explain it on the technical interview.

Also I need to solve this asap so I can move to prepare for an entrance exam for another academy. The plan is to get in both at the same time. I hope someone reads this and helps me. Thanks  🤝

Oh, I also wrote the same solution in C# - same problem.
I solved it without recursion.

https://pastebin.com/aXBvphFt


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by MelonbreadHFNov 4, 2018 01:44 AM 0 160 Nov 4, 2018 01:44 AM
Last Post: MelonbreadHF
[.NET] by iNCEPTiON_VApr 18, 2017 05:54 AM 3 213 Apr 18, 2017 05:54 AM
Last Post: iNCEPTiON_V
[Java] by AstroJackMar 6, 2015 06:53 AM 34 930 Mar 6, 2015 06:53 AM
Last Post: AstroJack