Light OJ 1303——Basic Data Structure

本文记录了一次使用数组模拟队列解决轮转问题的过程。作者最初因理解题目错误及忽视运算符优先级导致调试困难,最终通过增加括号解决了问题。文章详细展示了问题的解决过程及完整的代码实现。

调了一下午的水题,一开始是题目读错了……然后把queue直接用数组模拟,调了好久还是不对,直到前面才发现是pos = check(w, l, r) == -1出错了,==是先执行的,这样的话pos就被赋为0或1了,这样子就必然杯具。最后加个括号即可。

在调的时候我始终都不知道算法哪里错了……从注释的长度就可以看出来了,弱爆了鄙视


Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n, m;
bool visit[22][22];
int c[22], q[111111];

int check(int w, int l, int r)
{
     for(int i = l; i < r; i ++)
             if(visit[q[i]][w] == 0)
                             return i;
     return -1;
}

int main()
{
    int t = 1, cse, i, j;
    scanf("%d", &cse);
    while(cse --)
    {
              scanf("%d%d", &n, &m);
              int wheel[22], l = 0, r = n;    
              memset(visit, 0, sizeof(visit));
              memset(c, 0, sizeof(c));
              memset(wheel, -1, sizeof(wheel));
              memset(q, 0, sizeof(q));
              for(i = 0; i < n; i ++)
                    q[i] = i;
              int cnt = 0, tot = n * m, ti = 0, w = 0;
              ti ++;
              while(cnt != tot)
              {
                        while(l == r) // if the queue is empty
                        {
                                        while(wheel[w] == -1) // if current wheel is not occupied
                                        {
                                                    w ++;
                                                    w %= m;
                                                    ti ++; //switch to next wheel
                                        }
                                        c[wheel[w]] ++; // the people on the wheel goes down
                                        if(c[wheel[w]] != m)
                                                       q[r ++] = wheel[w]; //if it's the mth time he goes down, then don't let him go into the queue
                                        wheel[w] = -1; //current wheel is available
                                        cnt ++; // tot times plus one
                                        if(cnt >= tot) // if there are no people on the wheel, then break...to avoid while again
                                               break;
                        }
                        if(cnt >= tot) // to avoid an empty queue
                               break;
                        int pos;
                        while((pos = check(w, l, r)) == -1) // if the wheel has been visited by all people
                        {
                                            if(wheel[w] != -1) // if someone is on the wheel
                                            {
                                                        c[wheel[w]] ++;
                                                        if(c[wheel[w]] != m) // if he hasn't visited all those m wheels, push him into the queue again
                                                                q[r ++] = wheel[w];
                                                        wheel[w] = -1; //the wheel is avalable again
                                                        cnt ++;
                                            }
                                            w ++; // switch to next wheel
                                            w %= m;
                                            ti ++;
                        }
                        if(wheel[w] != -1) // if the wheel someone is going to get on is occupied by another person
                        {
                                    c[wheel[w]] ++;
                                    if(c[wheel[w]] != m)
                                                   q[r ++] = wheel[w];
                                    wheel[w] = -1;
                                    cnt ++; // let the man down
                        }
                        visit[q[pos]][w] = 1; // let the person who meets the condition get onto the wheel
                        wheel[w] = q[pos];
                        r --; // minus one of the size of queue
                        for(i = pos; i < r; i ++)
                              q[i] = q[i+1];
                        w ++;
                        w %= m;
                        ti ++;
              }
              printf("Case %d: %d\n", t ++, ti * 5);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值