2 条题解
-
0njnu19180318 LV 7 @ 2020-03-11 01:12:27
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int a[9][9], n;
string result;
cin >> n;
for (int x = 0; x < n; x++)
{
bool flag = true;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
cin >> a[i][j];
for (int i = 0; i < 9; i++)//判断每行
{
int temp = a[i][0];
for (int j = i + 1; j < 9; j++)
{
if (temp == a[i][j]) { result += "Wrong\n"; flag = false; break; }
}
if (flag == false)break;
}
if (flag == true)
{
for (int i = 0; i < 9; i++)//判断每列
{
int temp = a[0][i];
for (int j = i + 1; j < 9; j++)
{
if (temp == a[j][i]) { result += "Wrong\n"; flag = false; break; }
}
if (flag == false)break;
}
}
if (flag == true)
{
int s[9], m = 0;
for (int i = 0; i < 7; i += 3)
{
for (int j = i; j < i + 3; j++)
{
for (int k = i; k < i + 3; k++)
{
s[m++] = a[j][k];
}
}
for (int i = 0; i < 9; i++)
{
int temp = s[i];
for (int j = i + 1; j < 9; j++)
{
if (temp == s[j]) { result += "Wrong\n"; flag = false; break; }
}
if (flag == false)break;
}
if (flag == false)break;
m = 0;
}
}
if (flag == true)result += "Right\n";
}
cout << result;
return 0;
} -
02020-01-23 16:01:23@
按照规则验证一下即可,好久不做这类题目,我的代码貌似有点冗余。
/* */ #define method_1 #ifdef method_1 /* */ #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<set> #include<map> #include<queue> #include<stack> #include<vector> #include<cstring> #include<cstdlib> #include<iomanip> #include<ctime> #include<string> #include<bitset> #define D(x) cout<<#x<<" = "<<x<<" " #define E cout<<endl using namespace std; typedef long long ll; typedef pair<int,int>pii; const int maxn=9+5; const int INF=0x3f3f3f3f; const int table[][10]={ {0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,2,2,2,3,3,3}, {0,1,1,1,2,2,2,3,3,3}, {0,1,1,1,2,2,2,3,3,3}, {0,4,4,4,5,5,5,6,6,6}, {0,4,4,4,5,5,5,6,6,6}, {0,4,4,4,5,5,5,6,6,6}, {0,7,7,7,8,8,8,9,9,9}, {0,7,7,7,8,8,8,9,9,9}, {0,7,7,7,8,8,8,9,9,9}}; int T; int a[maxn][maxn]; int n=9; int vis[maxn]; int v[maxn][maxn]; void init(){ memset(v,0,sizeof(v)); } bool check(){ for(int i=1;i<=n;i++){ //row memset(vis,0,sizeof(vis)); int cnt=0; for(int j=1;j<=n;j++){ if(!vis[a[i][j]]){ vis[a[i][j]]=1; cnt++; } } if(cnt!=9) return false; } for(int i=1;i<=n;i++){ //col memset(vis,0,sizeof(vis)); int cnt=0; for(int j=1;j<=n;j++){ if(!vis[a[j][i]]){ vis[a[j][i]]=1; cnt++; } } if(cnt!=9) return false; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ //九宫格 int belong=table[i][j]; if(!v[belong][a[i][j]]){ v[belong][a[i][j]]=1; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(!v[i][j]) return false; } } return true; } int main() { ios::sync_with_stdio(false); //freopen("数独验证.in","r",stdin); cin>>T; while(T--){ init(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>a[i][j]; /* for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<table[i][j]<<" "; cout<<endl; } */ if(check()) cout<<"Right"<<endl; else cout<<"Wrong"<<endl; } return 0; } #endif #ifdef method_2 /* */ #endif #ifdef method_3 /* */ #endif
- 1