SRM 590 1A 2014.5.26

SRM 590 1A 2014.5.26

DIV1

250

Problem Statement

    

Fox Ciel is playing a game with some chess pawns and a strip of paper. The strip of paper is divided into a row of cells. Each cell can contain at most one pawn.
The white pawns will be denoted 'R', because they are only allowed to move to the right.
The black pawns will be denoted 'L', because they are only allowed to move to the left.


You are given a string begin that describes the current state of the strip: 'R' and 'L' are cells with corresponding pawns, and '.' are empty cells.


You are now allowed to make some moves (as many as you wish, possibly zero). In each move you may either take a 'R' pawn and move it one cell to the right, or take a 'L' pawn and move it one cell to the left. Of course, both types of moves are only valid if the destination cell is currently empty. Pawns are not allowed to move out of bounds.


You are also given a string target that represents the desired state of the strip.
If it is possible to reach target from begin by making some valid moves, return "Possible" (quotes for clarity), otherwise return "Impossible".

Definition

    

Class:

FoxAndChess

Method:

ableToMove

Parameters:

string, string

Returns:

string

Method signature:

string ableToMove(string begin, string target)

(be sure your method is public)

Limits

    

Time limit (s):

2.000

Memory limit (MB):

64

Constraints

-

begin will contain between 1 and 50 elements, inclusive.

-

begin and target will contain the same number of elements.

-

Each character in begin and target will be one of '.', 'L' and 'R'.

Examples

0)

    

"R..."
"..R."
Returns: "Possible"

You have to move the only pawn to the right twice.

1)

    

"..R."
"R..."
Returns: "Impossible"

Here target cannot be reached because the only pawn is only allowed to move to the right.

2)

    

".L.R.R."
"L...R.R"
Returns: "Possible"

3)

    

".L.R."
".R.L."
Returns: "Impossible"

4)

    

"LRLLRLRLLRLLRLRLRL"
"LRLLRLRLLRLLRLRLRL"
Returns: "Possible"

5)

    

"L"
"."
Returns: "Impossible"

This problemstatement is the exclusive and proprietary property of TopCoder, Inc. Anyunauthorized use or reproduction of this information without the prior writtenconsent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. Allrights reserved.

Code:

#include<string>

#include<cstdio>

#include<vector>

 

using namespace std;

 

class FoxAndChess

{

public: string ableToMove(string begin,string target)

{

         stringans;

         stringS="",T="";

         vector<int>L1,L2,R1,R2;

         L1.clear();L2.clear();R1.clear();R2.clear();

         for(inti=0;i<begin.size();i++)

         {

                   if(begin[i]!='.')

                   {

                            S=S+begin[i];

                            if(begin[i]=='L') L1.push_back(i); else R1.push_back(i);

                   }

                   if(target[i]!='.')

                   {

                            T=T+target[i];

                            if(target[i]=='L') L2.push_back(i); else R2.push_back(i);

                   }

         }

 

         for(intj=0;j<S.size();j++)

                   if(S[j]!=T[j])

                   {

                            ans="Impossible";

                            returnans;

                   }

         for(intk=0;k<L1.size();k++)

         {

                   if(L1[k]<L2[k])

                   {

                            ans="Impossible";

                            returnans;

                   }

         }

         for(intk=0;k<R1.size();k++)

         {

                   if(R1[k]>R2[k])

                   {

                            ans="Impossible";

                            returnans;

                   }

         }

         ans="Possible";

         returnans;

}

};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值