• HDU 4753 Fishhead’s Little Game(DFS)


    题目链接

    很繁琐的爆搜,最多要加2^12条边,暴力就可以,回溯那部分一直没有回溯好,写了一晚上。。。代码非常,非常难看。。对了,还不是普通的爆搜,双向搜索博弈,以前记得看过,这次好像第一次写。。

      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 #include <cmath>
      5 #include <algorithm>
      6 using namespace std;
      7 int p[40][40],flag[101],o[41][41];
      8 int que[30];
      9 int sp[100];
     10 int sf[101];
     11 int num,sz;
     12 int t1,t2;
     13 int dfs1(int x,int tc,int jc);
     14 int dfs2(int x,int tc,int jc);
     15 int fun(int a,int b,int c,int nu)
     16 {
     17     if(sz == 1)
     18     {
     19         if(sf[nu] == 0&&sp[a]&&sp[b]&&sp[c])
     20         {
     21             sf[nu] = 1;
     22             return 1;
     23         }
     24         else
     25             return 0;
     26     }
     27     else
     28     {
     29         if(sf[nu] == 1&&sp[a]&&sp[b]&&sp[c])
     30         {
     31             sf[nu] = 0;
     32             return 1;
     33         }
     34         else
     35             return 0;
     36     }
     37 }
     38 int judge(int x)
     39 {
     40     if(x <= 2)
     41         return fun(12+x,13+x,3+x,x);
     42     else if(x <= 5)
     43         return fun(x-3,x+9,x+10,x-3) + fun(x+3,x+13,x+14,x);
     44     else if(x <= 8)
     45         return fun(x-3,x+10,x+11,x-3) + fun(x+3,x+14,x+15,x);
     46     else if(x <= 11)
     47         return fun(x-3,x+11,x+12,x-3);
     48     else if(x == 12)
     49         return fun(3,0,13,0);
     50     else if(x == 15)
     51         return fun(2,5,14,2);
     52     else if(x == 16)
     53         return fun(3,6,17,3);
     54     else if(x == 19)
     55         return fun(5,8,18,5);
     56     else if(x == 20)
     57         return fun(6,9,21,6);
     58     else if(x == 23)
     59         return fun(8,11,22,8);
     60     else if(x == 14|| x == 13)
     61         return fun(x-1,x-13,x-10,x-13) + fun(x+1,x-12,x-9,x-12);
     62     else if(x == 17||x == 18)
     63         return fun(x-1,x-14,x-11,x-14) + fun(x+1,x-13,x-10,x-13);
     64     else if(x == 22||x == 21)
     65         return fun(x-1,x-15,x-12,x-15) + fun(x+1,x-14,x-11,x-14);
     66     return 0;
     67 }
     68 int dfs1(int x,int tc,int jc)
     69 {
     70     int i,z,s;
     71     if(x == (1<<num)-1)
     72     {
     73         if(tc > jc)
     74             return 1;
     75         else
     76             return 0;
     77     }
     78     s = 0;
     79     for(i = 0; i < num; i ++)
     80     {
     81         if((x&(1<<i)) == 0)
     82         {
     83             sz = 1;
     84             z = judge(que[i]);
     85             sp[que[i]] = 1;
     86             if(dfs2(x+(1<<i),tc+z,jc) == 0)
     87             s = 1;
     88             sp[que[i]] = 0;
     89             sz = 0;
     90             judge(que[i]);
     91         }
     92         if(s) return s;//为了回溯,回溯完了,再返回。
     93     }
     94     return s;
     95 }
     96 int dfs2(int x,int tc,int jc)
     97 {
     98     int i,z,s;
     99     if(x == (1<<num)-1)
    100     {
    101         if(tc > jc)
    102             return 0;
    103         else
    104             return 1;
    105     }
    106     s = 0;
    107     for(i = 0; i < num; i ++)
    108     {
    109         if((x&(1<<i)) == 0)
    110         {
    111             sz = 1;
    112             sp[que[i]] = 1;
    113             z = judge(que[i]);
    114             if(dfs1(x+(1<<i),tc,jc+z) == 0)
    115             s = 1;
    116             sz = 0;
    117             judge(que[i]);
    118             sp[que[i]] = 0;
    119         }
    120         if(s) return s;
    121     }
    122     return s;
    123 }
    124 int main()
    125 {
    126     int i,j,k,t,u,v,n,tc,jc,cas = 1;
    127     k = 1;
    128     for(i = 1; i <= 4; i ++)
    129     {
    130         for(j = 1; j <= 4; j ++)
    131             p[i][j] = k ++;
    132     }
    133     k = 0;
    134     for(i = 1; i <= 4; i ++)
    135     {
    136         for(j = 1; j <= 3; j ++)
    137         {
    138             // printf("%d %d
    ",p[i][j],p[i][j+1]);
    139             o[p[i][j]][p[i][j+1]] = k ++;
    140         }
    141     }
    142     for(i = 1; i <= 3; i ++)
    143     {
    144         for(j = 1; j <= 4; j ++)
    145         {
    146             //printf("%d %d
    ",p[i][j],p[i+1][j]);
    147             o[p[i][j]][p[i+1][j]] = k ++;
    148         }
    149     }
    150     scanf("%d",&t);
    151     while(t--)
    152     {
    153         memset(flag,0,sizeof(flag));
    154         memset(sp,0,sizeof(sp));
    155         memset(sf,0,sizeof(sf));
    156         scanf("%d",&n);
    157         num = 0;
    158         tc = jc = 0;
    159         sz = 1;
    160         for(i = 0; i < n; i ++)
    161         {
    162             scanf("%d%d",&u,&v);
    163             int te;
    164             if(u > v)
    165             {
    166                 te = u;
    167                 u = v;
    168                 v = te;
    169             }
    170             if(i%2 == 0)
    171                 tc += judge(o[u][v]);
    172             else
    173                 jc += judge(o[u][v]);
    174             flag[o[u][v]] = 1;
    175             sp[o[u][v]] = 1;
    176         }
    177         for(i = 0; i < 24; i ++)
    178         {
    179             if(!flag[i])
    180                 que[num++] = i;
    181         }
    182         printf("Case #%d: ",cas ++);
    183         if(n%2 == 0)
    184         {
    185             if(dfs1(0,tc,jc))
    186                 printf("Tom200
    ");
    187             else
    188                 printf("Jerry404
    ");
    189         }
    190         else
    191         {
    192             if(dfs1(0,jc,tc))
    193                 printf("Jerry404
    ");
    194             else
    195                 printf("Tom200
    ");
    196         }
    197     }
    198     return 0;
    199 }
  • 相关阅读:
    6:定位锚点透明
    5:CSS元素类型
    4、css盒模型和文本溢出
    3、CSS属性组成和作用
    Linq to Xml读取复杂xml(带命名空间)
    经典语句
    服务器不能设置内容类型HTTP头信息后发送
    vs2015 VS-Visual Studio-IIS Express 支持局域网访问
    Mysql一些常用语句
    swfupload上传图片
  • 原文地址:https://www.cnblogs.com/naix-x/p/3336182.html
Copyright © 2020-2023  润新知