zoukankan      html  css  js  c++  java
  • 2019计蒜之道 B:个性化评测系统

    比赛的时候没有想清楚,直接排序然后从前向后dfs,一直卡着,就是没有想到其他的情况。

    其实有可能是223344这样的,最后写的时候忘记处理了222233334444情况,还是看别人博客才发现的。

    还是太菜了,继续努力。

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <string>
      5 #include <algorithm>
      6 #include <map>
      7 #define ll long long
      8 
      9 using namespace std;
     10 
     11 char mian[10]="mspz";
     12 map<string,int> majong[4];
     13 map<string,int>::iterator itr;
     14 string tmp;
     15 int cnt[4][14];
     16 
     17 int dfs(int x,int c1,int c2)
     18 {
     19     int i,j,flag=0;
     20     if(c1==4 && c2==1)
     21         return 1;
     22     else if(c1>4 || c2>1 || x>=14)
     23         return 0;
     24     if(c2==0)
     25     {
     26         for(i=0;i<4;i++)
     27             for(j=1;j<=9;j++)
     28                 if(cnt[i][j]>=2)
     29                 {
     30                     cnt[i][j]-=2;
     31                     flag|=dfs(x+2,c1,c2+1);
     32                     cnt[i][j]+=2;
     33                     if(flag)
     34                         return 1;
     35                 }
     36         return 0;
     37     }
     38     else
     39     {
     40         for(i=0;i<4;i++)
     41             for(j=1;j<=9;j++)
     42             {
     43                 if(cnt[i][j]==0)
     44                     continue;
     45                 else if(cnt[i][j]==1)
     46                 {
     47                     if(i!=3 && cnt[i][j+1]>0 && cnt[i][j+2]>0)
     48                     {
     49                         cnt[i][j]--,cnt[i][j+1]--,cnt[i][j+2]--;
     50                         flag|=dfs(x+3,c1+1,c2);
     51                         cnt[i][j]++,cnt[i][j+1]++,cnt[i][j+2]++;
     52                         if(flag)
     53                             return 1;
     54                     }
     55                     else
     56                         return 0;
     57                 }
     58                 else if(cnt[i][j]==2)
     59                 {
     60                     if(i!=3 && cnt[i][j+1]>=2 && cnt[i][j+2]>=2)
     61                     {
     62                         cnt[i][j]-=2,cnt[i][j+1]-=2,cnt[i][j+2]-=2;
     63                         flag|=dfs(x+6,c1+2,c2);
     64                         cnt[i][j]+=2,cnt[i][j+1]+=2,cnt[i][j+2]+=2;
     65                         if(flag)
     66                             return 1;
     67                     }
     68                     else
     69                         return 0;
     70                 }
     71                 else if(cnt[i][j]==3)
     72                 {
     73                     cnt[i][j]-=3;
     74                     flag|=dfs(x+3,c1+1,c2);
     75                     cnt[i][j]+=3;
     76                     if(flag)
     77                         return 1;
     78                     if(i!=3 && cnt[i][j+1]==3 && cnt[i][j+2]==3)
     79                     {
     80                         cnt[i][j]-=3,cnt[i][j+1]-=3,cnt[i][j+2]-=3;
     81                         flag|=dfs(x+9,c1+3,c2);
     82                         cnt[i][j]+=3,cnt[i][j+1]+=3,cnt[i][j+2]+=3;
     83                         if(flag)
     84                             return 1;
     85                     }
     86                     else
     87                         return 0;
     88                 }
     89                 else if(cnt[i][j]==4)
     90                 {
     91                     cnt[i][j]-=3;
     92                     flag|=dfs(x+3,c1+1,c2);
     93                     cnt[i][j]+=3;
     94                     if(flag)
     95                         return 1;
     96                     if(i!=3 && cnt[i][j+1]==4 && cnt[i][j+2]==4)
     97                     {
     98                         cnt[i][j]-=4,cnt[i][j+1]-=4,cnt[i][j+2]-=4;
     99                         flag|=dfs(x+12,c1+3,c2);
    100                         cnt[i][j]+=4,cnt[i][j+1]+=4,cnt[i][j+2]+=4;
    101                         if(flag)
    102                             return 1;
    103                     }
    104                     else
    105                         return 0;
    106                 }
    107             }
    108     }
    109 }
    110 
    111 inline void add(string t)
    112 {
    113     switch(t[1])
    114     {
    115     case 'm':
    116         cnt[0][t[0]-'0']++; break;
    117     case 's':
    118         cnt[1][t[0]-'0']++; break;
    119     case 'p':
    120         cnt[2][t[0]-'0']++; break;
    121     case 'z':
    122         cnt[3][t[0]-'0']++; break;
    123     };
    124 }
    125 
    126 inline int check()
    127 {
    128     int i,j;
    129     for(i=0;i<4;i++)
    130         for(j=1;j<=9;j++)
    131             if(cnt[i][j]>4)
    132                 return 0;
    133     return 1;
    134 }
    135 
    136 int main()
    137 {
    138     int i,j;
    139     string str;
    140     while(cin>>str)
    141     {
    142         memset(cnt,0,sizeof(cnt));
    143         add(str);
    144         for(i=0;i<12;i++)
    145             cin>>str, add(str);
    146         /*for(i=0;i<4;i++)
    147         {
    148             for(j=1;j<=9;j++)
    149                 cout<<cnt[i][j]<<' ';
    150             cout<<endl;
    151         }*/
    152 
    153         for(i=0;i<3;i++)
    154             for(j=1;j<=9;j++)
    155             {
    156                 tmp="";
    157                 tmp+='0'+j;
    158                 tmp+=mian[i];
    159                 cnt[i][j]++;
    160                 if(check() && dfs(0,0,0))
    161                     cout<<tmp<<endl;
    162                 cnt[i][j]--;
    163             }
    164         for(j=1;j<=7;j++)
    165         {
    166             tmp="";
    167             tmp+='0'+j;
    168             tmp+=mian[3];
    169             cnt[3][j]++;
    170             if(check() && dfs(0,0,0))
    171                 cout<<tmp<<endl;
    172             cnt[3][j]--;
    173         }
    174     }
    175     return 0;
    176 }
    177 
    178 
    179 /*
    180 
    181 1s 2s 3s 4s 5s 6s 7s 8s 9s 1z 1z 3p 4p
    182 
    183 
    184 */
    View Code
  • 相关阅读:
    laravel常用函数大全Helper
    laravel查询语句指定索引(mysql强制索引)
    laravel-admin后台系统开发
    ES搜索引擎详解
    怎么查看当前的git分支是基于哪个分支创建的
    laravel中使用offsetSet
    laragon安装新的php版本
    Laravel collect妙用
    composer install file could not be downloaded (HTTP/1.1 405 Not Allowed)
    garphql
  • 原文地址:https://www.cnblogs.com/canchan/p/11072521.html
Copyright © 2011-2022 走看看