簡體   English   中英

在一維數組中查找子矩陣-C

[英]Finding submatrix in one dimensional array - C

美好的一天,我需要幫助。 我們得到了一個用C編寫程序的作業,該程序應生成並打印由“ X”和“。”組成的越來越大的矩陣。 然后找到較小的3x3矩陣是否在較大的矩陣中。 我試圖通過一維字段來實現,但是我的程序有時只找到矩陣。 我找不到我的錯誤在哪里以及如何解決它。 我在論壇上閱讀了一些主題,但沒有一個對我有幫助。 謝謝你的幫助。

PS原諒我語言上的錯誤,我不是說英語的人。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Generates matrix of given dimensions */
void initMatrix(char *Matrix, int rows, int cols)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        {
        Matrix[i*cols+j]= "X.." [rand () % 3];         // 2/3 that X will be generated
        }
    }
}

/* Prints given matrix */
void printMatrix(char *Matrix, int rows, int cols)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
            {
            printf("%c", Matrix[i * cols + j]);
            }
        printf("\n");
    }
}

int main(void)
{
    int rowM1, colM1;                               // Dimensions of primary (bigger) matrix
    int rowM2 = 3, colM2 = 3;                       // Dimensions of secondary (smaller) matrix
    int first, second;                                      // Position of the begginng of matrix 2 in matrix 1
    int rel_pos;
    int i, j, k, l;
    char *M1 = NULL;                                // Pointer to matrix 1
    char *M2 = NULL;                                // Pointer to matrix 2

    printf("Enter the matrix dimensions separated by a space ([rows] [columns]) : ");
    if (scanf("%d %d", &rowM1, &colM1) != 2)        // Bad parameters
    {
        printf("Wrong parameters.");
        return 1;                                   // End program
    }

    if (rowM1 < rowM2 || colM1 < colM2)
    {
        printf("Matrix 2 can not be found because is bigger than Matrix 1.");
        return 1;
    }

    srand(time(NULL));                              // Randomly generates numbers

    M1 = malloc(rowM1 * colM1 * sizeof(char));      // M1 points to matrix 1
    M2 = malloc(rowM2 * colM2 * sizeof(char));      // M2 points to matrix 2

    initMatrix(M1, rowM1, colM1);                   // Initializes matrix 1
    initMatrix(M2, rowM2, colM2);                   // Initializes matrix 2

    printf("\nMatrix 1:\n");
    printMatrix(M1, rowM1, colM1);                  // Prints matrix 1
    printf("\nMatrix 2:\n");
    printMatrix(M2, rowM2, colM2);                  // Prints matrix 2

    putchar('\n');

    for (i = 0; i < rowM1; i++)
    {
        for(j = 0; j < colM1; j++){
        {
                for (k = 0; k <  rowM2 * colM2; k++)    // checking the smaller matrix
            {
                if(M1[i*rowM1+j] == M2[k])
                {
                    first = i*rowM1;
                    rel_pos = i+1;
                }
                if(j % colM2 == 0)                 // Matrix 2 has ended on this line, move on next one.
                    rel_pos += colM1 - colM2;

                if(M1[rel_pos] == M2[j])           // If character are same, keep searching
                    rel_pos++;

                else                                // else this is not the matrix I'm searching for
                    break;

            }
                if(k == rowM2*colM2)                // if all k cykle went to the end I found the matrix
                    {
                    printf("Matrix found at [%d][%d]", first, second);
                    return 0;
                    }
            }

        }
                if(i*colM1 > i*colM1-colM2)           // matrix cannot be found
                printf("Matrix not found");
                break;
        }

    free(M1);                                       // frees memory of matrix 1
    free(M2);                                       // frees memory of matrix 2
    return 0;
}

您的內部循環for (k = 0; k < rowM2 * colM2; k++)遍歷小矩陣的內容,並且應該將小矩陣的每個條目與大矩陣中的相應條目進行比較(由起點定義)由i和j給出)。

但是,比較if(M1[i*rowM1+j] == M2[k])將小矩陣的所有條目與大矩陣中的相同條目進行比較(M1的數組索引與k無關)。

要解決此問題,您需要進行三維循環

for(y0 = 0; y0 < colM1 - colM2 + 1; y0++) {
    for(x0 = 0; x0 < rowM1 - rowM2 + 1; x0++) {
        for(dy = 0; dy < colM2; dy++) {
            for(dx = 0; dx < rowM2; dx++) {
                if(M1[(y0 + dy)*rowM1 + (x0 + dx)] == M2[dy*rowM2 + dx]) {
                    ...
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM