P1514 water diversion into the city, p1514 Water Diversion
Description
In a distant country, one side is a beautiful lake with great scenery, and the other side is a desert without any limitations. The country's administrative divisions are very special. They form a rectangle with N rows and M columns, as shown in. Each grid represents a city, with each city having an altitude.
Water conservancy facilities are now needed in some cities to allow residents to drink clear water as much as possible. There are two types of water conservancy facilities: water storage plants and water stations. The function of a water storage plant is to use a water pump to extract water from a lake to a reservoir in a city.
Therefore, water storage plants can be built in only 1st cities adjacent to lakes. The function of the water delivery station is to transport the lake water from the height to the lower place by using the height gap of the water transmission line. Therefore, the premise that a city can build a water supply station is that there are adjacent cities with a higher altitude and a public edge, and water conservancy facilities have been built. Because the city in line N is near the desert, it is the arid area of the country, so each city must have water conservancy facilities. So can this requirement be met? If yes, calculate the minimum number of water storage plants. If not, find the number of cities in the arid area where water conservancy facilities cannot be built.
Input/Output Format
Input Format:
Two numbers in each line of the input file are separated by a space. The first line of the input is two positive integers N and M, indicating the size of the rectangle. Next N rows, each row has M positive integers, which represent the altitude of each city in turn.
Output Format:
The output has two rows. If the requirements are met, the first line of output is integer 1, and the second line is an integer, which indicates that at least several water storage plants are built. If the requirements cannot be met, the first line of the output is an integer of 0, and the second line is an integer, which indicates that water conservancy facilities cannot be built in cities in several dry regions.
Input and Output sample input sample #1:
[Input Example 1] 2 59 1 5 4 38 7 6 1 2 [input Example 2] 3 68 4 5 6 4 47 3 4 3 3 33 2 2 1 2 2
Output sample #1:
[Output Example 1] 11 [Output Example 2] 13
Description
[Example 1]
You only need to build a water storage plant in the city at an altitude of 9 to meet the requirements.
[Example 2]
The construction of water storage plants in three cities with crude wireframes can meet the requirements. Taking the three water storage plants as the source
The water delivery stations built in the arid area are marked in three colors respectively. Of course, the construction method may not be unique.
[Data Scope]
DFS + line segment coverage
Note: I don't know why I need to run the DFS first to check whether it can be completely overwritten.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <algorithm> 7 using namespace std; 8 const int MAXN = 1001; 9 void read (int & n) 10 {11 char c = '+'; int x = 0; bool flag = 0; 12 while (c <'0' | c> '9') 13 {c = getchar (); if (c = '-') flag = 1 ;} 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 struct node 21 {22 int l, r, id; 23} xushui [MAXN]; 24 int xx [5] = {-1, +, 0}; 25 int yy [5] = {,-1, + 1}; 26 int vis [MAXN]; 27 int vis2 [MAXN] [MAXN]; 28 int happen = 0; 29 void pdcan (int x, int y) 30 {31 if (x = n) 32 happen ++; 33 vis2 [x] [y] = 1; 34 for (int I = 0; I <4; I ++) 35 {36 int wx = x + xx [I]; 37 int wy = y + yy [I]; 38 if (vis2 [wx] [wy] = 0 & map [wx] [wy] <map [x] [y] & Wx> = 1 & wy> = 1 & wx <= n & wy <= m) 39 pdcan (wx, wy ); 40} 41} 42 void dfs (int num, int x, int y) 43 {44 if (x = n) 45 {46 xushui [num]. l = min (xushui [num]. l, y); 47 xushui [num]. r = max (xushui [num]. r, y); 48 vis [y] = 1; 49} 50 for (int I = 0; I <4; I ++) 51 {52 int wx = x + xx [I]; 53 int wy = y + yy [I]; 54 if (map [wx] [wy] <map [x] [y] & wx> = 1 & wy> = 1 & wx <= n & wy <= m) 55 dfs (num, wx, wy); 56} 57} 58 int cannot = 0; 59 int comp (const node & A, const node & B) 60 {61 if (. l = B. l) 62 return. r> B. r; 63 return. l <B. l; 64} 65 int main () 66 {67 freopen ("flow. in "," r ", stdin); 68 freopen (" flow. out "," w ", stdout); 69 read (n); read (m); 70 for (int I = 1; I <= m; I ++) 71 {72 xushui [I]. l = 0x7ffff; 73 xushui [I]. r =-1; 74} 75 for (int I = 1; I <= n; I ++) 76 for (int j = 1; j <= m; j ++) 77 read (map [I] [j]); 78 for (int I = 1; I <= m; I ++) 79 if (vis2 [1] [I] = 0) 80 pdcan (1, I ); 81 if (happen! = M) 82 {83 printf ("0 \ n % d", m-happen); 84 return 0; 85} 86 for (int I = 1; I <= m; I ++) 87 {88 xushui [I]. id = I; 89 dfs (I, 1, I); 90} 91 for (int I = 1; I <= m; I ++) 92 if (vis [I] = 0) 93 cannot ++; 94 if (cannot) 95 {96 printf ("0 \ n % d", cannot ); 97 return 0; 98} 99 int to = 0; // up to 100 int now = 0; 101 int ans = 0; // a few points are required: 102 for (int I = 1; I <= m; I ++) 103 {104 if (xushui [I]. l> 1001) continue; 105 if (now + 1> = xushui [I]. l) 106 to = Max (to, xushui [I]. r); 107 else 108 {109 now = to; 110 to = max (to, xushui [I]. r); 111 ans ++; 112} 113} 114 if (now! = M) 115 printf ("1 \ n % d", ans + 1); 116 else117 printf ("1 \ n % d", ans); 118 return 0; 119}