Codeforces 74B Train (博弈 & 贪心)

33 篇文章 1 订阅

 

B. Train

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A stowaway and a controller play the following game.

The train is represented by n wagons which are numbered with positive integers from 1 to n from the head to the tail. The stowaway and the controller are initially in some two different wagons. Every minute the train can be in one of two conditions — moving or idle. Every minute the players move.

The controller's move is as follows. The controller has the movement direction — to the train's head or to its tail. During a move the controller moves to the neighbouring wagon correspondingly to its movement direction. If at the end of his move the controller enters the 1-st or the n-th wagon, that he changes the direction of his movement into the other one. In other words, the controller cyclically goes from the train's head to its tail and back again during all the time of a game, shifting during each move by one wagon. Note, that the controller always have exactly one possible move.

The stowaway's move depends from the state of the train. If the train is moving, then the stowaway can shift to one of neighbouring wagons or he can stay where he is without moving. If the train is at a station and is idle, then the stowaway leaves the train (i.e. he is now not present in any train wagon) and then, if it is not the terminal train station, he enters the train again into any of n wagons (not necessarily into the one he's just left and not necessarily into the neighbouring one). If the train is idle for several minutes then each such minute the stowaway leaves the train and enters it back.

Let's determine the order of the players' moves. If at the given minute the train is moving, then first the stowaway moves and then the controller does. If at this minute the train is idle, then first the stowaway leaves the train, then the controller moves and then the stowaway enters the train.

If at some point in time the stowaway and the controller happen to be in one wagon, then the controller wins: he makes the stowaway pay fine. If after a while the stowaway reaches the terminal train station, then the stowaway wins: he simply leaves the station during his move and never returns there again.

At any moment of time the players know each other's positions. The players play in the optimal way. Specifically, if the controller wins, then the stowaway plays so as to lose as late as possible. As all the possible moves for the controller are determined uniquely, then he is considered to play optimally always. Determine the winner.

Input

The first line contains three integers nm and k. They represent the number of wagons in the train, the stowaway's and the controller's initial positions correspondingly (2 ≤ n ≤ 50, 1 ≤ m, k ≤ nm ≠ k).

The second line contains the direction in which a controller moves. "to head" means that the controller moves to the train's head and "to tail" means that the controller moves to its tail. It is guaranteed that in the direction in which the controller is moving, there is at least one wagon. Wagon 1 is the head, and wagon n is the tail.

The third line has the length from 1 to 200 and consists of symbols "0" and "1". The i-th symbol contains information about the train's state at the i-th minute of time. "0" means that in this very minute the train moves and "1" means that the train in this very minute stands idle. The last symbol of the third line is always "1" — that's the terminal train station.

Output

If the stowaway wins, print "Stowaway" without quotes. Otherwise, print "Controller" again without quotes, then, separated by a space, print the number of a minute, at which the stowaway will be caught.

Examples

input

Copy

5 3 2
to head
0001001

output

Stowaway

input

Copy

3 2 1
to tail
0001

output

Controller 2

 

【题目链接】http://codeforces.com/problemset/problem/74/B

 

【题意】有一个火车,有n节车厢,有两个人,分别为偷渡者和管理者,车头在1号车厢,车尾在n号车厢,to head 表示去车头,to tail表示去车尾,第三行输入一行字符串,第i个字符表示第i分钟的状态,为0表示当前火车正在行进,在当前状态偷渡者有两种选择,移动到下一个车厢或者不动;为1表示当前火车停靠在某站,则偷渡者要下火车,并且重新选择1-n中的任一车厢上车。不管0状态还是1状态,管理者都要一直往下一个车厢移动。终点站的火车状态总为1,在到达终点站之前,如果两个人会在相同的车厢相遇,则管理者胜(管理者胜利的话要输出在第几分钟抓住了偷渡者),否则,偷渡者胜利。

【思路】状态为0时,当管理者将要移动的车厢为偷渡者的车厢时,则偷渡者要按照管理者的移动方向往下一个车厢移动,无法移动则偷渡者被抓;当状态为1时,则偷渡车重新选择车厢时应该选择一个距离管理者相对距离最远的一个。

其中  dir=1   表示往尾部移动,  dir=0  表示往头部移动

【代码如下】

代码巨丑,研究完大佬代码后会继续更新代码

 

#include <bits/stdc++.h>
using namespace std;

int n,m,k;
char sta[5],stb[5],str[310];

int main(){
    scanf("%d%d%d",&n,&k,&m);
    scanf("%s%s",sta,stb);
    scanf("%s",str);
    int dir = 0;
    if(stb[0] == 't') dir = 1;
    int len = strlen(str),flag=0;
    for(int i = 0; i < len; i ++){
        if(str[i] == '0'){
            if(dir){
                if(m<n&&m+1 != k) m ++;
                else if(m == n){
                     if(m-1 != k ) m --, dir = 0;
                     else{
                        printf("Controller %d\n",i+1); flag = 1;  break;
                     }
                }
                else{
                    if(k < n) k ++, m ++;
                    else{
                        printf("Controller %d\n",i+1); flag = 1;  break;
                    }
                }
            }
            else{
                if(m>1&&m-1 != k) m --;
                else if(m == 1){
                    if(m+1 != k)  m ++, dir = 1;
                    else{
                        printf("Controller %d\n",i+1); flag = 1;  break;
                    }
                }
                else{
                    if(k > 1) k --, m --;
                    else{
                        printf("Controller %d\n",i+1); flag = 1;  break;
                    }
                }
            }
        }
        else{
            if(dir){
                if(m < n) m ++;
                else m --, dir=0;
            }
            else{
               if(m > 1) m --;
               else m ++, dir=1;
            }
            if(dir) k = 1;
            else k = n;
        }
    }
    if(!flag) printf("Stowaway\n");
    return 0;
}

【代码2】

#include <bits/stdc++.h>
using namespace std;

char str[210],s[10];
int n,m,k;

int main(){
    scanf("%d%d%d",&n,&k,&m);
    scanf("\nto %s",&s);
    scanf("%s",str);
    k = k<m? 0 : 1;
    int dir = s[0] == 'h'? 1:0;
    int len = strlen(str),flag=0;
    for(int i = 0; i < len; i ++){
        if(str[i]-48) k = dir;
        if(dir) m --; else m ++;
        if(m == n || m == 1){
            if((m == n) == k){
                printf("Controller %d\n",i+1); flag = 1; break;
            }
            dir = !dir;
        }
    }
    if(!flag) printf("Stowaway\n");
    return 0;
}

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值