# Please Help: Recursion and Array Problem (Noob)

The explanation of what I’m trying to do are noted in the comments…
I’m freakin’ lost… the really big issue is determining how to test
mines that have been blown up against remaining
mines… you’ll find that comment smack dab in the middle…

today is the first day I’ve ever used Ruby, I appreciate the help:

## Load data from file into Python

mines_data = file.open(‘example_mines.txt’, ‘r’)

## function to determine if a mine blows up another mine

def within_blast_radius(x1, y1, blast_radius, x2, y2)
((x1 - x2) ** 2 + (y1 - y2) ** 2) <= (blast_radius ** 2)
end

## define variables to keep track of outputs

qty_mines_blown = 0
max_mines_blown = 0

## define a multidimensional array with x number of rows and 3 columns

master_array = array.new® {Array.new(3)}
cnt = 0
starting_mine = master_array [cnt]

# For Each Mine do this:

master_array.each = {|r|
# within_blast_radius(x1, y1, bomb_radius, x2, y2) ## Tests to see
if bomb x1, y1 blows up bomb x2, y2
master_array.each{|r|
if within_blast_radius(master_array[cnt][1], master_array[cnt][2],
master_array[cnt][3], master_array[r][1], master_array[r][2]) = True
qty_mines_blown += 1
## code to test mines that have been blown up against remaining
mines
endif
}
if qty_mines_blown > max_mines_blown
max_mines_blown = qty_mines_blown
answers_array.delete
answers_array[0] = starting_mine
elseif qty_mines_blown = max_mines_blown
answers_array.next_line = starting_mine
endif
cnt += 1
}
print "The maximum number of mines that can be blown up from triggering
just one mine is: "
print max_mines_blown
print “The location and blast radius of mine(s) that can trigger the
blast is :”
print elements in answers_array

On Wed, May 25, 2011 at 8:08 AM, James K. [email protected] wrote:

The explanation of what I’m trying to do are noted in the comments…
I’m freakin’ lost… the really big issue is determining how to test
mines that have been blown up against remaining
mines… you’ll find that comment smack dab in the middle…

today is the first day I’ve ever used Ruby, I appreciate the help:

## Load data from file into Python

mines_data = file.open(‘example_mines.txt’, ‘r’)

You are not loading data here, you are merely opening a file for
reading - and then do nothing with it. Did you mean any of these?

mines_data = File.read ‘example_mines.txt’
mines_data = File.readlines ‘example_mines.txt’

master_array = array.new(r) {Array.new(3)}
Spelling error. Also, in the comment you write “x” but in the code it’s
“r”.

cnt = 0
starting_mine = master_array [cnt]

# For Each Mine do this:

master_array.each = {|r|

# within_blast_radius(x1, y1, bomb_radius, x2, y2) ## Tests to see

if bomb x1, y1 blows up bomb x2, y2
master_array.each{|r|

The “r” used here shadows the “r” used in the outer #each block. This
is likely an issue.

answers_array[0] = starting_mine
print elements in answers_array
If I understand your problem properly basically this is what you want
to do: for all possible locations of a bomb in the grid check how many
mines can be blown up from here including mines which are blown by
other mines. Determine the maximum number of mines blown up.

Going at this top down (i.e. from the most abstract to the more
detailed) a simple implementation would look like this

grid = Array.new(rows) { Array.new(columns) }

# fill grid

max_blown = 0

grid.each_with_index do |row, row_idx|
row.size.times do |col_idx|
blows = calc_blows(row_idx, col_idx)
max_blown = blows if blows > max_blown
end
end

printf “Max number of blows in grid: %d\n”, max_blown

Now we need to determine the number of blows. We start from the
center (i.e. a possible bomb position) and increase the radius until
we find no more explosions:

def calc_blows(x, y)
blown = [[x,y]] # init with the bomb
radius = 0

# anything which gets blown

begin
radius += 1
current = find_blown(x, y, radius, blown)
sum.concat current
end until current.empty?

blown.size
end

Now you only need to implement find_blown() - and loading of the grid
from the file.

It seems this straightforward approach does several calculations
multiple times. I guess if we look long enough on the problem we will
find a solution which is more efficient. This sounds like a candidate
for dynamic programming but I don’t have the time right now.

Kind regards

robert

Thanks for your advice! I solved the issue and pretty much gutted the
logic.

this thread can be closed if someone can do so