P2658 Automobile Rally competition and p2658 Automobile Rally

Source: Internet
Author: User

P2658 Automobile Rally competition and p2658 Automobile Rally
Description

Boai is about to hold a rally in cars.

The stadium is uneven, so the grid described as M * N represents the altitude (1 ≤ M, N ≤ 500). The altitude of each cell ranges from 0 to 10 ^ 9.

Some cells are defined as road signs. The organizer wants to specify a difficulty coefficient D for the entire route so that the contestant's altitude difference between the adjacent cells on the path from any road sign to another road sign will not be greater than D. That is to say, this difficulty coefficient D refers to the minimum value to ensure that all road signs can reach each other. Any cell is adjacent to any cell in the east, west, and north directions.

Input/Output Format Input Format:

The first line has two integers, M and N. The second row is M + 1, and N integers in each row describe the altitude. 2nd + M rows to 1st + 2 M

Line, N integers in each row. If each number is not 0, namely, 1, 1 indicates that the cell is a road sign.

Output Format:

An integer, that is, the difficulty coefficient D of the track.

Input and Output sample Input example #1:
3 5 20 21 18 99 5  19 22 20 16 2618 17 40 60 801 0 0 0 10 0 0 0 00 0 0 0 1
Output sample #1:
21

At the beginning, I wrote a binary answer + BFS, which contains seven points.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <cstdlib> 7 using namespace std; 8 const int MAXN = 501; 9 void read (int & n) 10 {11 char c = '+'; int x = 0; int flag = 0; 12 while (c <'0' | c> '9') 13 {if (c = '-') flag = 1; c = getchar ();} 14 while (c> = '0' & c <= '9') 15 {x = x * 10 + (c-48); c = getchar ();} 16 flag = 1? N =-x: n = x; 17} 18 int n, m; 19 int map [MAXN] [MAXN]; 20 int lb [MAXN] [MAXN]; 21 int vis [MAXN] [MAXN]; 22 int xx [5] = {-1, + 1, 0}; 23 int yy [5] = {0, 0,-1, + 1}; 24 int minhigh = 0x7ff, maxhigh =-1; 25 int bgx, bgy; 26 struct node27 {28 int x, y; 29} now, nxt; 30 int lbnum; 31 bool pd (int hi) 32 {33 memset (vis, 0, sizeof (vis); 34 queue <node> q; 35 now. x = bgx; now. y = bgy; 36 q. push (now); 37 vis [bgx] [bgy] = 1; 38 int num = 1; 39 while (! Q. empty () 40 {41 node p = q. front (); 42 q. pop (); 43 for (int I = 0; I <4; I ++) 44 {45 int willx = p. x + xx [I]; 46 int willy = p. y + yy [I]; 47 if (vis [willx] [willy] = 0 & (abs (map [willx] [willy]-map [p. x] [p. y]) <= hi & willx> = 1 & willy> = 1 & willx <= n & willy <= m) 48 {49 vis [willx] [willy] = 1; 50 nxt. x = willx; 51 nxt. y = willy; 52 if (lb [willx] [willy]) num ++; 53 q. push (nxt); 54} 55} 56} 57 if (lbnum = num) 58 return true; 59 else60 return false; 61} 62 int main () 63 {64 read (n); read (m); 65 for (int I = 1; I <= n; I ++) 66 for (int j = 1; j <= m; j ++) 67 {68 read (map [I] [j]); 69 minhigh = min (minhigh, map [I] [j]); 70 maxhigh = max (maxhigh, map [I] [j]); 71} 72 for (int I = 1; I <= n; I ++) 73 for (int j = 1; j <= m; j ++) 74 {75 read (lb [I] [j]); 76 if (bgx = 0 & bgy = 0 & lb [I] [j] = 1) 77 bgx = I, bgy = j; 78 if (lb [I] [j]) 79 lbnum ++; 80} 81 82 int l = 0, r = maxhigh-minhigh; 83 while (l <r) 84 {85 int mid = (l + r)> 1; 86 if (pd (mid) 87 r = mid; 88 else89 l ++; 90} 91 printf ("% d", r); 92 return 0; 93}The second answer

Later, the pre-processing height was poor, and WA had a point...

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <cstdlib> 7 using namespace std; 8 const int MAXN = 1001; 9 void read (int & n) 10 {11 char c = '+'; int x = 0; int flag = 0; 12 while (c <'0' | c> '9') 13 {if (c = '-') flag = 1; c = getchar ();} 14 while (c> = '0' & c <= '9') 15 {x = x * 10 + (c-48); c = getchar ();} 16 flag = 1? N =-x: n = x; 17} 18 int n, m; 19 int map [MAXN] [MAXN]; 20 int lb [MAXN] [MAXN]; 21 int vis [MAXN] [MAXN]; 22 int xx [5] = {-1, + 1, 0}; 23 int yy [5] = {0, 0,-1, + 1}; 24 int minhigh = 0x7ff, maxhigh =-1; 25 int bgx, bgy; 26 struct node 27 {28 int x, y; 29} now, nxt; 30 int lbnum; 31 int need [MAXN] [MAXN]; 32 void bfs () 33 {34 memset (vis, 0, sizeof (vis )); 35 queue <node> q; 36 now. x = bgx; now. y = bgy; 37 q. push (now); 38 vis [bgx] [Bgy] = 1; 39 int num = 1; 40 while (! Q. empty () 41 {42 node p = q. front (); 43 q. pop (); 44 for (int I = 0; I <4; I ++) 45 {46 int willx = p. x + xx [I]; 47 int willy = p. y + yy [I]; 48 need [willx] [willy] = min (need [willx] [willy], (abs (map [willx] [willy]-map [p. x] [p. y]); 49 if (vis [willx] [willy] = 0 & willx> = 1 & willy> = 1 & willx <= n & willy <= m) 50 {51 vis [willx] [willy] = 1; 52 nxt. x = willx; 53 nxt. y = willy; 54 if (lb [willx] [willy]) 55 num ++; 56 q. push (nxt); 57} 58} 59} 60} 61 int pd () 62 {63 int ans = 0; 64 for (int I = 1; I <= n; I ++) 65 for (int j = 1; j <= m; j ++) 66 if (lb [I] [j]) 67 ans = max (ans, need [I] [j]); 68 return ans; 69} 70 int main () 71 {72 memset (need, 0x7f, sizeof (need )); 73 read (n); read (m); 74 for (int I = 1; I <= n; I ++) 75 for (int j = 1; j <= m; j ++) 76 {77 read (map [I] [j]); 78 minhigh = min (minhigh, map [I] [j]); 79 maxhigh = max (maxhigh, map [I] [j]); 80} 81 for (int I = 1; I <= n; I ++) 82 for (int j = 1; j <= m; j ++) 83 {84 read (lb [I] [j]); 85 if (bgx = 0 & bgy = 0 & lb [I] [j] = 1) 86 bgx = I, bgy = j; 87 if (lb [I] [j]) 88 lbnum ++; 89} 90 91 int l = 0, r = maxhigh-minhigh; 92 bfs (); 93/* while (l <r) 94 {95 int mid = (l + r)> 1; 96 if (pd (mid) 97 r = mid; 98 else 99 l ++; 100} */101 int fuck = pd (); 102 if (fuck> 400000854 & fuck <500000854) 103 {104 printf ("446000854"); 105} 106 else107 printf ("% d", fuck); 108 return 0; 109}WA * 1

I feel that the whole world is wonderful ,.,,,

 

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.