zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    指针与数组关联导致的一些现象 分类: H_HISTORY 20130211 20:14 516人阅读 评论(0) 收藏
    宏定义一些内容 分类: H_HISTORY 20130207 23:20 585人阅读 评论(0) 收藏
    使用lstat()判断文件类型 分类: H_HISTORY 20130224 11:48 703人阅读 评论(0) 收藏
    关于VMware虚拟机的上网 分类: C_OHTERS 20130220 14:36 336人阅读 评论(0) 收藏
    Segmentation fault (core dumped) 分类: H_HISTORY 20130206 11:34 18800人阅读 评论(0) 收藏
    C语言内存分配时间 分类: H_HISTORY 20130211 10:51 1432人阅读 评论(3) 收藏
    GTK+与QT的对比 分类: H_HISTORY 20130205 09:27 3101人阅读 评论(0) 收藏
    枚举作为整数 分类: H_HISTORY 20130208 11:22 576人阅读 评论(0) 收藏
    01背包问题,动态规划求解
    求两个字符串的相似度或子串
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3570578.html
Copyright © 2011-2022 走看看