Please Help: Recursion and Array Problem (Noob)

J

James K.

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(r) {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
 
R

Robert Klemme

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 =3D 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 =3D File.read 'example_mines.txt'
mines_data =3D File.readlines 'example_mines.txt'
## function to determine if a mine blows up another mine
def within_blast_radius(x1, y1, blast_radius, x2, y2)
=A0((x1 - x2) ** 2 + (y1 - y2) ** 2) <=3D (blast_radius ** 2)
end

## define variables to keep track of outputs
qty_mines_blown =3D 0
max_mines_blown =3D 0

## define a multidimensional =A0array with x number of rows and 3 columns
master_array =3D array.new(r) {Array.new(3)}

Spelling error. Also, in the comment you write "x" but in the code it's "r=
".
cnt =3D 0
starting_mine =3D master_array [cnt]
# For Each Mine do this:
master_array.each =3D {|r|
=A0 =A0# within_blast_radius(x1, y1, bomb_radius, x2, y2) =A0## Tests to = see
if bomb x1, y1 blows up bomb x2, y2
=A0 =A0master_array.each{|r|

The "r" used here shadows the "r" used in the outer #each block. This
is likely an issue.
=A0 =A0if within_blast_radius(master_array[cnt][1], master_array[cnt][2],
master_array[cnt][3], master_array[r][1], master_array[r][2]) =3D True
=A0 =A0 =A0 =A0qty_mines_blown +=3D 1
=A0 =A0 =A0 =A0## code to test mines that have been blown up against rema= ining
mines
=A0 =A0endif
=A0 =A0}
=A0 =A0if qty_mines_blown > max_mines_blown
=A0 =A0 =A0 =A0max_mines_blown =3D qty_mines_blown
=A0 =A0 =A0 =A0answers_array.delete
=A0 =A0 =A0 =A0answers_array[0] =3D starting_mine
=A0 =A0elseif qty_mines_blown =3D max_mines_blown
=A0 =A0 =A0 =A0answers_array.next_line =3D starting_mine
=A0 =A0endif
=A0 =A0cnt +=3D 1
=A0 =A0}
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

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 =3D Array.new(rows) { Array.new(columns) }
# fill grid

max_blown =3D 0

grid.each_with_index do |row, row_idx|
row.size.times do |col_idx|
blows =3D calc_blows(row_idx, col_idx)
max_blown =3D 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 =3D [[x,y]] # init with the bomb
radius =3D 0

# increase the radius until we do not find
# anything which gets blown
begin
radius +=3D 1
current =3D 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

James K.

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

this thread can be closed if someone can do so
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top