Water diversion into the city

Source: Internet
Author: User

"Title description"

In a faraway country, one side is the beautiful lake, the other side is the rambling desert. The administrative divisions of the country are very special, forming a rectangle of n-Rows M-columns, as shown, each of which represents a city with an altitude of each city. In order for residents to drink as much water as possible in the clear waters, they now have to build irrigation facilities in some cities. There are two kinds of water conservancy facilities, namely water storage plant and conveyance station. The function of a storage plant is to pump water from a lake into a reservoir in the city in which it is used. As a result, only cities with a 1th row adjacent to the lake can build water storage plants. The function of the water-conveyance station is to transfer the lake from the high to the lower level by using the height drop of the pipe line. Therefore, a city can build a water station, the premise is that there is higher than its elevation and the public side of the adjacent cities, has built water conservancy facilities. As the nth row of cities is close to the desert, it is the arid zone of the country, requiring water conservancy facilities in each of the cities. So, can this requirement be met? If so, please calculate a minimum number of water storage plants and, if not, the numbers of cities in the arid zone that are unlikely to be able to build irrigation facilities.

"Input description"

The input is separated by a space between two numbers in each row. The first line of input is two positive integers n and m, representing the size of the rectangle. Next n rows, m positive integers per line, which in turn represent the altitude of each city.

"Output description"

The output has two lines. If the requirements are met, the first line of the output is an integer 1, the second line is an integer, representing a minimum of several water storage plants; If the requirement is not met, the first line of the output is an integer 0, and the second line is an integer, which means that there are few cities in the arid zone that cannot have irrigation facilities.

"Sample Input"

2 5

9 1 5) 4 3

8 7 6) 1 2

"Sample Output"

1

1

"Data range and Tips"

One example illustrates:

source code: #include<cstdio>#include<cstring>#include<algorithm>#defineINF 100000000using namespacestd;structnode{intx, y;} h[300001];structnode{intLeft,right;} f[501];intM,n,now,ans (0), num[501],i[501][501];Const intsum[4][2]={{0,1},{1,0},{0,-1},{-1,0}};BOOLf[501][501]={0};voidBFS ()//this konjac Konjac will finally BFS. {    intHead0), Tail (0);  for(intA=1; a<=m;a++) {f[1][a]=true; h[++tail].x=1; H[tail].y=A; }     while(head<tail) {Node T=h[++head];//the use of novel structural bodies.          for(intA=0;a<4; a++)//The lazy usage of this array should be worthy of reference. {Node s; S.x=t.x+sum[a][0]; S.y=t.y+sum[a][1]; if(s.x<1|| s.x>n| | s.y<1|| S.y>m)Continue; if(i[s.x][s.y]>=I[t.x][t.y])Continue; if(F[s.x][s.y])Continue; F[S.X][S.Y]=true; h[++tail]=s; }    }}voidDFS (intXintY//Dfs Find the smallest left and largest right intervals. {F[x][y]=true; if(x==N) {f[now].left=min (f[now].left,y); F[now].right=Max (f[now].right,y); }     for(intA=0;a<4; a++) {Node T; T.x=x+sum[a][0]; T.y=y+sum[a][1]; if(t.x<1|| t.x>n| | t.y<1|| T.y>m)Continue; if(i[t.x][t.y]>=I[x][y])Continue; if(!F[t.x][t.y])    DFS (T.X,T.Y); }}intMain () {scanf ("%d%d",&n,&m);  for(intA=1; a<=n;a++)       for(intb=1; b<=m;b++) scanf ("%d",&I[a][b]);    BFS ();  for(intA=1; a<=m;a++)      if(!F[n][a]) ans++; if(ans) {printf ("0\n%d", ans); return 0;//You can play like that. } printf ("1\n");  for(intA=1; a<=m;a++) {memset (F,0,sizeof(f));//a fresh start. Now=a;//the application of global variables. f[now].left=m+1; F[now].right=0; DFS (1, a); } num[0]=0;//Num[i] Represents the minimum number of segments required for 1~i.      for(intA=1; a<=m;a++)//The line segment overrides DP. {Num[a]=INF;  for(intb=1; b<=m;b++)          if(a>=f[b].left&&a<=f[b].right) Num[a]=min (num[a],num[f[b].left-1]+1); } printf ("%d", Num[m]); return 0;}

Water diversion into the city

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.