P1332 and p1332

Source: Internet
Author: User

P1332 and p1332
Description

The arms of the Lich King have finally made a comeback. The bloody uploaders organized a pioneer army to fight against the arms of natural disasters on the Chinese continent of noventd, as well as all dead creatures. The bloody pioneer army isolated from the Alliance and tribe was quickly surrounded by the natural disasters army. Now they have to gather their main forces to resist the suppression of the natural disasters army. What's terrible is that some of them are infected with the plague of the dead. If they don't try to prevent the spread of the plague, they will soon be destroyed. The Lord abdisis has begun investigating the source of the plague. It turns out that a traitor has appeared inside the bloody pioneer army. This traitor has been relying on the natural disasters army, and wants to turn all the bloody pioneer army into a natural disasters army! Don't be surprised. You are the traitor. Complete the tasks assigned to you by The Lich King as soon as possible before your whereabouts are revealed.

The Legion is a matrix of N rows and M columns. Each unit is a member of a bloody pioneer army. People infected with plague spread around every hour until all of them are infected with the plague. You have mastered the location of the source of infection. The task is to calculate the time when the Lord of the bloody pioneer army was infected with the plague and report it to the Lich King, in order to carry out a targeted round of encirclement and suppression of the bloody pioneer army.

Input/Output Format Input Format:

Row 1st: Four integers, N, M, A, and B, indicating that the Legion matrix has N rows and M columns. There is A source of infection, and B is the number of lords in the bloody death squad.

Row A: each row has two integers, x and y, indicating that the infection source is in column y of row x.

Next row B: each row has two integers x and y, indicating that the Lord is located in column y of row x.

[Data scale]

1 <= M, N <= 500

1 <= A, B <= M * N

Output Format:

Row 1st to row B: an integer in each line, indicating the time when the Lord was infected with the plague. The output order is the same as the input order. If a person is located at the source of the infection, the time for his plague infection is 0.

Input and Output sample Input example #1:
5 4 2 31 15 43 35 32 4
Output sample #1:
313
Description

For example, it indicates the time when everyone is infected with the plague and the locations of the source and Lord.

 

The data on this question is poisonous ..

How to search times out ..

I looked at the problem and found that the problem was very concise.

First, the vis array is optimized.

Then optimized the storage leader's array ,,

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 using namespace std; 7 const int MAXN = 1001; 8 void read (int & n) 9 {10 char c = '+'; int x = 0; bool flag = 0; 11 while (c <'0' | c> '9') 12 {13 c = getchar (); 14 if (c = '-') flag = 1; 15} 16 while (c> = '0' & c <= '9') 17 x = x * 10 + c-48, c = getchar (); 18 flag = 1? N =-x: n = x; 19 20} 21 int n, m, gr, pep, x, y; 22 int out [MAXN * 100]; 23 int map [MAXN] [MAXN]; 24 struct node25 {26 int x, y, step; 27} now, nxt; 28 int willx [MAXN], willy [MAXN]; 29 int xx [7] = {-1, + 1, 0}; 30 int yy [7] = {0,-1, + 1 }; 31 int jzx [MAXN * 100], jzy [MAXN * 100]; 32 queue <node> q; 33 void bfs () 34 {35 while (q. size ()! = 0) 36 {37 node p = q. front (); 38 q. pop (); 39 for (int I = 0; I <4; I ++) 40 {41 nxt. x = p. x + xx [I]; 42 nxt. y = p. y + yy [I]; 43 nxt. step = p. step + 1; 44 if (nxt. x> = 1 & nxt. y> = 1 & nxt. x <= n & nxt. y <= m) 45 {46 if (map [nxt. x] [nxt. y]! =-1) continue; 47 map [nxt. x] [nxt. y] = p. step + 1; 48 q. push (nxt); 49} 50} 51} 52} 53 int main () 54 {55 memset (map,-1, sizeof (map); 56 read (n ); read (m); read (gr); read (pep); 57 for (int I = 1; I <= gr; I ++) 58 {59 read (x ); read (y); 60 map [x] [y] = 0; // source of infection 61 now. x = x; 62 now. y = y; 63 now. step = 0; 64 q. push (now); 65} 66 for (int I = 1; I <= pep; I ++) 67 {68 read (x); read (y ); 69 jzx [I] = x; 70 jzy [I] = y; 71} 72 bfs (); 73 for (int I = 1; I <= pep; I ++) 74 {75 printf ("% d \ n", map [jzx [I] [jzy [I]); 76} 77 return 0; 78}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.