4029: 网格行走

4029: 网格行走_第1张图片

#include 
using namespace std;
const int N = 25;
int n, g[N][N];
long long ans;
void dfs(int i, int j, int sum, int n, int m, vector<int>& v) {
    sum ^= g[i][j];
    if (i == n && j == m) { v.push_back(sum); return; }
    if (i + 1 <= n) dfs(i + 1, j, sum, n, m, v);
    if (j + 1 <= m) dfs(i, j + 1, sum, n, m, v);

}
int main() {
    cin >> n, n--;
    int x;
    cin >> x;
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
            cin >> g[i][j];
    g[0][0] ^= x;

    for (int i = 0; i <= n; i++) {
        vector<int> v1, v2;
        dfs(0, 0, 0, i, n - i, v1), dfs(i, n - i, g[i][n - i], n, n, v2);
        sort(v1.begin(), v1.end());
        for (int x : v2) {
            ans += upper_bound(v1.begin(), v1.end(), x) - lower_bound(v1.begin(), v1.end(), x);
        }
    }

    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(算法,深度优先,图论)