The Dole Queue UVa 133

 

 思路:直接模拟即可

鬼知道为什么这道题我写了100多行...快赶上网络流和平衡树的代码量了

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const  int N = 110;
  4 bool st[N];
  5 int n,k,m;
  6 int lasta,lastb;
  7 bool check()
  8 {
  9     bool flag=true;
 10     for(int i=1;i<=n;i++)
 11         if(st[i]==false)
 12         {
 13             return false;
 14         }
 15     return true;
 16 }
 17 
 18 void worka(int nowa,int cnta)
 19 {
 20     if(cnta==k)
 21     {
 22         lasta=nowa;
 23         return;
 24     }
 25     int nexta=nowa+1;
 26     while(1)
 27     {
 28         if(nexta>n)nexta=nexta-n;
 29         if(st[nexta]==false)break;
 30         nexta++;
 31     }
 32     worka(nexta,cnta+1);
 33 }
 34 
 35 void workb(int nowb,int cntb)
 36 {
 37     if(cntb==m)
 38     {
 39         lastb=nowb;
 40         return;
 41     }
 42     int nextb=nowb-1;
 43     while(1)
 44     {
 45         if(nextb<1)nextb=n;
 46         if(st[nextb]==false)break;
 47         nextb--;
 48     }
 49     workb(nextb,cntb+1);
 50 }
 51 
 52 int find_a()
 53 {
 54     int nowa=lasta;
 55     while(1)
 56     {
 57         if(nowa>n)nowa=nowa-n;
 58         if(st[nowa]==false)break;
 59         nowa++;
 60     }
 61     return nowa;
 62 }
 63 
 64 int find_b()
 65 {
 66     int nowb=lastb;
 67     while(1)
 68     {
 69         if(nowb<1)nowb=n;
 70         if(st[nowb]==false)break;
 71         nowb--;
 72     }
 73     return nowb;
 74 }
 75 
 76 int cal()
 77 {
 78     int cnt=0;
 79     for(int i=1;i<=n;i++)
 80         if(st[i]==false)cnt++;
 81     return cnt;
 82 }
 83 
 84 int main()
 85 {
 86     while(cin>>n>>k>>m,n||k||m)
 87     {
 88         memset(st,0,sizeof st);
 89         lasta=1,lastb=n;
 90         while(1)
 91         {
 92             if(check()==true)
 93             {
 94                 cout<<endl;
 95                 break;
 96             }
 97             int nowa,nowb;
 98             nowa=find_a(),nowb=find_b();
 99             worka(nowa,1);
100             workb(nowb,1);
101             st[lasta]=true;
102             st[lastb]=true;
103             
104             if(lasta==lastb)
105             {
106                 printf("%3d",lasta);
107             }
108             else
109             {
110                 printf("%3d%3d",lasta,lastb);
111             }
112             int cnt_false=cal();
113             if(cnt_false>0)printf(",");
114         }
115     }
116     return 0;
117 }

 

posted @ 2021-05-15 16:43  Kaoru1120  阅读(25)  评论(0)    收藏  举报