1 条题解

  • 0
    @ 2021-6-15 1:40:05

    C++ :

    #include<cstdio>
    #include<stack>
    #include<cstring>
    #include<algorithm>
    #define MAX 503
    using namespace std; 
    int n,m,l,r,tot,ans;
    int map[MAX][MAX];
    int px[]={0,1,0,-1},py[]={1,0,-1,0};
    int ask[MAX][MAX];
    struct _p{int x,y;};
    struct _l{int f,t;}ln[MAX];
    inline bool pp(const _l&a,const _l&b){return a.f<b.f;}
    inline bool pk1(int a,int b){return a>b;}
    inline bool pk2(int a,int b){return a<b;}
    void dfs(int a,int b,int c,bool(*pk)(int,int)){
    	int x1,y1;
    	ask[a][b]=c;
    	stack<_p>stk;
    	for(stk.push((_p){a,b});!stk.empty();){
    		_p now=stk.top();
    		stk.pop();
    		for(int i=0;i<4;++i){
    			x1=now.x+px[i];
    			y1=now.y+py[i];
    			if(!ask[x1][y1]&&pk(map[now.x][now.y],map[x1][y1])){
    				ask[x1][y1]=c;
    				stk.push((_p){x1,y1});
    			}
    		}
    	}
    }
    int main(){
    	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=0;i<m+2;++i)
    	map[0][i]=map[n+1][i]=1<<30;
    	for(int i=0;i<n+2;++i)
    	map[i][0]=map[i][m+1]=1<<30;
    	for(int i=1;i<=m;++i)
    	if(!ask[1][i])dfs(1,i,1,pk1);
    	for(int i=1;i<=m;++i)
    	if(!ask[n][i])++ans;
    	if(ans){
    		printf("0\n%d",ans);
    		return 0;
    	}
    	memset(ask,0,sizeof ask);
    	for(int i=1;i<=m;++i)
    	if(!ask[n][i])dfs(n,i,i,pk2);
    	for(int i=1;i<=m;++i)
    	if(ask[1][i])ln[i].f=ask[1][i];
    	memset(ask,0,sizeof ask);
    	for(int i=m;i>=1;--i)
    	if(!ask[n][i])dfs(n,i,i,pk2);
    	for(int i=1;i<=m;++i)
    	if(ask[1][i])ln[i].t=ask[1][i];
    	sort(ln+1,ln+1+n,pp);
    	for(int i=1,k=1,t=0;k<=m; )
    	if(ln[i].f<=k){
    		t=max(t,ln[i++].t);
    		if(t>m){ans++;break;}
        }
    	else k=t+1,ans++,t=0;
    	printf("1\n%d",ans);
    	return 0;
    }
    

    Pascal :

    var
    a:array[0..501,0..501] of longint;
    vv:array[0..501,0..501] of boolean;
    v:array[0..501] of boolean;
    l,r:array[1..500] of longint;
    n,i,j,k,m,ans,sum:longint;
    procedure bianli(i,j:integer);
    begin
      vv[i,j]:=false;
      if i=n then begin v[j]:=true;
      if j<l[k] then l[k]:=j;
      if j>r[k] then r[k]:=j;end;
      if (a[i,j]>a[i,j-1])and(vv[i,j-1])then bianli(i,j-1);
      if (a[i,j]>a[i,j+1])and(vv[i,j+1])then bianli(i,j+1);
      if (a[i,j]>a[i-1,j])and(vv[i-1,j])then bianli(i-1,j);
      if (a[i,j]>a[i+1,j])and(vv[i+1,j])then bianli(i+1,j);
    end;
    begin
      read(n,m);
      for i:=1 to m do l[i]:=m+1;
      for i:=1 to n do
      for j:=1 to m do
        begin
          read(a[i,j]);
          if j=m then readln;
        end;
      for k:=1 to m do
        if (a[1,k]>=a[1,k-1])and(a[1,k]>=a[1,k+1]) then
          begin
           for i:=1 to n do
              for j:=1 to m do
              vv[i,j]:=true;
          bianli(1,k);
          end;
      for i:=1 to m do if not(v[i]) then inc(sum);
      if sum>0 then begin
      writeln('0');writeln(sum);halt;end;
      i:=1;k:=0;
      while i<=m do
      begin
        for j:=1 to m do
        if (l[j]<=i)and(r[j]>k) then k:=r[j];i:=k+1;
        inc(sum);
        end;
      writeln('1');
      writeln(sum);
      end.
    
    • 1

    信息

    ID
    283
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者