Feb 24, 2018 10:12 PM
(This post was last modified: Feb 24, 2018 10:32 PM by LoGiurato.)
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.
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.

