提交时间:2024-06-10 10:14:07
运行 ID: 150962
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cstdio> #include <cstdlib> #include <bits.stdc++.h> using namespace std; #define int long long #define INF 0x3f3f3f3f3f3f3f3f #define N (int)(1e3+15) int n,m; int dx[5]={0,0,1,-1}; int dy[5]={1,-1,0,0}; int vis[N][N],h[N][N],l[N][N],r[N][N]; bool safe(int x,int y) {return 1<=x&&x<=n&&1<=y&&y<=m;} void dfs(int x,int y) { vis[x][y]=1; for(int i=0; i<4; i++) { int tx=x+dx[i]; int ty=y+dy[i]; if(!safe(tx,ty)||h[tx][ty]>=h[x][y])continue; if(!vis[tx][ty])dfs(tx,ty); l[x][y]=min(l[x][y],l[tx][ty]); r[x][y]=max(r[x][y],r[tx][ty]); } } signed main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); // freopen("check.in","r",stdin); // freopen("check.out","w",stdout); memset(l,0x3f,sizeof(l)); cin >> n >> m; for(int i=1; i<=m; i++) l[n][i]=r[n][i]=i; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) cin >> h[i][j]; for(int i=1; i<=m; i++) if(!vis[1][i])dfs(1,i); bool ok=1;int res=0; for(int i=1; i<=m; i++) if(!vis[n][i]) ok=0;++res; if(!ok) { cout << "0\n" << res << '\n'; return 0; } int L=1,R=r[1][1]; while(L<=m) { for(int i=1; i<=m; i++) if(l[1][i]<=L) R=max(R,r[1][i]); L=R+1; ++res; } cout << "1\n" << res << '\n'; return 0; }