2 条题解

  • -2
    @ 2024-10-15 18:38:01

    血泪教训: 本题的数据是在windows下生成的,而评测系统却是Linux,两者在字符的处理上有些许不同,例如换行处理上。 我原本使用getchar(),由于上述原因无法通过,可以将矩阵的每行看成一个字符串,循环行数次,读入字符串暂时存储到字符数组里,这样就省了换行的处理,然后对字符串遍历,再对每个字符进行判断,最后再将信息转存到二维表里

    #include<iostream>
    #include<cstring>
    using namespace std;
    
    const int fx[10]={0,-1,1,0,0};
    const int fy[10]={0,0,0,1,-1};
    
    bool maps[1001][1001];
    char s [1001];
    int a[3][100001]; 
    int m,n;
    int sum=0;
    
    void bfs(int,int);
    
    int main()
    { scanf("%d%d",&m,&n);
      
      for(int i=1;i<=m;i++)
        { scanf("%s",s);
          for(int j=0;j<=n-1;j++)
            { if(s[j]!='0')
                maps[i][j+1]=1;
    		}
    	}
      for(int i=1;i<=m;i++)
        { for(int j=1;j<=n;j++)
            {
    		  if(maps[i][j])
                {
                	
    			 sum++;
    			 bfs(i,j);
                 
    			}
    		}
    	  
    	}
      printf("%d",sum);
      return 0;
    }
    
    void bfs(int x,int y)
    { 
      int head=0;
      int tail=1;
      a[1][1]=x;
      a[2][1]=y;
      maps[x][y]=0;
      do
      { head++;
        for(int i=1;i<=4;i++)
          { if(maps[a[1][head]+fx[i]][a[2][head]+fy[i]]!=0&&a[1][head]+fx[i]>=1&&a[1][head]+fx[i]<=m&&a[2][head]+fy[i]>=1&&a[2][head]+fy[i]<=n)
              { tail++;
    		    a[1][tail]=a[1][head]+fx[i];
    		    a[2][tail]=a[2][head]+fy[i];
    		    maps[a[1][tail]][a[2][tail]]=0;
    		  }
    	  }
      }while(head<tail);
    }
    
    

    信息

    ID
    1566
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    95
    已通过
    19
    上传者