Why does this BFS solution work for this question about euclidean distance and what’s its complexity?

Given a matrix of 1s and 0s where 0 represents houses and 1 represents stores, find the square of minimum Euclidean distance of every house to nearest store. Return it as a vector of vector.

[[1, 1, 0],  [0, 0, 0],    [0, 0, 0]] 

should return

[[0, 0, 1],  [1, 1, 2],     [4, 4, 5]]  

Basic BFS: ( approach-1)

If there are N elements in the matrix, one could do BFS starting every house to solve this. The worst case complexity of this approach is: Worst number of BFSes * O(E+V), where E is the number of edges in the matrix and V number of nodes = N * O(N + 4N) as number of edges is a constant factor of 4N. Time complexity: O(N*N)

multi-source BFS (approach-2)

  • Initialize the queue with all nodes (i, j) where i is the row number and j is the column for all stores. For each node (i, j) in the queue also store its corresponding (si, sj) node for the store that gives it the minimum square of euclidean distance (call it sqDist). For each store node (i, j), (si, sj) would be same as (i, j)

  • Initialize the distance answer that we are looking for each house with -1

  • During multi-source BFS, for a node (i, j) and it’s best store (si, sj) in the queue, look at each of 8 adjacent nodes (since it’s euclidean distance, we need to consider all 8 nodes not only 4 of left, right, top, down). If the current distance value for any of those nodes (nexti, nextj) is > the sqDist if (si, sj) is chosen as the best store for (nexti, nextj), then

    • update distance of (nexti, nextj) with the lower value
    • add (nexti,nextj) along with its best store as (si, sj) to the queue
  • Keep doing this until queue is empty

My question:

1) why does this work? Since we’re looking at euclidean distance it’s possible that (nexti, nextj) may not be added to the queue (or have their distance updated) for a certain (si, sj) but that (si, sj) node may still update the distance for nodes that are farther to it than (nexti, nextj) due to nature of euclidean distance. So (si, sj) may still need to be in the queue. How does this solution work inspite of this issue or what’s the intuitive conceptual idea of why this works despite this issue as it’s not obvious?

2) What’s the time complexity?

it seems in the worst case you could add a node in the queue N times. So it becomes N * O(N *4N) where 4N is the number of edges in the matrix. So is it not any better than doing BFS individually for each house?

How does the reaction timing work for Wrath of the Storm? Can it potentially prevent the damage from the triggering attack?

With attacks of opportunity, the PHB is pretty clear that your reaction occurs as an interruption to your opponent’s move, just before they move beyond your reach, then they resume their turn. Fair enough.

However, there are some abilities that happen during an opponent’s attack – for example, the Shield spell, and a Tempest cleric’s Wrath of the Storm ability. These both say they happen when you’re “hit” by an attack – but the Shield spell raises your AC, so I assume that occurs before rolling damage, and makes it possible for you to be retroactively not hit by the attack. Is the same true for Wrath – that your reaction might KO your opponent, and you wouldn’t have to worry about taking damage?

(I was planning to include Hellish Rebuke in this question but I see it specifically refers to “being damaged”, so I assume that means it happens on your opponent’s turn but definitely after getting hit, meaning you can only cast it if you’re still conscious.)

Mechanics of a town where magic doesn’t work

I’m planning a campaign and I want to have a village where magic doesn’t work. You can’t cast spells, magical items lose their ability, things like that.

I am fairly new to the lore and mechanics of the D&D world so I don’t have a good grasp on how to do this. I could say the village is cursed but that opens up who and why questions I don’t want my adventurers distracted by. I want something more like, the village happens to lie in a pocket that is disconnected for the magic plane (if that’s a thing). How could that work?

How does the Skill Trick Extreme Leap work?

The skill trick “Extreme Leap” reads:

Extreme Leap [Movement] Your extraordinary leaping ability carries you over great distances. Prerequisite: Jump 5 ranks. Benefit: If you make a horizontal jump of at least 10 feet during your turn, you can spend a swift action to move an additional 10 feet on that turn. Complete Scoundrel, p. 86

So, if a character with a total move of 30 feet takes a move action to ty to leap over a chasm of 20 feet taking a 20 feet running start, does he…

a) …have to make a Jump check DC 10 and use the skill trick “Extreme Leap” to cover the distance over the chasm and has 10 feet of movement left on the other side.

b) …have to make a Jump check DC 20 to cover the distance and get a bonus move of 10 feet once he lands on the other side.

From the Name of the trick and the phrase “your extraodinary leaping abiltiy carries you over great distances” I always assumed the trick adds another 10 feet jumping distance, but rereading the skill I am not sure now.

How is Deadlands: Reloaded fanning supposed to work?

Deadlands: Reloaded rules say Fanning the Hammer works like Automatic Fire from Savage Worlds:

Assuming his six-gun is fully loaded, he may fire up to 6 shots in a single action as if taking the Automatic fire maneuver. Each shot suffers a -4 Shooting modifier [snip]

I think the rules mean to say the player who wants to fan the hammer gets to make six attack rolls. But this clearly conflicts Savage Worlds Automatic fire rules, where the weapon’s Rate of Fire means the amount of attack rolls to be made when firing the same amount of bullets squared:

The Rate of Fire is how many Shooting dice the character rolls while firing the weapon. [snip] Each die rolled for a fully-automatic weapon represents a number of actual bullets equal to its rate of fire. An Uzi with a Rate of Fire of 3, for example, uses 3 rounds of ammunition per shot (or 9 bullets if it fires with all 3 dice).

Six attack rolls would imply a Rate of Fire of six – that’s four times as fast as a gatling gun in terms of shots per turn, and seems to be quite overpowered – not to mention that the revolver only has six chambers, not 36. But that’s the only reasonable way I can think of: I would rather not roll ~2.45 dice (square root of six) to resolve the attack. How is this supposed to work?

(house rulings for resolving the fanning confusion are welcome, as long as they’re backed with either personal or documented experience)

How do spell slots and preparation work when multi-classing between cleric and bard

I’m planning to play a multi-class bard/cleric character, and I wanted to verify how spell preparation and slots work across the two classes, particularly since they use different approaches for casting.

  • Can spell slots from the the total number available from the Multiclass Caster table (PHB pg165) be used to cast either known Bard spells or prepared Cleric spells?
  • Is it correct that Cleric spells must still be prepared each day from the set of available Cleric spells for the character’s level?
  • Is it correct that Cleric spells must use Wis as the primary stat, and Bard spells must use Chr?

I’m sorry if this has already been asked before, I couldn’t find an answer on first look. Thanks.

Does Blood in the Water work if I score a critical hit on something immune to critical hits?

Blood in the Water (Tome of Battle, p. 86) states:

While you are in this stance, you whip yourself into a terrible rage. When you score a critical hit against an opponent, you enter a near frenzied state from the sight and smell of blood. You gain a +1 bonus on attack rolls and damage rolls. Because this is an untyped bonus, it stacks with each successful critical hit you score.

If you go more than 1 minute without achieving a critical hit, you no longer gain the benefit of the stance.

Does this work if I score a critical hit on something immune to critical hits?

The description doesn’t state that I have to damage the creature with a critical hit. This isn’t precision damage that can only be done to something that is deigned their Dex bonus.

Assign resources that each have a certain amount of work they can provide to tasks that require a certain amount of work

I’m attempting to do a hobby automation project and have come to a roadblock. I have a certain problem I need to solve, but can’t think of the solution nor what the name for the problem would be.

Say we have n tasks where each task requires $ x_i$ amount of work to be done and m resources where each resource can provide $ y_j$ work. The total amount of work required will equal the the total amount of work the resources can provide, i.e. $ \sum_{i=1}^{n} y_i = \sum_{j=1}^{m}x_j$ . For all j from 1 to m, $ y_j \in \left \{1, 2, 6, 12, 24\right \}$ and each $ x_i = a*1 +b *2 + c*6+d*12+e*24$ . I was looking at task assignment problems, but those seemed to be a bit overkill since they were concerned with optimization where I’m just simply trying to slot the correct blocks in the right place so that I don’t have tasks that are given too few or too many resources.

My current guess is that you can iterate over each task, and give it the largest resource available that doesn’t go over the amount of work that is left for the task to be completed. It’s almost analagous to filling a jar with different sized rocks; the best way is to start with the largest rocks and then go down in size from there, so that the smaller rocks fill in the space between the larger ones. Am I not taking something into consideration that complicates this problem further? I’m sorry if this is an obvious one, but I’m a hobbyist programmer and couldn’t think of the name of the problem or of a good set of keywords to google. The closest I could find is task assignment so far.

Does hiding variables work in terms of blocks or line-by-line?

Let’s assume I have the following code:

declaration of a declaration of b ..a..b {   ..a..b   declaration of a    declaration of b   ..a } 

In this code {} represent the inner block. declaration represents binding occurrence, and .. represent applied occurrence.

So my question is that is it legal to use the outer scope variables inside an inner scope and then redeclare them ? If so how does the hiding of outer variables system work (Does it start to work when it sees a redeclaration)? Thanks in advance.