Codevs 1066 Diversion into the city

Source: Internet
Author: User

My mom, this card has me for two days.

In fact very simple, on each starting point down the BFS, control the continuous section of the arid zone. It is then converted to a DP problem that covers all areas with the fewest segments.

However?!! I didn't know how to write a magical greed at first. The miserable world.

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace Std;
const int maxn=505;
struct SEG
{
int left,right;
}S[MAXN*3];
int map[maxn][maxn],n,m;
int DX[]={0,0,1,0,-1},DY[]={0,-1,0,1,0},CNT=0,DP[MAXN];
BOOL VIS[MAXN][MAXN],K[MAXN];
Queue <int> q;
BOOL CMP (SEG x,seg y)
{
if (x.left==y.left)
Return x.right>y.right;
Return x.left<y.left;
}
BOOL Judge (int x,int y)
{
if ((x>=1) && (x<=n) && (y>=1) && (y<=m) && (Vis[x][y]==false))
return true;
return false;
}
void BFs (int x)
{
memset (vis,false,sizeof (VIS));
while (!q.empty ())
Q.pop ();
int minn=99999999,maxn=0;
if (n==1)
{
Minn=min (MINN,X);
Maxn=max (MAXN,X);
K[x]=true;
}
Vis[1][x]=true;
Q.push (x);
while (!q.empty ())
{
int Head=q.front ();
Q.pop ();
int nx,ny,tx,ty;
if (head%m==0) nx=head/m;
else nx=head/m+1;
if (head-nx*m==0) ny=m;
else ny=head-(nx-1) *m;
for (int i=1;i<=4;i++)
{
int tx=nx+dx[i],ty=ny+dy[i];
if ((Judge (Tx,ty) ==true) && (Map[nx][ny]>map[tx][ty]))
{
Q.push ((tx-1) *m+ty);
Vis[tx][ty]=true;
if (tx==n)
{
Minn=min (Minn,ty);
Maxn=max (Maxn,ty);
K[ty]=true;
}
}
}
}
cnt++;
S[cnt].left=minn;
S[CNT].RIGHT=MAXN;
}
int main ()
{
Memset (K,false,sizeof (k));
Memset (Dp,0x3f,sizeof (DP));
scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
scanf ("%d", &map[i][j]);
for (int i=1;i<=m;i++)
if ((Map[1][i]>=map[1][i-1]) && (map[1][i]>=map[1][i+1]))
BFS (i);
int num=0;
for (int i=1;i<=m;i++)
if (K[i]==false)
num++;
if (num>0)
{
printf ("0\n%d\n", num);
return 0;
}
Sort (s+1,s+cnt+1,cmp);
dp[0]=0;
for (int i=1;i<=m;i++)
for (int j=1;j<=cnt;j++)
if ((s[j].left<=i) && (s[j].right>=i))
Dp[i]=min (dp[i],dp[s[j].left-1]+1);
printf ("1\n%d\n", Dp[m]);
return 0;
}

Codevs 1066 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.