I have this problem that I need to solve in the most efficient way. I have a 2d array that contains the following: All that is 1 is a “wall”, which means you cannot go through it. 2 is the input where you "enter" the array or map, if you want. 3 is what we need to find. Here is an example map:
1111111 1 3131 2 11111 1 31 1111111
This might be an example of an array that I need to look into. As you can see, there are 3 that are “inaccessible, since they are surrounded by a wall of“ 1. ”This means that there are two numbers available in this array.
First we need to find the entrance. Since the input can be anywhere, I need to search the entire array. I have done the following:
int treasureAmount = 0; Point entrance = new Point(0,0); for (int i = 0; i < N; i++) { for (int j = 0; j < N; i++){ if(map[i][j] == 2){ entrance.x =i; entrance.y =j; } }
It takes O (n ^ 2) time, and I see no other way to do this, since the input can be anywhere. However, I am not sure how to quickly and quickly find the available rooms. I was thinking that when searching for arrays to enter, at the same time I will find all the number 3 in the array, although some may not be available, and after that I am not quite sure how to efficiently find available ones.
source share