1

美好的一天,我需要帮助。我们得到了一个作业,用 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;
}
4

1 回答 1

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]) {
                    ...
                }
            }
        }
    }
}
于 2013-11-24T15:07:07.310 回答