POJ 1753 枚举

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36371 Accepted: 15851

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 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. 

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

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).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4
 
学习别人代码
 
#include <stdio.h>
#include <stdlib.h>


//所有都是白的,或者所有都是黑的
int all_white_or_black(int* bits, int len)
{
  int i = 0;
  for (i = 0; i < len - 1; i++)
    if (bits[i] != bits[i + 1])
      return 0;
  return 1;
}


//改变一个格子的颜色,并根据其所在位置改变其周围格子的颜色
void change_color(int* arr, int i)
{
  arr[i] = !(arr[i]);
  int x = i/4;
  int y = i%4;
  if (y < 3)
    arr[i + 1] = !(arr[i + 1]);
  if (y > 0)
    arr[i - 1] = !(arr[i - 1]);
  if (x > 0)
    arr[i - 4] = !(arr[i - 4]);
  if (x < 3)
    arr[i + 4] = !(arr[i + 4]);
}


//递归判断
//这个完全用了前一篇文章的递归方法,只是在else语句中添加了整个图形是否为纯色的判断而已
void combine(int* arr, int len, int* result, int count, const int NUM, int* last)
{
  int i;
  for (i = len; i >= count; i--)
  {
    result[count - 1] = i - 1;
    if (count > 1)
      combine(arr, i - 1, result, count - 1, NUM, last);
    else
    {
      int j = 0;
      //在这里生成arr的副本
      int* new_arr = (int*)malloc(sizeof(int)*16);
      for (j = 0; j < 16; j++)
        new_arr[j] = arr[j];


      for (j = NUM - 1; j >=0; j--)
      {
         change_color(new_arr, result[j]);
      }
      if (all_white_or_black(new_arr, 16))
      {
         *last = NUM;
         free(new_arr);
         break;
      }
      free(new_arr);
    }
  }
}


int main()
{
  char str[5];
  int bits[16];
  int count = 15;
  int lines = 4;
  while (lines--)
  {
    scanf("%s", str);
    int i;
    for (i = 0; i < 4; i++)
    {
      if (str[i] == 'b')
        bits[count--] = 1;
      else
        bits[count--] = 0;
    }
  }


  if (all_white_or_black(bits, 16))
    printf("%d\n", 0);
  else
  {
    //生成bits数组的副本
    int* new_bits = (int*)malloc(sizeof(int)*16);
    int i;
    for (i = 0; i < 16; i++)
      new_bits[i] = bits[i];


    int j;
    //这里last用来接受combine函数里面的NUM,即需要的步数
    int last = 0;
    for (j = 1; j <= 16; j++)
    {
      int* result = (int*)malloc(sizeof(int)*j);
      combine(new_bits, 16, result, j, j, &last);
      if (last == j)
      {
        printf("%d\n", last);
        break;
      }
      //new_bits已被改变,所以要还原为bits
      for (i = 0; i < 16; i++)
        new_bits[i] = bits[i];
      free(result);
    }
    free(new_bits);
    if (j == 17)
      printf("Impossible\n");
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1753的题目描述为:有一个4×4的棋盘,棋盘中有16个棋子,其中有14个棋子是黑色的,用B表示;另外两个棋子是白色的,用W表示。现在要求移动棋子,将两个白色棋子移到一起,移动棋子方式是把与白色棋子相邻(上下左右,而非斜向相邻)的棋子移到空位上。现在,请你求出最少需要移动多少次。 题目看起来很简单,但是要考虑各种情况,一般在处理类似的搜索问题时,我们使用Breath First Search (BFS)来解决问题。 BFS 是一种优秀的遍历搜索算法,广泛应用于许多问题,特别是计算机科学和人工智能。BFS 只需要进行一次完整的搜索即可找到问题的最短路径或解决方案。 在这道题目中,我们可以使用 BFS 来解决问题。 我们首先需要定义状态的表示方式,可以这么表示: 1. 4*4的数组board表示状态。 2. 一个结构体Node,代表搜索树的每个节点。其中状态的表示形式为board。还有一些列信息,包括横,纵坐标,深度depth,以及方向dir。 我们使用 queue 来存储每一层需要遍历的结点,对于每个结点,我们枚举它可以到达的状态,并将这些状态添加到队列中,继续进行下一层的遍历。直到达到目标状态。 因此,我们的搜索过程主要包括以下的步骤: 1. 判断当前状态是否是目标状态 2. 枚举当前状态可能到达的所有状态,并判断是否合法 3. 如果该状态未被访问过,添加该状态,进行遍历。 知道了上面的步骤,我们就可以使用 bfs 来解决问题了。 具体实现可以参考以下代码:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值