BZOJ 1028

1 #include <cstdio>
2 const int Maxn=510;
3 int n,m,a[Maxn],b[Maxn],cnt,Ans[Maxn],x;
4 bool Check()
5 {
6     for (int i=1;i<=n;i++)
7         if (a[i]>=2)
8         {
9             for (int j=1;j<=n+2;j++) b[j]=a[j];
10             b[i]-=2; bool flag=true;
11             for (int j=1;j<=n;j++)
12                 if (b[j])
13                 {
14                     if (b[j]<0) {flag=false; break;}
15                     int t=b[j]%3;
16                     b[j+1]-=t,b[j+2]-=t;
17                 }
18             if (!flag || b[n+1]<0 || b[n+2]<0) continue;
19             return true;
20         }
21     return false;
22 }
23
24 int main()
25 {
26     scanf("%d%d",&n,&m);
27     for (int i=1;i<=3*m+1;i++) scanf("%d",&x),a[x]++;
28     for (int i=1;i<=n;i++)
29     {
30         a[i]++;
31         if (Check()) Ans[++cnt]=i;
32         a[i]--;
33     }
34     if (cnt==0) {puts("NO"); return 0;}
35     for (int i=1;i<cnt;i++) printf("%d ",Ans[i]); printf("%d\n",Ans[cnt]);
36     return 0;
37 }
BZOJ 1028

BZOJ 1860

1 #include<cstdio>
2 #include<set>
3 #define LL long long
4 using namespace std;
5 const LL Mod=1000000000000000003;
6 set<LL>S;
7 LL KASE,Bin[105],a[105],Sum,mul=131ll;
8 inline bool Dfs(LL k,bool two,LL Sta)
9 {
10   if (S.find(Sta)!=S.end()) return 0;
11   S.insert(Sta);
12   while (!a[k]&&k<=100) k++;
13   if (k==101) return two;
14   if (a[k]&&a[k+1]&&a[k+2]&&k<=98)
15   {
16     a[k]--;a[k+1]--;a[k+2]--;
17     if (Dfs(k,two,(Sta-Bin[k]-Bin[k+1]-Bin[k+2]+3*Mod)%Mod)) return true;
18     a[k]++;a[k+1]++;a[k+2]++;
19   }
20   if (a[k]>=4)
21   {
22       a[k]-=4;
23       if (Dfs(k,two,(Sta-Bin[k]*4+4*Mod)%Mod)) return true;
24       a[k]+=4;
25   }
26   if (a[k]>=3)
27   {
28       a[k]-=3;
29       if (Dfs(k,two,(Sta-Bin[k]*3+3*Mod)%Mod)) return true;
30       a[k]+=3;
31   }
32   if (a[k]>=2&&!two)
33   {
34       a[k]-=2;
35       if (Dfs(k,1,(Sta-Bin[k]*2-Bin[100]+3*Mod)%Mod)) return true;
36       a[k]+=2;
37   }
38   return false;
39 }
40 int main()
41 {
42     Bin[1]=1; for (int i=2;i<=100;i++) Bin[i]=(Bin[i-1]*mul)%Mod;
43     scanf("%d",&KASE);
44     for (int Kase=1;Kase<=KASE;Kase++)
45     {
46         Sum=0;
47         for (int i=1;i<=100;i++) scanf("%lld",&a[i]),Sum=(Sum+Bin[i]*a[i])%Mod;
48         S.clear();
49         if (Dfs(1,0,Sum)) puts("Yes"); else puts("No");
50     }
51     return 0;
52 }
BZOJ 1860

POJ 1077 BFS+康拓展开

1 #include <cstdio>
2 int Fac[20];
3 struct Data
4 {
5     int a[10];
6     int Father,Pos,Direct;
7 }Q[4000000],u;
8 bool vis[4000000];
9 int Ans[50];
10 inline int Cantor()
11 {
12     int Ret=0;
13     for (int i=0;i<9;i++)
14     {
15         int Tmp=0;
16         for (int j=i+1;j<9;j++)
17             if (u.a[j]<u.a[i]) Tmp++;
18         Ret+=Tmp*Fac[9-i-1];
19     }
20     return Ret;
21 }
22 int main()
23 {
24     Fac[0]=1; for (int i=1;i<=9;i++) Fac[i]=Fac[i-1]*i;
25     for (int i=0;i<9;i++)
26     {
27         char ch=getchar();
28         while (ch==' ') ch=getchar();
29         if (ch=='x') Q[1].a[i]=0,Q[1].Pos=i; else Q[1].a[i]=ch-'0';
30     }
31     u=Q[1];
32     int l=1,r=1;
33     while (l<=r)
34     {
35         u=Q[l]; int Tmp;
36         int Statue=Cantor();
37         // printf("%d\n",Statue);
38         if (Statue==46233)
39         {
40             int k=l,tot=0;
41             while (k!=0)
42             {
43                 Ans[++tot]=Q[k].Direct;
44                 k=Q[k].Father;
45             }
46             for (int i=tot;i>=1;i--)
47             {
48                 if (Ans[i]==1) putchar('u');
49                 if (Ans[i]==2) putchar('l');
50                 if (Ans[i]==3) putchar('r');
51                 if (Ans[i]==4) putchar('d');
52             }
53             return 0;
54         }
55         vis[Statue]=true;
56         int k=u.Pos;
57         if (k>=3)
58         {
59             Tmp=u.a[k]; u.a[k]=u.a[k-3]; u.a[k-3]=Tmp;
60             Statue=Cantor();
61             if (vis[Statue]==0)
62             {
63                 r++;
64                 Q[r]=u;
65                 Q[r].Pos=k-3;
66                 Q[r].Father=l;
67                 Q[r].Direct=1;
68                 vis[Statue]=true;
69             }
70              Tmp=u.a[k]; u.a[k]=u.a[k-3]; u.a[k-3]=Tmp;
71         }
72         if (k%3!=0)
73         {
74             Tmp=u.a[k]; u.a[k]=u.a[k-1]; u.a[k-1]=Tmp;
75             Statue=Cantor();
76             if (vis[Statue]==0)
77             {
78                 r++;
79                 Q[r]=u;
80                 Q[r].Pos=k-1;
81                 Q[r].Father=l;
82                 Q[r].Direct=2;
83                 vis[Statue]=true;
84             }
85              Tmp=u.a[k]; u.a[k]=u.a[k-1]; u.a[k-1]=Tmp;
86         }
87         if (k%3!=2)
88         {
89             Tmp=u.a[k]; u.a[k]=u.a[k+1]; u.a[k+1]=Tmp;
90             Statue=Cantor();
91             if (vis[Statue]==0)
92             {
93                 r++;
94                 Q[r]=u;
95                 Q[r].Pos=k+1;
96                 Q[r].Father=l;
97                 Q[r].Direct=3;
98                 vis[Statue]=true;
99             }
100              Tmp=u.a[k]; u.a[k]=u.a[k+1]; u.a[k+1]=Tmp;
101         }
102         if (k<6)
103         {
104             Tmp=u.a[k]; u.a[k]=u.a[k+3]; u.a[k+3]=Tmp;
105             Statue=Cantor();
106             if (vis[Statue]==0)
107             {
108                 r++;
109                 Q[r]=u;
110                 Q[r].Pos=k+3;
111                 Q[r].Father=l;
112                 Q[r].Direct=4;
113                 vis[Statue]=true;
114             }
115              Tmp=u.a[k]; u.a[k]=u.a[k+3]; u.a[k+3]=Tmp;
116         }
117         l++;
118     }
119     puts("unsolvable");
120     return 0;
121 }
POJ 1070