• OpenJudge/Poj 1753 Flip Game


    1.链接地址:

    http://bailian.openjudge.cn/practice/1753/

    http://poj.org/problem?id=1753

    2.题目:

    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述
    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 flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
    1. Choose any one of the 16 pieces.
    2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

    Consider the following position as an example:

    bwbw
    wwww
    bbwb
    bwwb
    Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

    bwbw
    bwww
    wwwb
    wwwb
    The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
    输入
    The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
    输出
    Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
    样例输入
    bwwb
    bbwb
    bwwb
    bwww
    样例输出
    4
    
    来源
    Northeastern Europe 2000

    3.思路:

    4.代码:

     1 #include "assert.h"
     2  #include <iostream>
     3  #include <queue>
     4 #include <cstring>
     5 #include <cstdlib>
     6  using namespace std;
     7  
     8  const int MAX_STATE = 65536;
     9  const int ALL_WHILE_STATE = 0;
    10  const int ALL_BLACK_STATE = 65535;
    11  const int WIDTH_OF_BOARD = 4;
    12  const int SIZE_OF_BOARD = WIDTH_OF_BOARD * WIDTH_OF_BOARD; // 4 * 4
    13  
    14  int ConvertPieceColorToInt(char color)
    15  {
    16      switch(color)
    17      {
    18      case 'b':
    19          return 1;
    20      case 'w':
    21          return 0;
    22      }
    23  }
    24  
    25  int FlipPiece(int state_id, int position)
    26  {
    27      state_id ^= (1 << position);
    28  
    29      // up
    30      if(position - 4 >= 0)
    31          state_id ^= (1 << (position - 4));
    32      // down
    33      if(position + 4 < SIZE_OF_BOARD)
    34          state_id ^= (1 << (position + 4));
    35      // left
    36      if(position % 4 != 0)
    37          state_id ^= (1 << (position - 1));
    38      // right
    39      if(position % 4 != 3)
    40          state_id ^= (1 << (position + 1));
    41  
    42      return state_id;
    43  }
    44  
    45  int main()
    46  {
    47      int current_state_id = 0;
    48      int state[MAX_STATE];
    49      queue<int> search_queue;
    50  
    51      memset(state, -1, sizeof(state));
    52  
    53      char color;
    54  
    55      for(int i = 0; i < SIZE_OF_BOARD; ++i)
    56      {
    57          cin >> color;
    58          current_state_id += ConvertPieceColorToInt(color) << i;
    59      }
    60  
    61      if(current_state_id == ALL_WHILE_STATE
    62          || current_state_id == ALL_BLACK_STATE)
    63      {
    64          cout << "0" << endl;
    65          return 0;
    66      }
    67  
    68      state[current_state_id] = 0;
    69      search_queue.push(current_state_id);
    70  
    71      int next_state_id;
    72  
    73      while(!search_queue.empty())
    74      {
    75          current_state_id = search_queue.front();
    76          search_queue.pop();
    77  
    78          for(int i = 0; i < SIZE_OF_BOARD; ++i)
    79          {
    80              next_state_id = FlipPiece(current_state_id, i);
    81              if(next_state_id == ALL_WHILE_STATE
    82                  || next_state_id == ALL_BLACK_STATE)
    83              {
    84                  cout << state[current_state_id] + 1 << endl;
    85                  return 0;
    86              }
    87              assert(next_state_id < MAX_STATE);
    88              if(state[next_state_id] == -1 /* not visited */)
    89              {
    90                  state[next_state_id] = state[current_state_id] + 1;
    91                  search_queue.push(next_state_id);
    92              }
    93          }
    94      }
    95  
    96      cout << "Impossible" << endl;
    97      return 0;
    98 }
  • 相关阅读:
    Android 千牛数据库分析
    Flutter 你需要知道的那些事 01
    Flutter 异常处理之图片篇
    Flutter 即学即用系列博客——09 EventChannel 实现原生与 Flutter 通信(一)
    Flutter 即学即用系列博客——08 MethodChannel 实现 Flutter 与原生通信
    Flutter 即学即用系列博客——06 超实用 Widget 集锦
    Kingfisher-swift缓存图片框架
    swift冒泡排序,swift快速排序,swift归并排序,swift插入排序,swift基数排序
    swift声明属性为某个类型同时遵循某协议
    Xcode真机运行报错iPhone has denied the launch request
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3570578.html
Copyright © 2020-2023  润新知