2016蓝桥杯决赛 机器人塔(会超时)

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

const int maxn = 500 + 3;
int M[maxn][maxn];
int num[2];

int sum;

void dfs(int r, int c)
{
    if(c > r) return;
    if(num[0] < 0 || num[1] < 0) return;
    else if(num[0] == 0 && num[1] == 0){
        sum++;
        return;
    }
    int cur = M[r][c];
    if(c == 0){
        int rr, cc;
        if(r == c){
            rr = r + 1;
            cc = 0;
        }else{
            rr = r;
            cc = c + 1;
        }
        if(cur == 0){
            M[r+1][c] = M[r+1][c+1] = 0; num[0] -= 2;
            dfs(rr, cc); num[0] += 2;
            M[r+1][c] = M[r+1][c+1] = 1; num[1] -= 2;
            dfs(rr, cc); num[1] += 2;
        }else if(cur == 1){
            M[r+1][c] = 0; M[r+1][c+1] = 1; num[0]--; num[1]--;
            dfs(rr, cc); num[0]++; num[1]++;
            M[r+1][c] = 1; M[r+1][c+1] = 0; num[0]--; num[1]--;
            dfs(rr, cc); num[0]++; num[1]++;
        }
    }else{
        if(cur == 0){
            M[r+1][c+1] = M[r+1][c]; num[M[r+1][c+1]]--;
        }else{
            M[r+1][c+1] = 1 - M[r+1][c]; num[M[r+1][c+1]]--;
        }
        if(c != r) {
            dfs(r, c + 1);
            num[M[r+1][c+1]]++;
        }
        else {
            dfs(r + 1, 0);
            num[M[r+1][c+1]]++;
        }
    }
}



int main()
{
    cin >> num[0] >> num[1];
    sum = 0;
    M[0][0] = 0; num[0]--;
    dfs(0, 0); num[0]++;

    M[0][0] = 1; num[1]--;
    dfs(0, 0);
    cout << sum << endl;
    return 0;
}

 

posted on 2019-05-22 21:29  nbsanshi  阅读(131)  评论(0编辑  收藏  举报

导航