辽宁师范大学 • 张大为@https://daweizh.github.io/noip/

1.8 编程基础之多维数组(25)

  1. 矩阵交换行,http://noi.openjudge.cn/ch0108/01/
    #include <iostream>
    using namespace std;
    
    int main(){
        int mat[5][5],n,m;
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                cin >> mat[i][j]; 
            }
        }
        cin >> n >> m;
        n--,m--;
        for(int i=0;i<5;i++){
            int tmp = mat[n][i];
            mat[n][i] = mat[m][i];
            mat[m][i] = tmp;
        }
        
        for(int i=0;i<5;i++){
            cout << mat[i][0] ;
            for(int j=1;j<5;j++){
                cout << " " << mat[i][j];
            }
            cout << endl;
        }
        
        return 0;
    }
    
  2. 同行列对角线的格子,http://noi.openjudge.cn/ch0108/02/
    #include <iostream>
    using namespace std;
    
    int main(){
      int n,x,y;
      cin >> n >> x >> y;
      
        cout << "(" << x << "," << 1 << ")";
      for(int i=2;i<=n;i++)
          cout << " (" << x << "," << i << ")";
      cout << endl;
      
        cout << "(" << 1 << "," << y << ")";
      for(int i=2;i<=n;i++)
          cout << " (" << i << "," << y << ")";
        cout << endl;
      
      int i=x,j=y;
      while(i>1 && j>1){
        i--;
        j--;
      }
      
        cout << "(" << i++ << "," << j++ << ")";
      while(i<=n && j<=n)
          cout << " (" << i++ << "," << j++ << ")";
      cout << endl;
    
      i=x,j=y;
      while(i<n && j>1){
        i++;
        j--;
      }
        cout << "(" << i-- << "," << j++ << ")";
      while(i>=1 && j<=n)
          cout << " (" << i-- << "," << j++ << ")";
      cout << endl;
    
        return 0;
    }
    
  3. 计算矩阵边缘元素之和,http://noi.openjudge.cn/ch0108/03/
    #include <iostream>
    using namespace std;
    
    int a[105][105],n,m;
    long long sum=0;
    int main(){
      cin >> n >> m;
      
      for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
          cin >> a[i][j];
      
      for(int i=0;i<m;i++)
        sum = sum + a[0][i];
      if(n>1)
        for(int i=0;i<m;i++)
          sum = sum + a[n-1][i];
        
      if(n>2){
        for(int i=1;i<n-1;i++)
          sum = sum + a[i][0];
        if(m>1)
          for(int i=1;i<n-1;i++)
            sum = sum + a[i][m-1];
          
      }
        
      cout << sum << endl;
      
      return 0;
    } 
    
  4. 错误探测,http://noi.openjudge.cn/ch0108/04/
    #include <iostream>
    using namespace std;
    
    int a[100][100],n;
    int row[100],col[100];
    int rn=0,cn=0,r,c;
    int main(){
      cin >> n;
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
          cin >> a[i][j];
          if(a[i][j]==1){
            row[i]++;
            col[j]++;
          }
        }	
      for(int i=0;i<n;i++){
        if(row[i]%2!=0){
          rn++;
          r=i+1;
        }
        if(col[i]%2!=0){
          cn++;
          c=i+1;
        } 
      }
      
      if(rn==0 && cn==0)
        cout << "OK" << endl;
      else if(rn==1 && cn==1)
        cout << r << " " << c << endl;
      else
        cout << "Corrupt" << endl;
      
      return 0;
    }
    
  5. 计算鞍点,http://noi.openjudge.cn/ch0108/05/
    #include <iostream>
    using namespace std;
    
    int a[6][6];
    int row[6],col[6];
    int main(){
      for(int i=0;i<6;i++)
        col[i]=0x7fffffff;
      for(int i=0;i<5;i++)
        for(int j=0;j<5;j++){
          cin >> a[i][j];
          if(a[i][j]>row[i])row[i]=a[i][j];
          if(a[i][j]<col[j])col[j]=a[i][j];
        }
      int flag = 1;
      for(int i=0;i<5;i++)
        for(int j=0;j<5;j++){
          if(row[i]==col[j]){
            cout << i+1 << " " << j+1 << " " << a[i][j] << endl;
            flag = 0; 
          }
        }	
      if(flag)
        cout << "not found" << endl;
      
      return 0;
    }
    
  6. 图像相似度,http://noi.openjudge.cn/ch0108/06/
    #include <iostream>
    using namespace std;
    
    int a[105][105],n,m,p;
    double sum = 0;
    
    int main(){
      cin >> n >> m;
    
      for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
          cin >> a[i][j];
      for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
          cin >> p;
          if(p==a[i][j]){
            sum = sum + 1;
          }
        }
            
      printf("%.2f\n",100*sum/(n*m));
      
      return 0;
    }
    
  7. 矩阵归零消减序列和,http://noi.openjudge.cn/ch0108/07/
    #include <iostream>
    using namespace std;
    
    int a[105][105],n;
    int row[105],col[105];
    
    void print(){
      for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
          cout << a[i][j] << " ";
        }
        cout << endl;
      }
      cout << endl;
    }
    
    int main(){
      
      cin >> n;
      
      for(int i=0;i<n;i++){
        row[i]=0x7fffffff;
        col[i]=0x7fffffff;
      }
      for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
          cin >> a[i][j];
          if(a[i][j]<row[i]) row[i]=a[i][j];
        }
      }
      int t = n;
      do{
        cout << a[1][1] << endl;
        
        for(int i=0;i<t;i++){
          for(int j=0;j<t;j++){
            a[i][j] = a[i][j]-row[i];
            if(a[i][j]<col[j]) col[j]=a[i][j];
          }
        }
        //print();
        for(int i=0;i<t;i++){
          for(int j=0;j<t;j++){
            a[j][i] = a[j][i] - col[i];
          }
        }
        //print();
        for(int i=0;i<t;i++){
          row[i]=a[i][0];
          for(int j=1;j<t-1;j++){
            a[i][j]=a[i][j+1];
            if(a[i][j]<row[i]) row[i]=a[i][j];
          }
          col[i]=a[0][i];
        }
        //print();
        for(int i=1;i<t-1;i++){
          for(int j=0;j<t;j++){
            a[i][j]=a[i+1][j];
          }
          row[i]=row[i+1];
        }
      }while(--t>0);
      
      return 0;
    }
    
  8. 矩阵加法,http://noi.openjudge.cn/ch0108/08/
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int a[105][105];
    int n,m,b;
    int main(){
      
      cin >> n >> m;
      
      for(int i=1;i<=n;i++)
        for(int j=1;j<=m ;j++)
          cin >> a[i][j];
      
      for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
          cin >> b;
          a[i][j]+=b;
        }		
      
      for(int i=1;i<=n;i++){
        cout << a[i][1];
        for(int j=2;j<=m;j++){
          cout << " " << a[i][j];
        }
        cout << endl;
      }
      
      
      return 0;
    } 
    
  9. 矩阵乘法,http://noi.openjudge.cn/ch0108/09/
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    int n,m,k;
    int a[105][105],b[105][105],c[105][105];
    
    int main(){
      
      cin >> n >> m >> k;
      
      for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
          cin >> a[i][j];
      
      for(int i=1;i<=m;i++)
        for(int j=1;j<=k;j++)
          cin >> b[i][j];
          
      for(int i=1;i<=n;i++)
        for(int j=1;j<=k;j++)
          for(int l=1;l<=m;l++)
            c[i][j] = c[i][j] + a[i][l] * b[l][j];
            
      for(int i=1;i<=n;i++){
        cout << c[i][1];
        for(int j=2;j<=k;j++){
          cout << " " << c[i][j];
        }
        cout << endl;
      }
          
      return 0;
    }
    
  10. 矩阵转置,http://noi.openjudge.cn/ch0108/10/
    #include <iostream>
    using namespace std;
    
    int n,m,a[105][105];
    
    int main(){
      cin >> n >> m;
      
      for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
          cin >> a[i][j];
      
      for(int i=0;i<m;i++){
        for(int j=0;j<n;j++)
          cout << a[j][i] << " ";
        cout << endl;
      }
        
      
      return 0;
    }
    
  11. 图像旋转,http://noi.openjudge.cn/ch0108/11/
    #include <iostream>
    using namespace std;
    int n,m,a[105][105];
    
    int main(){
      cin >> n >> m;
      for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
          cin >> a[i][j];
      
      for(int i=0;i<m;i++){
        for(int j=n-1;j>=0;j--)
          cout << a[j][i] << " ";
        cout << endl;
      }	
      
      return 0;
    }
    
  12. 变幻的矩阵,http://noi.openjudge.cn/ch0108/12/
    #include <iostream>
    using namespace std;
    
    int n;
    char a[10][10],b[10][10];
    bool judge1();
    bool judge2();
    bool judge3();
    bool judge4();
    
    int main(){
      cin >> n;
      
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          cin >> a[i][j];
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          cin >> b[i][j];
      
      if(judge1()) cout << 1 << endl;
      else if(judge2()) cout << 2 << endl;
      else if(judge3()) cout << 3 << endl;
      else if(judge4()) cout << 4 << endl;
      else 	cout << 5 << endl;
      
      return 0;
    }
    
    bool judge1(){
      bool ret = true;
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          if(a[i][j]!=b[j][n-i-1]){
            ret = false;
          }
      return ret;
    }
    
    bool judge2(){
      bool ret = true;
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          if(a[i][j]!=b[n-j-1][i]){
            ret = false;
          }
      return ret;
    }
    
    bool judge3(){
      bool ret = true;
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          if(a[i][j]!=b[n-i-1][n-j-1]){
            ret = false;
          }
      return ret;
    }
    
    bool judge4(){
      bool ret = true;
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          if(a[i][j]!=b[i][j]){
            ret = false;
          }
      return ret;
    }
    
  13. 图像模糊处理,http://noi.openjudge.cn/ch0108/13/
    #include <iostream>
    using namespace std;
    
    int n,m,b[105][105];
    double a[105][105];
    
    int main(){
      cin >> n >> m;
      for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
          cin >> a[i][j];
          b[i][j] = a[i][j];
        }
      if(n>=3 && m>=3)
        for(int i=1;i<n-1;i++)
          for(int j=1;j<m-1;j++){
            b[i][j] = (a[i][j]+a[i][j-1]+a[i][j+1]+a[i-1][j]+a[i+1][j])/5+0.5;
          }
      
      for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
          cout << b[i][j] << " ";
        cout << endl;
      }
      return 0;
    }
    
  14. 扫雷游戏地雷数计算,http://noi.openjudge.cn/ch0108/14/
    #include <iostream>
    using namespace std;
    
    char d[105][105];
    char mine[105][105];
    int x[8]={-1,0,1,-1,1,-1,0,1};
    int y[8]={-1,-1,-1,0,0,1,1,1};
    int n,m;
    
    int main(){
      cin >> n >> m;
      for(int i=0;i<n;i++)
        scanf("%s",d[i]);
        
      for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
          if(d[i][j]=='*')
            mine[i][j]='*';
          else{
            int tmp = '0';
            for(int k=0;k<8;k++){
              int x1 = i+x[k];
              int y1 = j+y[k];
              if(x1>=0 && x1<=n && y1>=0 && y1<=m){
                if(d[x1][y1]=='*')
                  tmp = tmp + 1;
              }
            }
            mine[i][j]=tmp;
          }			
        }
      }
      
      for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
          cout << mine[i][j];
        }
        cout << endl;
      }
      
      return 0;
    } 
    
  15. 细菌的繁殖与扩散,http://noi.openjudge.cn/ch0108/15/
    #include <iostream>
    using namespace std;
    
    long long a[12][12],b[12][12],m,n;
    int x[8]={-1,0,1,-1,1,-1,0,1};
    int y[8]={-1,-1,-1,0,0,1,1,1};
    int main(){
      cin >> m >> n;
      
      a[5][5] = m;
      
      for(int t=1;t<=n;t++){
        for(int i=1;i<=9;i++){
          for(int j=1;j<=9;j++){
            b[i][j] = b[i][j]+ a[i][j]*2;
            for(int k=0;k<8;k++){
              int x1=i+x[k];
              int y1=j+y[k];
              b[x1][y1] = b[x1][y1] + a[i][j];
            }				
          }
        }
        
        for(int i=1;i<=9;i++)
          for(int j=1;j<=9;j++){
            a[i][j] = b[i][j];
            b[i][j] = 0;
          }
      }
      
      for(int i=1;i<=9;i++){
        cout << a[i][1];
        for(int j=2;j<=9;j++)
          cout << " " << a[i][j];
        cout << endl;
      }
      
      return 0;
    }
    
  16. 矩阵剪刀石头布,http://noi.openjudge.cn/ch0108/16/
    #include <iostream>
    using namespace std;
    
    char a[101][101],b[101][101];
    int c,r,n;
    
    int main(){
      cin >> r >> c >> n;
      for(int i=0;i<r;i++)
        for(int j=0;j<c;j++){
          cin >> a[i][j];
          b[i][j] = a[i][j];			
        }
    
      while(n--){
        for(int i=0;i<r;i++){
          for(int j=0;j<c;j++){
            switch(a[i][j]){
              case 'R':
                if((i+1<r&&a[i+1][j]=='P')||(j+1<c&&a[i][j+1]=='P')||(i-1>=0&&a[i-1][j]=='P')||(j-1>=0&&a[i][j-1]=='P'))
                  b[i][j]='P';
                break;
              case 'S':
                if((i+1<r&&a[i+1][j]=='R')||(j+1<c&&a[i][j+1]=='R')||(i-1>=0&&a[i-1][j]=='R')||(j-1>=0&&a[i][j-1]=='R'))
                  b[i][j]='R';
                break;
              case 'P': 
                if((i+1<r&&a[i+1][j]=='S')||(j+1<c&&a[i][j+1]=='S')||(i-1>=0&&a[i-1][j]=='S')||(j-1>=0&&a[i][j-1]=='S'))
                  b[i][j]='S';
                break;
            }
          }
        }
        for(int i=0;i<r;i++)
          for(int j=0;j<c;j++){
            a[i][j] = b[i][j];			
          }
      }
      
      for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
          cout << a[i][j];			
        }
        cout << endl; 
      }
    
      return 0;
    }
    
  17. 最好的草,http://noi.openjudge.cn/ch0108/17/
    #include <iostream>
    using namespace std;
    
    char f[101][101];
    int r,c;
    
    int main(){
      cin >> r >> c;
      for(int i=0;i<r;i++)
        cin >> f[i];
      int cnt = 0;
      for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
          if(f[i][j]=='#'){
            f[i][j]='.';
            if(f[i][j+1]=='#'){
              f[i][j+1]='.';
            }else if(f[i+1][j]=='#'){
              f[i+1][j]='.';
            }
            cnt++;
          }
        }
      }
      cout << cnt << endl;
      
      return 0;
    }
    
  18. 肿瘤面积,http://noi.openjudge.cn/ch0108/18/
    #include <iostream>
    using namespace std;
    
    int p[1005][1005],n,x1,y1,x2,y2;
    
    int main(){
      cin >> n;
      
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          cin >> p[i][j];
    
      for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
          if(p[i][j]==0){
            if(i<(n-1) && j<(n-1) && p[i+1][j]==0&&p[i][j+1]==0){
              x1=i;
              y1=j;
            }else if(j>0&&i>0&&p[i-1][j]==0&&p[i][j-1]==0){
              x2=i;
              y2=j;
            }
          }
        }
    
      if(x1!=0 || x2!=0)
        cout << (x2-x1-1)*(y2-y1-1) << endl;
      else
        cout << 0 << endl;
        
      return 0;
    }
    
  19. 肿瘤检测,http://noi.openjudge.cn/ch0108/19/
    #include <iostream>
    using namespace std;
    
    int p[105][105],n,s,l;
    
    int main(){
      cin >> n;
      for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
          cin >> p[i][j];
        }
      }
      for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
          if(p[i][j]<=50){
            s++;
            if(i==0 || j==0 || i==n-1 || j==n-1)
              l++;
            else if(p[i-1][j]>50 || p[i+1][j]>50 || p[i][j-1]>50 || p[i][j+1]>50)
              l++;
          } 
        }
      }
      
      cout << s << " " << l << endl;
      
      return 0;
    }
    
  20. 反反复复,http://noi.openjudge.cn/ch0108/20/
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char w[105][21],s[205];
    int n,x,y;
    
    int main(){
      cin >> n >> s;
      
      int len = strlen(s);
      x = -1;	
      for(int i=0;i<len;i++){
        y = i %n;
        if(y==0) x++;
        if(x%2==0)
          w[x][y] = s[i];
        else
          w[x][n-y-1] = s[i];
      }
      
      for(int j=0;j<n;j++){
        for(int i=0;i<=x;i++){
          cout << w[i][j];
        }
      }
      cout << endl;
      
      return 0;
    }
    
  21. 二维数组右上左下遍历,http://noi.openjudge.cn/ch0108/21/
    #include <iostream>
    using namespace std;
    
    int row,col,arr[105][105],x,y,tot,r,c;
    
    int main(){
      cin >> row >> col;
      for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
          cin >> arr[i][j];
        }
      }
      tot = row * col;
      r=x = 0;
      c=y = 0;
      while(tot--){
        cout << arr[x][y] << endl;
        if(y==0 || x==row-1){
          c++;
          if(c>=col){
            y = col-1;
            r++;
            x = r;
          }else{
            y=c;
            x=0;
          }
        }else{
          x++;
          y--;			
        }
      }
    
      return 0;
    }
    
  22. 神奇的幻方,http://noi.openjudge.cn/ch0108/22/
    #include <iostream>
    using namespace std;
    
    int n,m[50][50],x1,x2,y1,y2,c,t,p;
    
    int main(){
      cin >> n;
      x1 = 0;
      y1 = n-1;
      p = 1;
      t=(2*n-1)*(2*n-1)-1;
      m[x1][y1] = p;
      
      while(t--){
        if(x1==0){
          if(y1==2*n-2){
            x2 = x1+1;
            y2 = y1;
          }else{
            x2 = 2*n-2;
            y2= y1+1;
          }
        }else if(y1==2*n-2){
          y2=0;
          x2=x1-1;
        }else{
          y2=y1+1;
          x2=x1-1;
          if(m[x2][y2]!=0){
            x2=x1+1;
            y2=y1;
          }
        }
        m[x2][y2]=++p;
        
        x1 = x2;
        y1 = y2;
      }
      
      t = 2*n-1;
      for(int i=0;i<t;i++){
        cout << m[i][0];
        for(int j=1;j<t;j++){
          cout << " " << m[i][j];
        }
        cout << endl;
      }
      
      return 0;
    }
    
  23. 二维数组回形遍历,http://noi.openjudge.cn/ch0108/23/
    #include <iostream>
    using namespace std;
    
    int a[100][100],n,m,row=1,col=1;
    int lft,rgt,top,btm;
    
    int main(){
      cin >> n >> m;
      for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
          cin >> a[i][j];
        }
      }
      
      while(row<=n||col<=m){
        for(int i=row,j=col;j<=m&&row<=n;j++)
          cout << a[i][j] << endl;
        row++;
        for(int i=row,j=m;i<=n&&col<=m;i++)
          cout << a[i][j] << endl;
        m--;
        for(int i=n,j=m;j>=col&&row<=n;j--)
          cout << a[i][j] << endl;
        n--;
        for(int i=n,j=col;i>=row&&col<=m;i--)
          cout << a[i][j] << endl;
        col++;
      }
    
      return 0;
    }
    
  24. 蛇形填充数组,http://noi.openjudge.cn/ch0108/24/
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int a[20][20],x,y,t,n,p=1;
    
    int main(){
      cin >> n;
      t = n*n;
      x=0;
      y=0;
      a[x][y]=p++;
      while(p<=t){
        if(x==0){
          y++;
          a[x][y]=p++;
          while(y!=0 && p<=t){
            x++;
            y--;
            a[x][y]=p++;
          }
        }
        if(y==0 && x!=n-1 && p<=t){
          x++;
          a[x][y]=p++;
          while(x!=0){
            x--;
            y++;
            a[x][y]=p++;
          }
        }
        if(x==n-1 && p<=t){
          y++;
          a[x][y]=p++;
          while(y!=n-1){
            x--;
            y++;
            a[x][y]=p++;
          }
        }
        if(y==n-1 && p<=t){
          x++;
          a[x][y]=p++;
          while(x!=n-1){
            x++;
            y--;
            a[x][y]=p++;
          }
        }
      }
    
      for(int i=0;i<n;i++){
        cout << a[i][0];
        for(int j=1;j<n;j++){
          cout << " " << a[i][j];
        }
        cout << endl;
      }
      
      return 0;
    }
    
  25. 螺旋加密,http://noi.openjudge.cn/ch0108/25/
    #include <iostream>
    #include <string>
    using namespace std;
    
    int r,c;
    char m[21][21],dst[21][21];
    string s,src;
    
    string getc(char ch){
      char t[6];
      int cn=0;
      if(ch!=' ') cn = ch-'A' + 1;
      for(int i=0;i<5;i++){
        t[4-i]=char(48+cn%2);
        cn= cn /2;
      }
      t[5]='\0';
      return t;
    }
    
    int main(){
      cin >> r >> c ;
      int x,y,row=0,col=0;
      x=r;
      y=c;
      getchar();
      getline(cin,s);
      src="";
      for(int i=0;i<s.size();i++)
        src = src + getc(s[i]);
      int tt = r*c,p=0;
      for(int i=src.size();i<=tt;i++)
        src = src + '0';
        
      while(col<=c || row<=r){
        for(int i=row,j=col;j<c&&row<r;j++)
          dst[i][j] = src[p++];
        row++;
        for(int i=row,j=c-1;i<r&&col<c;i++)
          dst[i][j] = src[p++];
        c--;		
        for(int i=r-1,j=c-1;j>=col&&row<r;j--)
          dst[i][j] = src[p++];
        r--;
        for(int i=r-1,j=col;i>=row&&col<c;i--)
          dst[i][j] = src[p++];
        col++;
      }	
      for(int i=0;i<x;i++){
        for(int j=0;j<y;j++)
          cout << dst[i][j];
      }
      
      return 0;
    }
    

参考网址

  1. http://noi.openjudge.cn