2019计蒜之道 B:个性化评测系统
比賽的時候沒有想清楚,直接排序然后從前向后dfs,一直卡著,就是沒有想到其他的情況。其實有可能是223344這樣的,最后寫的時候忘記處理了222233334444情況,還是看別人博客才發現的。還是太菜了,繼續努力。
?
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
char mian[10]="mspz";
map<string,int> majong[4];
map<string,int>::iterator itr;
string tmp;
int cnt[4][14];
int dfs(int x,int c1,int c2)
{
??? int i,j,flag=0;
??? if(c1==4 && c2==1)
??????? return 1;
??? else if(c1>4 || c2>1 || x>=14)
??????? return 0;
??? if(c2==0)
??? {
??????? for(i=0;i<4;i++)
??????????? for(j=1;j<=9;j++)
??????????????? if(cnt[i][j]>=2)
??????????????? {
??????????????????? cnt[i][j]-=2;
??????????????????? flag|=dfs(x+2,c1,c2+1);
??????????????????? cnt[i][j]+=2;
??????????????????? if(flag)
??????????????????????? return 1;
??????????????? }
??????? return 0;
??? }
??? else
??? {
??????? for(i=0;i<4;i++)
??????????? for(j=1;j<=9;j++)
??????????? {
??????????????? if(cnt[i][j]==0)
??????????????????? continue;
??????????????? else if(cnt[i][j]==1)
??????????????? {
??????????????????? if(i!=3 && cnt[i][j+1]>0 && cnt[i][j+2]>0)
??????????????????? {
??????????????????????? cnt[i][j]--,cnt[i][j+1]--,cnt[i][j+2]--;
??????????????????????? flag|=dfs(x+3,c1+1,c2);
??????????????????????? cnt[i][j]++,cnt[i][j+1]++,cnt[i][j+2]++;
??????????????????????? if(flag)
??????????????????????????? return 1;
??????????????????? }
??????????????????? else
??????????????????????? return 0;
??????????????? }
??????????????? else if(cnt[i][j]==2)
??????????????? {
??????????????????? if(i!=3 && cnt[i][j+1]>=2 && cnt[i][j+2]>=2)
??????????????????? {
??????????????????????? cnt[i][j]-=2,cnt[i][j+1]-=2,cnt[i][j+2]-=2;
??????????????????????? flag|=dfs(x+6,c1+2,c2);
??????????????????????? cnt[i][j]+=2,cnt[i][j+1]+=2,cnt[i][j+2]+=2;
??????????????????????? if(flag)
??????????????????????????? return 1;
??????????????????? }
??????????????????? else
??????????????????????? return 0;
??????????????? }
??????????????? else if(cnt[i][j]==3)
??????????????? {
??????????????????? cnt[i][j]-=3;
??????????????????? flag|=dfs(x+3,c1+1,c2);
??????????????????? cnt[i][j]+=3;
??????????????????? if(flag)
??????????????????????? return 1;
??????????????????? if(i!=3 && cnt[i][j+1]==3 && cnt[i][j+2]==3)
??????????????????? {
??????????????????????? cnt[i][j]-=3,cnt[i][j+1]-=3,cnt[i][j+2]-=3;
??????????????????????? flag|=dfs(x+9,c1+3,c2);
??????????????????????? cnt[i][j]+=3,cnt[i][j+1]+=3,cnt[i][j+2]+=3;
??????????????????????? if(flag)
??????????????????????????? return 1;
??????????????????? }
??????????????????? else
??????????????????????? return 0;
??????????????? }
??????????????? else if(cnt[i][j]==4)
??????????????? {
??????????????????? cnt[i][j]-=3;
??????????????????? flag|=dfs(x+3,c1+1,c2);
??????????????????? cnt[i][j]+=3;
??????????????????? if(flag)
??????????????????????? return 1;
??????????????????? if(i!=3 && cnt[i][j+1]==4 && cnt[i][j+2]==4)
??????????????????? {
??????????????????????? cnt[i][j]-=4,cnt[i][j+1]-=4,cnt[i][j+2]-=4;
??????????????????????? flag|=dfs(x+12,c1+3,c2);
??????????????????????? cnt[i][j]+=4,cnt[i][j+1]+=4,cnt[i][j+2]+=4;
??????????????????????? if(flag)
??????????????????????????? return 1;
??????????????????? }
??????????????????? else
??????????????????????? return 0;
??????????????? }
??????????? }
??? }
}
inline void add(string t)
{
??? switch(t[1])
??? {
??? case 'm':
??????? cnt[0][t[0]-'0']++; break;
??? case 's':
??????? cnt[1][t[0]-'0']++; break;
??? case 'p':
??????? cnt[2][t[0]-'0']++; break;
??? case 'z':
??????? cnt[3][t[0]-'0']++; break;
??? };
}
inline int check()
{
??? int i,j;
??? for(i=0;i<4;i++)
??????? for(j=1;j<=9;j++)
??????????? if(cnt[i][j]>4)
??????????????? return 0;
??? return 1;
}
int main()
{
??? int i,j;
??? string str;
??? while(cin>>str)
??? {
??????? memset(cnt,0,sizeof(cnt));
??????? add(str);
??????? for(i=0;i<12;i++)
??????????? cin>>str, add(str);
??????? /*for(i=0;i<4;i++)
??????? {
??????????? for(j=1;j<=9;j++)
??????????????? cout<<cnt[i][j]<<' ';
??????????? cout<<endl;
??????? }*/
??????? for(i=0;i<3;i++)
??????????? for(j=1;j<=9;j++)
??????????? {
??????????????? tmp="";
??????????????? tmp+='0'+j;
??????????????? tmp+=mian[i];
??????????????? cnt[i][j]++;
??????????????? if(check() && dfs(0,0,0))
??????????????????? cout<<tmp<<endl;
??????????????? cnt[i][j]--;
??????????? }
??????? for(j=1;j<=7;j++)
??????? {
??????????? tmp="";
??????????? tmp+='0'+j;
??????????? tmp+=mian[3];
??????????? cnt[3][j]++;
??????????? if(check() && dfs(0,0,0))
??????????????? cout<<tmp<<endl;
??????????? cnt[3][j]--;
??????? }
??? }
??? return 0;
}
/*
1s 2s 3s 4s 5s 6s 7s 8s 9s 1z 1z 3p 4p
*/
?
轉載于:https://www.cnblogs.com/canchan/p/11072521.html
總結
以上是生活随笔為你收集整理的2019计蒜之道 B:个性化评测系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的CSS学习笔记
- 下一篇: hdu 5511 Minimum Cut