luogu P1514 引水入城
搜索+dp
// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxn = 607;
int n,m;
int map[maxn][maxn];
int fs[5] = {1,0,-1,0,1};
bool vis[maxn];
int l[maxn],r[maxn],dp[maxn];
bool V[maxn][maxn];
void dfs(int x,int y,int root) {
V[x][y] = 1;
if(x == n) {
vis[y] = 1;
l[root] = std::min (l[root],y); r[root] = std::max (r[root],y);
}
for(int i = 0;i < 4;++ i) {
int tx = x + fs[i],ty = y + fs[i + 1];
if(!V[tx][ty] && tx >= 1 && tx <= n && ty >= 1 && ty <= m && map[tx][ty] < map[x][y]) dfs(tx,ty,root);
}
}
int main() {
scanf("%d%d",&n,&m) ;
memset(l,0x3f,sizeof l);
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) {
dfs(1,i,i); memset(V,0,sizeof V);
} int num = 0 ;
for(int i = 1;i <= m;++ i) {
if(!vis[i]) num ++ ;
}
if(num) {puts("0"); printf("%d\n",num);return 0;}
memset(dp,0x3f,sizeof dp);
dp[0] = 0;
for(int i = 1;i <= m; ++i)
for (int j = 1;j <= r[i];j ++)
dp[j] = std::min(dp[j],dp[l[i] - 1] + 1);
puts("1");
printf("%d\n",dp[m]);
return 0;
}