Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n,m;
- string s;
- string mat[52][52];
- map<char,int>f;
- string c="23456789TJQKA";
- string c2="CDHS";
- vector<pair<int,int>>ans;
- string jok1="",jok2="";
- map<string,int>aux;
- int patrat;
- int joker;
- int x1=-1,Y1=-1,x2=-1,Y2=-1;
- int main(){
- cin>>n>>m;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- cin>>s;
- mat[i][j]=s;
- aux[s]++;
- }
- }
- //BRUTE-FORCE
- for(int i=0;i<=n-3&&patrat<2;i++)
- {
- for(int j=0;j<=m-3 &&patrat<2;j++)
- {
- f.clear();
- int to_verify=0;
- x1=Y1=x2=Y2=-1;
- if(ans.size()==1)
- {
- if(i<ans[0].first+3 && j<ans[0].second+3)continue;
- }else if(ans.size()==2)break;
- for(int k1=i;k1<i+3;k1++)
- {
- for(int k2=j;k2<j+3;k2++)
- {
- if(mat[k1][k2][0]=='J')
- {
- if(mat[k1][k2][1]=='1' || mat[k1][k2][1]=='2'){
- to_verify++;
- if(mat[k1][k2][1]=='1'){x1=k1;Y1=k2;}
- else {x2=k1;Y2=k2;}
- }else {
- f[mat[k1][k2][0]]++;
- f[mat[k1][k2][1]]++;
- }
- }else{
- f[mat[k1][k2][0]]++;
- f[mat[k1][k2][1]]++;
- }
- }
- }
- if(to_verify)
- {
- int ok=0;
- for(int k=0;k<4;k++)
- {
- if(f[c2[k]]==9-to_verify)
- {
- ok=0;
- for(int k2=0;k2<c.size();k2++)
- {
- if(f[c[k2]]==0){
- if(x1>=0)
- {
- jok1 = c[k2];
- jok1 = jok1+c2[k];
- if(aux[jok1]==0)
- {
- x1=-1;
- f[c[k2]]=1;
- ok++;
- aux[jok1]++;
- if(ok==to_verify)break;
- }
- }else{
- if(x2>=0){
- jok2 = c[k2];
- jok2 = jok2+c2[k];
- if(aux[jok2]==0)
- {
- x2=-1;
- f[c[k2]]=1;
- ok++;
- aux[jok2]++;
- if(ok==to_verify)break;
- }
- }
- }
- }
- }
- if(ok!=to_verify){
- aux[jok1]=0;
- aux[jok2]=0;
- ok=0;
- }
- if(ok==to_verify){
- ans.push_back(make_pair(i,j));
- patrat++;
- break;
- }
- }
- }
- if(!ok)
- {
- ok=1;
- for(int k=0;k<c.size();k++)
- {
- if(f[c[k]]>1)
- {
- ok=0;
- break;
- }
- }
- if(ok)
- {
- ok=0;
- for(int k=0;k<c.size();k++)
- {
- if(f[c[k]]==0)
- {
- for(int k2=0;k2<4;k2++)
- {
- string z;
- z = c[k];
- z = z+c2[k2];
- if(x1>=0)
- {
- if(aux[z]==0 && f[c[k]]==0)
- {
- jok1=z;
- x1=-1;
- f[c2[k]]=1;
- ok++;
- aux[jok1]++;
- }
- }else{
- if(x2>=0){
- jok2 = z;
- if(aux[jok2]==0 && f[c[k]]==0)
- {
- x2=-1;
- f[c2[k]]=1;
- ok++;
- aux[jok2]++;
- }
- }
- }
- }
- }
- }
- if(ok==to_verify)
- {
- ans.push_back(make_pair(i,j));
- patrat++;
- j+=2;
- }else{
- aux[jok1]=0;
- aux[jok2]=0;
- }
- }
- }else{
- ans.push_back(make_pair(i,j));
- patrat++;
- j+=2;
- }
- }else{
- int ok=0;
- for(int k=0;k<4;k++)
- {
- if(f[c2[k]]==9)
- {
- patrat++;
- ok=1;
- ans.push_back(make_pair(i,j));
- break;
- }
- }
- if(!ok)
- {
- for(int k=0;k<c.size();k++)
- {
- if(f[c[k]]>1)
- {
- ok=1;
- break;
- }
- }
- if(!ok)
- {
- patrat++;
- ok=1;
- ans.push_back(make_pair(i,j));
- }
- }
- }
- }
- }
- if(patrat>=2)
- {
- cout<<"Solution exists.\n";
- if(jok1=="" && jok2==""){
- cout<<"There are no jokers.\n";
- cout<<"Put the first square to ("<<ans[0].first+1<<", "<<ans[0].second+1<<").\n";
- cout<<"Put the second square to ("<<ans[1].first+1<<", "<<ans[1].second+1<<").\n";
- }else{
- if(jok1!="" && jok2!="")cout<<"Replace J1 with "<<jok1<<" and J2 with "<<jok2<<".\n";
- else{if(jok1!="")cout<<"Replace J1 with "<<jok1<<".\n";
- if(jok2!="")cout<<"Replace J2 with "<<jok2<<".\n";}
- cout<<"Put the first square to ("<<ans[0].first+1<<", "<<ans[0].second+1<<").\n";
- cout<<"Put the second square to ("<<ans[1].first+1<<", "<<ans[1].second+1<<").\n";
- }
- }else{
- cout<<"No solution.";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement