UVALive 6884 GREAT + SWERC = PORTO dfs模拟

题目连接:

  https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4896

                                                                              

We want to have a great SWERC at Porto this year and we approached this challenge in several ways.
We even framed it as a word addition problem, similar to the classic SEND+MORE=MONEY, where
each letter stands for a single digit (0, 1, 2, ..., 8, 9) that makes the arithmetic operation correct. In
word additions different letters cannot be assigned the same digit and the leftmost letter in a word
cannot be zero (0). In particular, a single letter term cannot be zero.
To solve this word addition problem we had to nd positive digits for G, S and P, and digits for R,
E, A, T, W, C, O, so that each letter has a different digit and the sum is correct. It turns out that,
unlike the classical SEND+MORE=MONEY which has a single solution, GREAT+SWERC=PORTO
has six solutions.
T=7, E=3, W=9, G=1, A=0, P=4, S=2, C=8, R=6, O=5
T=7, E=3, W=9, G=2, A=0, P=4, S=1, C=8, R=6, O=5
T=8, E=5, W=1, G=3, A=7, P=9, S=6, C=4, R=0, O=2
T=8, E=5, W=1, G=6, A=7, P=9, S=3, C=4, R=0, O=2
T=9, E=5, W=2, G=1, A=8, P=7, S=6, C=4, R=0, O=3
T=9, E=5, W=2, G=6, A=8, P=7, S=1, C=4, R=0, O=3
Having more than one solution does not make GREAT+SWERC=PORTO a good problem to solve
by hand, but it is still a piece of cake for a programer. Moreover, it gives us another reason to organize
SWERC again next year and, who knows, in years to come!
Given a word addition problem, compute the number of solutions (possibly zero)

题意:

  给你 n个字符串 问你是否能将1~n-1相加得到第n个字符串

  你可以用0~9中数字代替某一个字母

  一种数字只能代替一种字母

  不同的字母不会超过10,n不超过10

题解:

  对每一位填数爆搜

  要耐心写好

#include<bits/stdc++.h>
using namespace std;
const int N = 800, M = 1e2+10, mod = 1e9+7, inf = 2e9;
typedef long long ll;

char s[11][11];
int can = 0,n,v[N],num[N];
void dfs(int dep,int last,int p,int now) {
    int f = strlen(s[n]+1);
    if(dep>=(f+1)) {
        if(last==0)
            can++;
        return ;
    }
    f = strlen(s[p]+1);
    if(p!=n) {
        if((f - dep + 1) >= 1) {
            if(v[s[p][f - dep + 1]] != -1) {
                if(f - dep + 1 == 1 && v[s[p][f - dep + 1]]==0) return ;
                dfs(dep,last,p+1,now+v[s[p][(f - dep + 1)]]);
            }else {
                for(int i=0;i<=9;i++) {
                    if(num[i]) continue;
                    if(f - dep + 1==1&&i==0) continue;
                    num[i] = 1;
                    v[s[p][(f - dep + 1)]] = i;
                    dfs(dep,last,p+1,now+i);
                    v[s[p][(f - dep + 1)]] = -1;
                    num[i] = 0;
                }
            }
        }else dfs(dep,last,p+1,now);
    }else {
        if(v[s[p][(f - dep + 1)]]!=-1) {
            if((f-dep+1==1)&&v[s[p][(f - dep + 1)]]==0) return ;
            if((now+last)%10 != v[s[p][(f - dep + 1)]])  return ;
            dfs(dep+1,(now+last)/10,1,0);
        }
        else {
            if(num[(now+last)%10]) return ;
            if(dep==f&&(now+last)%10==0) {return ;}
            v[s[p][(f - dep + 1)]] = (now+last)%10;
            num[(now+last)%10] = 1;
            dfs(dep+1,(now+last)/10,1,0);
            num[(now+last)%10] = 0;
            v[s[p][(f - dep + 1)]] = -1;
        }
    }
}
int main()
{
    while(~scanf("%d",&n)) {
        for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
        int mx = 0;
        for(int i=1;i<n;i++) {
            int len = strlen(s[i]+1);
            mx = max(mx,len);
        }
        int mxx = strlen(s[n]+1);
        if(mx > mxx ) {
            cout<<0<<endl;
            continue;
        }
        memset(num,0,sizeof(num));
        memset(v,-1,sizeof(v));
        can = 0;
        dfs(1,0,1,0);
        cout<<can<<endl;
    }
}
原文地址:https://www.cnblogs.com/zxhl/p/5674463.html