#P1332. 血色先锋队

    ID: 329 远端评测题 1000ms 125MiB 尝试: 1 已通过: 1 难度: 5 上传者: 标签>模拟搜索福建省历届夏令营广度优先搜索,BFS剪枝

血色先锋队

Description

The legion is an nn by mm matrix, and each cell represents a member of the Scarlet Vanguard. An infected person spreads the plague to the four adjacent cells (up, down, left, right) every hour, until everyone is infected. You already know the positions of the infection sources. Your task is to compute the time when each of the Scarlet Vanguard’s lords becomes infected, and report it to the Lich King to enable a targeted strike against the Scarlet Vanguard.

Input Format

Line 11: Four integers nn, mm, aa, bb, meaning the legion matrix has nn rows and mm columns. There are aa infection sources, and bb is the number of lords in the Scarlet Vanguard.

The next aa lines: Each line has two integers xx, yy, indicating an infection source at row xx, column yy.

The next bb lines: Each line has two integers xx, yy, indicating a lord at row xx, column yy.

Output Format

Lines 11 to bb: Each line contains one integer, the time when that lord becomes infected. The output order matches the input order. If a person’s position is an infection source, then their infection time is 00.

5 4 2 3
1 1
5 4
3 3
5 3
2 4

3
1
3

Hint

Explanation for Sample 1

As shown below, the infection times for all people, and the positions of infection sources and lords, are marked.

Constraints

For 100%100\% of the testdata, it is guaranteed that 1n,m5001 \le n, m \le 500, 1a,b1051 \le a, b \le 10^5.

Translated by ChatGPT 5