Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on 27.1 Batch Processing: how to calculate minimum distance to a black square for all white squares in one BFS? #52

Closed
chao1995 opened this issue May 13, 2017 · 2 comments

Comments

@chao1995
Copy link

Hi @pllk, I really appreciate your huge efforts for writing this book and making it free and available. Thank you so much!

I have a question when I am reading 27.1 Batch Processing. The problem statement (not the latest version) is

Given a grid of size k by k initially consists of white squares and our task is to perform n operations, each of which is one of the following:

  • paint square (y,x) black

  • find the nearest black square to square (y,x) where the distance between
    squares (y1,x1) and (y2,x2) is |y1 - y2| + |x1 - x2|

There is a pre-processing step in each batch, to calculate for each square of the grid the smallest distance to a black square. This can be done in O(k^2) time using breadth-first search.

My question is: how is calculating for each square of the grid the smallest distance to a black square done in one BFS? (I can't figure out which square to start the BFS.)

Thanks in advance!

Btw, which is the better place, codeforces or this repo, to post questions when reading the book?

@pllk
Copy link
Owner

pllk commented May 13, 2017

Thank you! The trick is to start the search at each black square simultaneously. You can first add all black squares to the queue with distance 0, and then perform the search as usual.

Questions can be posted either here or on Codeforces. "Issues" are things that should be fixed, and usually questions point out such things because the book should be easy to understand.

@chao1995
Copy link
Author

The trick is to start the search at each black square simultaneously. You can first add all black squares to the queue with distance 0, and then perform the search as usual.

Oh I see! Thank you!!

@pllk pllk closed this as completed May 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants