NYOJ 294 Bot Trust

链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=294

一步一步模拟出来的....

把情况考虑全面: “Both robots know the full sequence in advance”

 
#include <iostream>
#include<cmath>
#include<cstdio>
using namespace std;
class bot
{
    public:
    char col;
    int pos;
};
bot data[105];

int main()
{

   // freopen("1.txt","r",stdin);

    int t;
    int ans;
    int n;
    int a,b;
    int i,j,k;
    cin>>t;
    int to,tb;
    bool s;
    for(k=1;k<=t;k++)
    {
        s=false;
        ans=0;
        to=1;tb=1;
        cin>>n;
        for(i=0;i<n;i++)
            cin>>data[i].col>>data[i].pos;
        i=0;
        while(i<n)
        {

            if(to==data[i].pos&&data[i].col=='O' )
            {

                i++;
                for(j=i; j<n; j++)
                    if(data[j].col=='B')
                    {
                        s=true;
                        break;
                    }
                if(s)
                {
                    if(tb<data[j].pos)
                        tb++;
                    else if(tb>data[j].pos)
                        tb--;
                    s=false;
                }

                ans++;

            }
            else if(tb==data[i].pos&&data[i].col=='B' )
            {


                i++;
                for(j=i; j<n; j++)
                    if(data[j].col=='O')
                    {
                        s=true;
                        break;
                    }
                if(s)
                {
                    if(to<data[j].pos)
                        to++;
                    if(to>data[j].pos)
                        to--;
                    s=false;
                }

                ans++;

            }

            else
            {
                for(j=i; j<n; j++)
                    if(data[j].col=='O')
                    {
                        s=true;
                        break;
                    }
                if(s)
                {

                    if(to<data[j].pos)
                        to++;
                    else if(to>data[j].pos)
                        to--;
                    s=false;
                }
                for(j=i; j<n; j++)
                    if(data[j].col=='B')
                    {
                        s=true;
                        break;
                    }
                if(s)
                {
                    if(tb<data[j].pos)
                        tb++;
                    else if(tb>data[j].pos)
                        tb--;
                    s=false;
                }

                ans++;
            }



        }


        cout<<"Case #"<<k<<": "<<ans<<endl;

    }


    return 0;
}        


 

原文地址:https://www.cnblogs.com/frankM/p/4399505.html