解法一:我也不記得怎麼做了,就記得好像是不斷拼接字串,然後用map做判斷去重
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e3+10,inf = 0x3f3f3f3f; int main() { int n; cin >> n; map<string,int> ma[N]; map<string,int>::iterator it; string s[N],r; int x,ans = 0; for(int i = 1; i <= n; i++) { s[i] = ""; for(int j = 1; j <= 5; j++) { cin >> x; s[i] += '0' + x; } ma[i][s[i]] = 1; for(int j = 0; j <= 4; j++) { r = s[i]; for(int k = 0; k <= 9; k++) { if(char('0' + k) != s[i][j]) { r[j] = '0' + k; ma[i][r] = 1; } } } for(int j = 0; j <= 3; j++) { r = s[i]; for(int k = 1; k <= 10; k++) { int a = (r[j] - '0' + 1) % 10,b = (r[j + 1] - '0' + 1) % 10; r[j] = a + '0'; r[j + 1] = b + '0'; //cout << r << endl; ma[i][r] = 1; } } } for(int i = n; i <= n; i++) { for(it = ma[i].begin(); it != ma[i].end(); it++) { int sum = 0; r = it->first; for(int j = 1; j < i; j++) { if(ma[j][r] && r != s[j] && r != s[i])sum++; } if(sum == n - 1)ans++; } } if(n == 1)cout << 81; else cout << ans; return 0; }
解法二:先列舉按0到9下時的所有情況,再寫個5重迴圈,列舉出所有方案
#include<bits/stdc++.h> using namespace std; const int N=20,mod=10; int f[N][N][N][N][N]; int main() { int n; cin>>n; int s=0; for(int i=1;i<=n;i++) { int a,b,c,d,e; cin>>a>>b>>c>>d>>e; for(int j=0;j<=9;j++) { f[(a+j)%mod][b][c][d][e]++; f[a][(b+j)%mod][c][d][e]++; f[a][b][(c+j)%mod][d][e]++; f[a][b][c][(d+j)%mod][e]++; f[a][b][c][d][(e+j)%mod]++; f[(a+j)%mod][(b+j)%mod][c][d][e]++; f[a][(b+j)%mod][(c+j)%mod][d][e]++; f[a][b][(c+j)%mod][(d+j)%mod][e]++; f[a][b][c][(d+j)%mod][(e+j)%mod]++; } } for(int i=0;i<=9;i++) for(int j=0;j<=9;j++) for(int k=0;k<=9;k++) for(int l=0;l<=9;l++) for(int m=0;m<=9;m++) if(f[i][j][k][l][m]==n) s++; cout<<s; return 0; }