Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
We have an NxM grid, grid have one element named Bob. Bob can travel diagonally blocks only. The grid has some blocked blocks on which Bob can not travel. Write a function that returns on how many possible positions Bob can move. Solve this problem using BFS and submit the executable code in any programming language. In the following image example, Bob's positioning is at 9,3, and it can visit the places where Y is marked; hence your method should return 31

What I have tried:

used grid method ..
C#
public class Grid{

public static void main(String args[]){
    int[][] gridData = {
                {0,0,0,0,0,0,0,0},
                {0,1,0,0,0,1,0,0},
                {0,0,0,0,1,0,0,0},
                {0,0,0,0,0,0,0,0},
                {0,0,0,1,0,0,1,0},
                {0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,1,0},
                {0,0,1,0,0,1,0,0},
                {0,0,0,0,0,0,0,0},
                {0,0,0,2,1,0,0,0}};  //2 is bobs position, 1 is blocked, 0 can be visited
        
    Map grid = new Map(gridData);
    MapHelper solution = new MapHelper();
    System.out.println(solution.countReachableCells(grid));
Posted
Updated 13-Jan-23 4:53am
v5
Comments
Richard MacCutchan 13-Jan-23 7:28am    
What error? Please do not assume that we can see your screen, or read your mind.
[no name] 13-Jan-23 7:41am    
Could u please check this .Sorry i didn't put this up earlier.
Richard MacCutchan 13-Jan-23 8:03am    
Check what? You have not explained what the problem is.
[no name] 13-Jan-23 8:07am    
In the above question i have stated my code which earlier i havent dropped it.
The code that i have wrote should return 31 as answer but according to my code its showing error.So i want you to please look through it and correct me where i had done my coding wrong.
Richard MacCutchan 13-Jan-23 8:16am    
Most of the code is missing from your solution. Also what is the error you keep referring to?

We won't make your homework. Come on, you should learn how to do it.

Take a paper and a pencil, draw the grid, draw the 0's and 1's in that grid, understand what those 0's and 1's represent, then try to figure out (being the grid a 2 dimensions array) how would you have to loop across the grid / array to start adding and remembering the number of found positions with a 0, given the statement I guess bob can change directions...

Remembering positions is important if bob can jump inside them several times (check on the paper if that is important given your specific case).
Adding positions should be super easy.

Without any programming language, first try to write down what you must do, how you would do it manually.
Then try to make a flowchart (nothing fancy) just something that makes it easier to understand the problem and how you would solve it.
Then start translating all this to a small program.

Divide your problem in small fragments, try to understand the problems that appear and start searching for the solutions, in fact, you know the amount of columns and rows (limits / walls bob maybe can go through (that 31 will tell you this), you know this is a bi-dimensional array, then you have a bunch of 0's (free cells) and 1's (walls)...

FORGET about compiling, linking, execution errors now, you can solve this without touching a computer, as soon as your flowchart is working properly make the translation to code.

Hope this helps.
 
Share this answer
 
Quote:
but according to my code its showing error.

Reread your question.
Part of your code is still secret, and error description is secret too. This ensure that we can't compile or run your code to see what is the error message.

The word "Error" is not alone because there is more than 1 kind of error. The error message usually include the line number where error occured because an error can occur anywhere.
 
Share this answer
 
v2
Comments
[no name] 13-Jan-23 8:59am    
@Patrice T how to make it available for others so that u guys can see it..Just new to this site so could u please tell how to do
Patrice T 13-Jan-23 9:07am    
Use Improve question to update your question.
[no name] 13-Jan-23 9:13am    
I have just improved .IS it now available .can u see the questions and the program that i tried?
Patrice T 13-Jan-23 9:32am    
the code in question is not complete.
[no name] 13-Jan-23 9:34am    
Can u tell what to add? Where to add?
First study carefully the algorithm (a starter here: Breadth-first search - Wikipedia[^]), then try to implement it yourself.
Eventually, if you get stuck, ask here (maybe providing the detailed input example).
 
Share this answer
 
Comments
[no name] 13-Jan-23 7:38am    
I have provided the detail can u look onto it..
CPallini 13-Jan-23 7:48am    
Well, you skipped the first two steps...
[no name] 13-Jan-23 7:50am    
what steps can u please tell me and correct me and the code that i tried.i have been in this for a 3 hours and as a beginner its being more challenging for me.
Patrice T 15-Jan-23 6:44am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900