Flip Game

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.

Write a function to compute all possible states of the string after one valid move.

For example, given s = "++++", after one move, it may become one of the following states:

[
  "--++",
  "+--+",
  "++--"
]

If there is no valid move, return an empty list [].

 1 class Solution {
 2 public:
 3     vector<string> generatePossibleNextMoves(string s) {
 4         vector<string> result;
 5         if (s.size() < 2) return result;
 6
 7         for (int i = 1; i < s.size(); i++) {
 8             if (s[i] == ‘+‘ && s[i - 1] == ‘+‘) {
 9                 s[i] = s[i - 1] = ‘-‘;
10                 result.push_back(s);
11                 s[i] = s[i - 1] = ‘+‘;
12             }
13         }
14         return result;
15     }
16 };
时间: 2024-10-01 04:58:44

Flip Game的相关文章

POJ 1753 Flip game状态压缩+广搜

题意有4*4的16个方格,每个方格有黑白两种颜色,每次点击方格后,被点击方格本身及其上下左右的方格都会改变颜色.给出一组状态,求将这组状态变为全白或者全黑至少需要点击几次.若无法达到,则输出Impossible. 样例输入bwwbbbwbbwwbbwww 样例输出4 思路每个方格只有黑白两种颜色,且只有16个方格,因此可以把每个状态看作一个16位的二进制数(状态压缩),2^16=25536,因此可以用int来保存状态.然后就是BFS暴搜,直到状态为0或者65535(全1)为止. 注意点65535

hdu 4146 Flip Game

Flip Game Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1800    Accepted Submission(s): 589 Problem Description Flip game is played on a square N*N field with two-sided pieces placed on each

poj1753 Flip Game

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you f

POJ 1753 Flip Game

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31424   Accepted: 13656 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the

uva10327 - Flip Sort

Flip Sort Sorting in computer science is an important part. Almost every problem can be solved effeciently if sorted data are found. There are some excellent sorting algorithm which has already acheived the lower bound nlgn. In this problem we will a

poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the o

LeetCode Flip Game II

原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 类似Flip Game. 若是有一段"++", 剩下的段和"--"组合 can not win, 那么返回true. 从头到尾试遍了没找到这么一段"++", 返回false. Time Complexity: exponential. AC Java: 1 public class Solution { 2 public boolean can

POJ_1753——Flip Game(枚举)

Flip Game Time Limit: 1000MS Memory Limit: 65536K Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying ei

Leetcode Flip Game

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer

java-第六章-for-计数100遇到3的倍替换单词Flip,5的倍数替换单词Flop,即使3,5倍数替换FlopFlip

public class A02 { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 0; i <100; i++) { if(i%3==0&i%5==0){ System.out.println("FlipFlio"); }else if(i%5==0){ System.out.println("Flop"); }else