求救求救,请问为何一直编译错误啊。。萌新数组学的不是太好
查看原帖
求救求救,请问为何一直编译错误啊。。萌新数组学的不是太好
393190
aldol_reaction楼主2020/10/15 17:47

真的要自闭了,改了快1h 我encode返回值不是int吗,为什么一直说encode做不了函数值。。 and定义二维数组函数参数的时候不就是这么定义吗。。为啥老是报错少; 以下是代码和编译信息

#include <bits/stdc++.h>
using namespace std;

int a[3][3];

int encode(a[][3]) {
    int ret = 0;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            ret = ret * 10 + a[i][j];
        }
    }
    return ret;
}
void decode(int t) {
    for (int i = 2; i >= 0; --i) {
        for (int j = 2; j >= 0; --j) {
            a[i][j] = t % 10;
            t /= 10;
        }
    }
}
char ch[10];
int main() {
    scanf("%s", ch);
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            a[i][j] = ch[i * 3 + j] - '0';
        }
    }

    if (encode(a[3][3]) == 123804765) {
        printf("0\n");
        return 0;
    }

    map<int, int> d;
    d[encode(a[3][3]] = 0;
    queue<int> q;
    q.push(encode(a[3][3]));
    while (!q.empty()) {
        int t = q.front();
        q.pop();
        int x, y;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                if (a[i][j] == 0) x = i, y = j;
            }
        }
        int dt = d[t];
        if (x > 0) {
            swap(a[x][y], a[x - 1][y]);
            int nt = encode(a[3][3]);
            if (nt == 123804765) {
                printf("%d\n", dt + 1);
                return 0;
            }
            if (d.find(nt) == d.end()) {
                d[nt] = dt + 1;
                q.push(nt);
            }
            swap(a[x][y], a[x - 1][y]);
        }
        if (x < 2) {
            swap(a[x][y], a[x + 1][y]);
            int nt = encode(a[3][3]);
            if (nt == 123804765) {
                printf("%d\n", dt + 1);
                return 0;
            }
            if (d.find(nt) == d.end()) {
                d[nt] = dt + 1;
                q.push(nt);
            }
            swap(a[x][y], a[x + 1][y]);
        }
        if (y > 0) {
            swap(a[x][y], a[x][y - 1]);
            int nt = encode(a[3][3]);
            if (nt == 123804765) {
                printf("%d\n", dt + 1);
                return 0;
            }
            if (d.find(nt) == d.end()) {
                d[nt] = dt + 1;
                q.push(nt);
            }
            swap(a[x][y], a[x][y - 1]);
        }
        if (y < 2) {
            swap(a[x][y], a[x][y + 1]);
            int nt = encode(a[3][3]);
            if (nt == 123804765) {
                printf("%d\n", dt + 1);
                return 0;
            }
            if (d.find(nt) == d.end()) {
                d[nt] = dt + 1;
                q.push(nt);
            }
            swap(a[x][y], a[x][y + 1]);
        }
    }

    return 0;
}

/tmp/compiler_fc4cl74e/src:6:14: 错误:expected primary-expression before ‘]’ token
 int encode(a[][3]) {
              ^
/tmp/compiler_fc4cl74e/src: 在函数‘int main()’中:
/tmp/compiler_fc4cl74e/src:32:23: 错误:‘encode’ cannot be used as a function
     if (encode(a[3][3]) == 123804765) {
                       ^
/tmp/compiler_fc4cl74e/src:38:21: 错误:expected ‘)’ before ‘]’ token
     d[encode(a[3][3]] = 0;
             ~       ^
                     )
/tmp/compiler_fc4cl74e/src:40:26: 错误:‘encode’ cannot be used as a function
     q.push(encode(a[3][3]));
                          ^
/tmp/compiler_fc4cl74e/src:53:36: 错误:‘encode’ cannot be used as a function
             int nt = encode(a[3][3]);
                                    ^
/tmp/compiler_fc4cl74e/src:66:36: 错误:‘encode’ cannot be used as a function
             int nt = encode(a[3][3]);
                                    ^
/tmp/compiler_fc4cl74e/src:79:36: 错误:‘encode’ cannot be used as a function
             int nt = encode(a[3][3]);
                                    ^
/tmp/compiler_fc4cl74e/src:92:36: 错误:‘encode’ cannot be used as a function
             int nt = encode(a[3][3]);
2020/10/15 17:47
加载中...