jzxx2860 water diversion into the city

Title Description In a distant country, there are beautiful lakes on one side and boundless deserts on the other. The ad...

Title Description

In a distant country, there are beautiful lakes on one side and boundless deserts on the other. The administrative division of the country is very special. It just forms a rectangle with N rows and M columns, as shown in the figure above. Each grid represents a city, and each city has an altitude.
In order for the residents to drink as much clear water as possible, water conservancy facilities should be built in some cities now. There are two kinds of water conservancy facilities: water storage plant and water transmission station. The function of the water storage plant is to pump water from the lake to the reservoir in the city. As a result, only the first row of cities adjacent to the lake can build water storage plants. The function of the water delivery station is to transport the lake water from high to low by using the height drop of the water delivery pipeline. Therefore, the premise for a city to build a water transmission station is that there are adjacent cities with higher elevation and public side, and water conservancy facilities have been built.
As the city in line N is close to the desert and is the arid area of the country, it is required that each of them has water conservancy facilities. So, can this requirement be met? If you can, please calculate the minimum number of water storage plants to be built; if you can't, please calculate the number of cities that can't have water conservancy facilities in the arid area.

input
Separate two numbers in each line of the input file with a space.
The first line of input is two positive integers, N and M, representing the size of the rectangle.
Next N lines, M positive integers in each line, represent the altitude of each city in turn.

output
The output has two lines. If the requirements can be met, the first line of output is integer 1, and the second line is an integer, representing the minimum number of water storage plants to be built; if the requirements can not be met, the first line of output is integer 0, and the second line is an integer, representing that there are several cities in the arid area that cannot be built with water conservancy facilities.

Example
Input 1
2 5
9 1 5 4 3 1
8 7 6 1 2
Output 1
1
Input 2
3 6
8 4 5 6 4 4
7 3 4 3 3 3
3 2 2 1 1 2
Output 2
1
3

Tips
[example 1 Description]
Just build a water storage plant in the city 9 above sea level to meet the requirements.

[example 2 Description]

In the above figure, water storage plants are built in three cities with thick wire frame, which can meet the requirements. The water delivery stations built in the arid area with these three water storage plants as the source are marked with three colors respectively. Of course, construction methods may not be unique.

Portal

Full mark code: const h:array[1..4,1..2] of longint=((1,0),(-1,0),(0,-1),(0,1)); var col,a:array[0..501,0..501] of longint; f:array[0..500] of longint; c:array[0..500,1..2] of longint; d:array[0..500000] of record x,y:longint;end; n,m,i,j,color:longint; function min(a,b:longint):longint; begin if a<b then exit(a) else exit(b); end; procedure Judge; var i,j:longint; procedure bfs(x,y:integer); var i,t,f:longint; begin t:=1;f:=1; d[t].x:=x;d[t].y:=y; col[x,y]:=color; repeat for i:=1 to 4 do if col[d[f].x+h[i,1],d[f].y+h[i,2]]=0 then if a[d[f].x+h[i,1],d[f].y+h[i,2]]<a[d[f].x,d[f].y] then begin inc(t); d[t].x:=d[f].x+h[i,1]; d[t].y:=d[f].y+h[i,2]; col[d[t].x,d[t].y]:=color; end; inc(f); until f>t; end; {end bfs} begin {judge} for i:=0 to n+1 do begin a[i,0]:=maxlongint; a[i,m+1]:=maxlongint; end; for i:=0 to m+1 do begin a[0,i]:=maxlongint; a[n+1,i]:=maxlongint; end; for color:=1 to m do bfs(1,color); j:=0; for i:=1 to m do if col[n,i]=0 then inc(j); if j>0 then begin writeln(0); writeln(j); halt; end; end; procedure floodfill; procedure bfs(x,y:integer); var i,t,f:longint; begin t:=1;f:=1; d[t].x:=x;d[t].y:=y; col[x,y]:=color; repeat for i:=1 to 4 do if col[d[f].x+h[i,1],d[f].y+h[i,2]]=0 then if a[d[f].x+h[i,1],d[f].y+h[i,2]]>a[d[f].x,d[f].y] then begin inc(t); d[t].x:=d[f].x+h[i,1]; d[t].y:=d[f].y+h[i,2]; col[d[t].x,d[t].y]:=color; end; inc(f); until f>t; end; var i,j:longint; begin for i:=0 to n+1 do begin a[i,0]:=0; a[i,m+1]:=0; end; for i:=0 to m+1 do begin a[0,i]:=0; a[n+1,i]:=0; end; fillchar(col,sizeof(col),0); for color:=1 to m do if col[n,color]=0 then bfs(n,color); for i:=1 to m do c[i,1]:=col[1,i]; fillchar(col,sizeof(col),0); for color:=m downto 1 do if col[n,color]=0 then bfs(n,color); for i:=1 to m do c[i,2]:=col[1,i]; end; procedure DP; var i,j:longint; begin f[0]:=0; for i:=1 to m do begin f[i]:=maxint; for j:=1 to m do if (c[j,2]>=i)and(c[j,1]<=i) then f[i]:=min(f[i],f[c[j,1]-1]+1); end; writeln(1); writeln(f[m]); end; BEGIN {main} read(n,m); for i:=1 to n do for j:=1 to m do read(a[i,j]); judge; floodfill; DP; END.

19 June 2020, 01:54 | Views: 4897

Add new comment

For adding a comment, please log in
or create account

0 comments