luogu#P1332. 血色先锋队

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

血色先锋队

Background

The Lich King’s Scourge has returned in force. The Scarlet Crusade organized a vanguard to sail to Northrend to fight the Scourge and any creature tainted by undeath. Isolated from both the Alliance and the Horde, the Scarlet Vanguard was soon surrounded by the Scourge. They gathered their main forces to resist the encirclement. Terrifyingly, some of them have been infected by the undead plague. If the spread is not stopped, annihilation is imminent. Highlord Abbendis has begun investigating the source of the plague. It turns out there is a traitor within the Scarlet Vanguard who has defected to the Scourge, seeking to convert the entire Scarlet Vanguard into the Scourge! Do not be surprised—you are that traitor. Before your identity is exposed, you must quickly complete the task assigned to you by the Lich King.

Problem 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