[topcoder]FoxAndChess

http://community.topcoder.com/stat?c=problem_statement&pm=12725&rd=15702

这题比较简单。首先所有的LR的顺序要一致,二来L和L,R和R的位置有限制关系。

#include <string>

using namespace std;



class FoxAndChess {

public:

    string ableToMove(string begin, string target);

};



string FoxAndChess::ableToMove(string begin, string target) {

    int i = 0;

    int j = 0;

    int len = begin.length();

    while (i < len && j < len) {

        while (i < len && begin[i] == '.') i++;

        while (j < len && target[j] == '.') j++;

        if (i == len && j == len) break;

        if (i == len || j == len) return "Impossible";

        if (begin[i] != target[j]) return "Impossible";

        if ((begin[i] == 'L' && i < j) ||

            (begin[i] == 'R' && i > j)) return "Impossible";

        i++; j++;

    }

    return "Possible";

};

  

你可能感兴趣的:(topcoder)