Matrix 子矩阵HW之和

Matrix 子矩阵HW之和,matrix,submatrix,Matrix,Submatrix,需要找出给定矩阵的子矩阵中出现所需和的次数。 例子: 矩阵: 0 0 0 0 1 0 0 0 0 总数:1 总数:16 int i, j, k, temp, counter=0; for(i=0; i<cols; i++){ // running the matrix for(j=0; j<rows; j++){ temp=0; for(k=j; (k<rows)&&(temp<=sum); k++){ //

需要找出给定矩阵的子矩阵中出现所需和的次数。 例子: 矩阵: 0 0 0 0 1 0 0 0 0 总数:1

总数:16

int i, j, k, temp, counter=0;

for(i=0; i<cols; i++){  // running the matrix
    for(j=0; j<rows; j++){
        temp=0;
        for(k=j; (k<rows)&&(temp<=sum); k++){   //  search for sub matrix sum N on rows.        
            temp=temp+matrix[i][k];
            if((temp==sum)&&(j<k)){                                   
                counter++;
                temp=0;
            }
        }
        temp=0;
        for(k=i; (k<cols)&&(temp<=sum); k++){   //  search for sub matrix sum N on columns.
            temp=temp+matrix[k][j];
            if((temp==sum)&&(i<k)){                  
                counter++;
                temp=0;
            }
        }
        if(matrix[i][j]==sum)
            counter++;
        temp=0;
    }   
}
return counter;
inti,j,k,temp,counter=0;
对于(i=0;i