Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较C语言中的两个二维数组_C_Arrays - Fatal编程技术网

比较C语言中的两个二维数组

比较C语言中的两个二维数组,c,arrays,C,Arrays,我在比较两个二维阵列时遇到问题 示例: 第一个数组: xxooo oxxoo oxooo oxooo ooooo 第二个数组(可旋转): oxoo xxxx xoo 但当我比较它们时,我会设法旋转并检查从左上角开始的两个for循环的相似性,如下所示: firstArr[i][j] == secArr[i][j] 如果第一个数组的维数大于第二个数组,如何检查整个数组中的相似性 另一个例子: 这是我的密码: #include <stdio.h> int main() { i

我在比较两个二维阵列时遇到问题

示例:

第一个数组:
xxooo
oxxoo
oxooo
oxooo
ooooo

第二个数组(可旋转):
oxoo
xxxx
xoo

但当我比较它们时,我会设法旋转并检查从左上角开始的两个for循环的相似性,如下所示:

firstArr[i][j] == secArr[i][j]
如果第一个数组的维数大于第二个数组,如何检查整个数组中的相似性

另一个例子:

这是我的密码:

#include <stdio.h>

int main()
{
    int i, j, pr, ps, pn, r, s;
    int indx = 0, indy = 0, brx = 0, bry = 0;
    int nema = 0;

    scanf(" %d %d %d %d", &r, &s, &pr, &ps);

    char prtljaga[r][s];
    char opredmet[pr][ps], opt1[pr][ps], opt2[ps][pr], opt3[ps][pr];
    int temp[r][s];

    for(i = 0; i < r; i++){
        for(j = 0; j < s; j++){
            temp[i][j] = 0;
        }
    }

    for(i = 0; i < r; i++){
        for(j = 0; j < s; j++){
            scanf(" %c", &prtljaga[i][j]);
        }
    }

    scanf(" %d", &pn);

    while(pn != 0){
        for(i = 0; i < pr; i++){
            for(j = 0; j < ps; j++){
                scanf(" %c", &opredmet[i][j]);
            }
        }

        for(i = 0; i < pr; i++){
            for(j = 0; j < ps; j++){
                opt1[pr-1-i][j] = opredmet[i][j];
            }
        }

        for(i = 0; i < pr; i++){
            for(j = 0; j < ps; j++){
                opt2[j][pr-1-i] = opredmet[i][j];
            }
        }

        for(i = 0; i < pr; i++){
            for(j = 0; j < ps; j++){
                opt3[ps-1-j][i] = opredmet[i][j];
            }
        }

        /* while */
        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(j > pr){
                    break;
                }else{
                    if(prtljaga[i][j] == opredmet[i][j]){
                        temp[i][j] = 1;
                    }else{

                    }
                }
            }
        }

        indx = 0; indy = 0; brx = 0; bry = 0;
        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(temp[i][j] == 1){
                    brx++;
                    if(brx == 1){
                        indx = j;
                        indy = i;
                    }
                }else{
                    if(brx != pr){
                         brx = 0;
                    }
                }
            }

            if(brx == pr){
                bry++;

                if(bry != pr){
                     brx = 0;
                }
            }else{
                if(bry != ps){
                    bry = 0;
                }
            }

            if(brx == pr && bry == ps){
                printf("%d: [%d, %d]", pn, indx+1, indy+1);
                nema = 1;
                break;
            }
        }

        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                temp[i][j] = 0;
            }
        }
        /* while */

        /* while */
        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(j > pr){
                    break;
                }else{
                    if(prtljaga[i][j] == opt1[i][j]){
                        temp[i][j] = 1;
                    }else{

                    }
                }
            }
        }

        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                printf("%d", temp[i][j]);
            }
            printf("\n");
        }

        indx = 0; indy = 0; brx = 0; bry = 0;
        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(temp[i][j] == 1){
                    brx++;
                    if(brx == 1 && bry == 0){
                        indx = j;
                        indy = i;
                    }
                }else{
                    if(brx != pr){
                         brx = 0;
                    }
                }
            }

            if(brx == pr){
                bry++;

                if(bry != ps){
                    brx = 0;
                }
            }else{
               if(bry != ps){
                    bry = 0;
                }
            }

            if(brx == pr && bry == ps){
                printf("%d: [%d, %d]", pn, indx+1, indy+1);
                nema = 1;
                break;
            }
        }

        if(nema == 0){
            for(i = 0; i < r; i++){
                for(j = 0; j < s; j++){
                    temp[i][j] = 0;
                }
            }
        }

        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                temp[i][j] = 0;
            }
        }
        /* while */

        /* while */
        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(j > pr){
                    break;
                }else{
                    if(prtljaga[i][j] == opt2[i][j]){
                        temp[i][j] = 1;
                    }else{

                    }
                }
            }
        }

        indx = 0; indy = 0; brx = 0; bry = 0;
        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(temp[i][j] == 1){
                    brx++;
                    if(brx == 1 && bry == 0){
                        indx = j;
                        indy = i;
                    }
                }else{
                    if(brx != ps){
                         brx = 0;
                    }
                }
            }

            if(brx == ps){
                bry++;

                if(bry != pr){
                     brx = 0;
                }
            }else{
                if(bry != pr){
                    bry = 0;
                }
            }

            if(brx == ps && bry == pr){
                printf("%d: [%d, %d]", pn, indx+1, indy+1);
                nema = 1;
                break;
            }
        }

        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                temp[i][j] = 0;
            }
        }

        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(j > pr){
                    break;
                }else{
                    if(prtljaga[i][j] == opt3[i][j]){
                        temp[i][j] = 1;
                    }else{

                    }
                }
            }
        }

        indx = 0; indy = 0; brx = 0; bry = 0;
        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                if(temp[i][j] == 1){
                    brx++;
                    if(brx == 1 && bry == 0){
                        indx = j;
                        indy = i;
                    }
                }else{
                    if(brx != ps){
                         brx = 0;
                    }
                }
            }

            if(brx == ps){
                bry++;

                if(bry != pr){
                     brx = 0;
                }
            }else{
                if(bry != pr){
                    bry = 0;
                }
            }

            if(brx == ps && bry == pr){
                printf("%d: [%d, %d]", pn, indx+1, indy+1);
                nema = 1;
                break;
            }
        }

        for(i = 0; i < r; i++){
            for(j = 0; j < s; j++){
                temp[i][j] = 0;
            }
        }

        indx = 0, indy = 0, brx = 0, bry = 0; pn--;
    }

    if(nema == 0){
        printf("-1");
    }
    return 0;
}
#包括
int main()
{
int i、j、pr、ps、pn、r、s;
int indx=0,indy=0,brx=0,bry=0;
int nema=0;
scanf(“%d%d%d%d”、&r、&s、&pr、&ps);
char prtljaga[r][s];
字符opredmet[pr][ps]、opt1[pr][ps]、opt2[ps][pr]、opt3[ps][pr];
内部温度[r][s];
对于(i=0;ipr){
打破
}否则{
if(prtljaga[i][j]==opredmet[i][j]){
温度[i][j]=1;
}否则{
}
}
}
}
indx=0;indy=0;brx=0;bry=0;
对于(i=0;ipr){
打破
}否则{
if(prtljaga[i][j]==opt1[i][j]){
温度[i][j]=1;
}否则{
}
}
}
}
对于(i=0;ipr){
打破
}否则{
if(prtljaga[i][j]==opt2[i][j]){
温度[i][j]=1;
}否则{
}
}
}
}
indx=0;indy=0;brx=0;bry=0;
对于(i=0;i#include <stdio.h>
#include <string.h>

int contains_similar(size_t rows,
                     size_t cols,
                     char arr[rows][cols],
                     size_t prows,
                     size_t pcols,
                     char pat[prows][pcols]);

void reverse_rows(size_t rows, size_t cols, char arr[rows][cols]);
void print_array(size_t rows, size_t cols, char arr[rows][cols]);

int main(void)
{
    char array[][10] = {
        { 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o' },
        { 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o' },
        { 'o', 'o', 'x', 'o', 'o', 'o', 'x', 'o', 'o', 'o' },
        { 'o', 'o', 'x', 'x', 'x', 'x', 'x', 'o', 'o', 'x' },
        { 'o', 'x', 'x', 'o', 'o', 'o', 'o', 'o', 'o', 'x' },
        { 'o', 'x', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'x' },
        { 'o', 'x', 'x', 'o', 'x', 'x', 'o', 'o', 'o', 'x' },
        { 'o', 'o', 'x', 'o', 'x', 'o', 'o', 'o', 'x', 'x' },
        { 'o', 'x', 'x', 'o', 'x', 'o', 'o', 'o', 'o', 'o' },
        { 'o', 'o', 'o', 'o', 'x', 'x', 'x', 'x', 'x', 'o' }
    };

    char pattern[][5] = {
        { 'x', 'x', 'x', 'x', 'x' },
        { 'x', 'o', 'o', 'o', 'x' }
    };

    size_t arr_cols = sizeof array[0];
    size_t arr_rows = sizeof array / arr_cols;
    size_t pat_cols = sizeof pattern[0];
    size_t pat_rows = sizeof pattern / pat_cols;

    printf("Working with %zu X %zu array:\n", arr_rows, arr_cols);
    putchar('\n');
    print_array(arr_rows, arr_cols, array);

    putchar('\n');
    printf("Using %zu X %zu pattern:\n", pat_rows, pat_cols);
    putchar('\n');
    print_array(pat_rows, pat_cols, pattern);

    putchar('\n');
    printf("Similar pattern %s in array\n",
           contains_similar(arr_rows, arr_cols, array,
                            pat_rows, pat_cols, pattern)
           ? "found"
           : "not found");

    return 0;
}

int contains_similar(size_t rows,
                     size_t cols,
                     char arr[rows][cols],
                     size_t prows,
                     size_t pcols,
                     char pat[prows][pcols])
{
    char temp[prows][pcols];
    size_t last_row = rows - prows;
    size_t last_col = cols - pcols;
    size_t pat_sz = prows * pcols;

    /* i and j index the upper-left corner of the region of interest */
    for (size_t i = 0; i <= last_row; i++) {
        for (size_t j = 0; j <= last_col; j++) {

            /* Copy region of interest to temp[][] */
            for (size_t k = 0; k < prows; k++) {
                memcpy(temp[k], &arr[i+k][j], pcols);
            }

            /* Make comparisons with transformations */
            if (memcmp(temp, pat, pat_sz) == 0) {      // pattern matched
                return 1;
            }

            reverse_rows (prows, pcols, temp);
            if (memcmp(temp, pat, pat_sz) == 0) {      // pattern matched
                return 1;
            }
        }
    }

    return 0;                                          // no match found
}

void reverse_rows(size_t rows, size_t cols, char arr[rows][cols])
{
    char temp[cols];
    size_t swap_row = rows - 1;

    for (size_t i = 0; i < swap_row; i++) {
        memcpy(temp, arr[i], cols);
        memmove(arr[i], arr[swap_row], cols);
        memcpy(arr[swap_row], temp, cols);
        --swap_row;
    }
}

void print_array(size_t rows, size_t cols, char arr[rows][cols])
{
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            putchar(arr[i][j]);
        }
        putchar('\n');
    }
}
#include <stdio.h>
#include <string.h>

#define ARR_SZ  100

struct Pattern
{
    size_t nrows;
    size_t ncols;
    char arr[ARR_SZ][ARR_SZ];
};

struct Coordinate
{
    size_t row;
    size_t col;
};

struct Matches
{
    size_t num;
    struct Coordinate location[ARR_SZ * ARR_SZ];
};

int find_pattern(struct Pattern pat,
                 struct Pattern sub,
                 struct Matches *found);

int find_similar(struct Pattern pat,
                 struct Pattern sub,
                 struct Matches *found);

void print_pattern(struct Pattern pat);
struct Pattern rotate_pattern(struct Pattern pat);

int main(void)
{
    struct Pattern large = {
        .arr = {
            { 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o' },
            { 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o' },
            { 'o', 'o', 'x', 'o', 'o', 'o', 'x', 'o', 'o', 'o' },
            { 'o', 'x', 'x', 'x', 'x', 'x', 'x', 'o', 'x', 'x' },
            { 'o', 'x', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'x' },
            { 'o', 'x', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'x' },
            { 'o', 'x', 'o', 'o', 'x', 'x', 'o', 'o', 'o', 'x' },
            { 'o', 'x', 'x', 'o', 'x', 'o', 'o', 'o', 'x', 'x' },
            { 'o', 'x', 'x', 'o', 'x', 'o', 'o', 'o', 'x', 'o' },
            { 'o', 'o', 'o', 'o', 'x', 'x', 'x', 'x', 'x', 'o' }
        }
    };
    large.ncols = 10;
    large.nrows = 10;

    struct Pattern small = {
        .arr = {
            { 'x', 'x', 'x', 'x', 'x' },
            { 'x', 'o', 'o', 'o', 'x' }
        }
    };
    small.ncols = 5;
    small.nrows = 2;

    struct Matches found = { .num = 0 };

    printf("Working with %zu X %zu array:\n", large.nrows, large.ncols);
    putchar('\n');
    print_pattern(large);

    putchar('\n');
    printf("Using %zu X %zu pattern:\n", small.nrows, small.ncols);
    putchar('\n');
    print_pattern(small);

    int ret = find_similar(large, small, &found);
    putchar('\n');
    printf("Similar patterns %s in array\n", ret ? "found" : "not found");

    if (ret) {
        for (size_t i = 0; i < found.num; i++) {
            printf("[%zu, %zu]\n",
                   found.location[i].row,
                   found.location[i].col);
        }
    }

    return 0;
}

int find_pattern(struct Pattern pat,
                 struct Pattern sub,
                 struct Matches *found)
{
    static size_t max_found = sizeof found->location / sizeof *found->location;
    int ret = 0;

    size_t rows = pat.nrows;
    size_t cols = pat.ncols;
    size_t srows = sub.nrows;
    size_t scols = sub.ncols;
    size_t last_row = rows - srows;
    size_t last_col = cols - scols;

    struct Pattern roi = { .nrows = srows,
                           .ncols = scols,
                           .arr = { { 0 } } };

    /* i and j index the upper-left corner of the region of interest */
    for (size_t i = 0; i <= last_row; i++) {
        for (size_t j = 0; j <= last_col; j++) {

            /* Copy region of interest to roi */
            for (size_t k = 0; k < srows; k++) {
                for (size_t l = 0; l < scols; l++) {
                    roi.arr[k][l] = pat.arr[i + k][j + l];
                }
            }

            /* Make comparison */
            if (memcmp(roi.arr, sub.arr, sizeof roi.arr) == 0) {

                /* Store location of match */
                struct Coordinate *cdt = &found->location[found->num];
                cdt->row = i;
                cdt->col = j;
                found->num += 1;
                if (found->num > max_found) {
                    found->num = max_found;
                }
                ret = 1;
            }
        }
    }

    return ret;
}

int find_similar(struct Pattern pat,
                 struct Pattern sub,
                 struct Matches *found)
{
    int ret = find_pattern(pat, sub, found);
    struct Pattern rot = sub;
    for (int i = 0; i < 3; i++) {
        rot = rotate_pattern(rot);
        ret = find_pattern(pat, rot, found) || ret;
    }

    return ret;
}

void print_pattern(struct Pattern pat)
{
    size_t rows = pat.nrows;
    size_t cols = pat.ncols;

    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            putchar(pat.arr[i][j]);
        }
        putchar('\n');
    }
}

struct Pattern rotate_pattern(struct Pattern pat)
{
    size_t rows = pat.nrows;
    size_t cols = pat.ncols;

    struct Pattern rotated = { .nrows = cols, .ncols = rows };
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            rotated.arr[j][rows - i - 1] = pat.arr[i][j];
        }
    }

    return rotated;
}