每个蓄水厂覆盖的干旱城市是连续的
广搜+一些小处理
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return f*x;
}
const int N=600;
int go[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m,high[N][N],xi[N*N],yi[N*N],ans,lim;
bool f[N][N],water[N],flag[N];
struct watercity { int l,r; } a[N];
bool cmp(const watercity &a,const watercity &b)
{
return a.r>b.r;
}
bool ok(int x,int y)
{
if(x<1||x>n||y<1||y>m||f[x][y]) return 0;
return 1;
}
void bfs(int sx,int sy)
{
memset(f,0,sizeof(f));
xi[1]=sx; yi[1]=sy; f[sx][sy]=1;
int h=0,t=1,x,y,x1,y1;
while(h<t)
{
h++; x=xi[h]; y=yi[h];
if(x==n) water[y]=1;
for(int k=0;k<4;k++)
{
x1=x+go[k][0]; y1=y+go[k][1];
if(!ok(x1,y1)||high[x1][y1]>=high[x][y]) continue;
f[x1][y1]=1;
t++;
xi[t]=x1; yi[t]=y1;
if(x1==n)
{
a[sy].l=min(a[sy].l,y1);
a[sy].r=max(a[sy].r,y1);
}
}
}
}
int main()
{
n=read(); m=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) high[i][j]=read();
for(int i=1;i<=m;i++)
{
a[i].l=2147483647;
if(n==1) a[i].l=a[i].r=i;
}
for(int i=1;i<=m;i++) bfs(1,i);
for(int i=1;i<=m;i++) if(!water[i]) ans++;
sort(a+1,a+m+1,cmp);
if(ans) cout<<0<<"\n"<<ans;
else
{
lim=1;
while(lim<=m)
{
for(int i=1;i<=m;i++)
if(a[i].l<=lim&&!flag[i])
{
flag[i]=1;
ans++;
lim=a[i].r+1;
break;
}
}
cout<<1<<"\n"<<ans;
}
return 0;
}