POJ 1753 Flip Game

这篇博客详细解析了POJ1753FlipGame问题,介绍了解决问题的思路,提供了代码实现,并给出了参考链接。通过广度优先搜索和位运算技巧,展示了如何在有限步内解决翻转棋盘的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ 1753 Flip Game

1题目

===================

Flip Game
Time Limit:1000MS Memory Limit:65536K
Total Submissions:25618 Accepted:11072

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

Source


===================

2思路

这是一个枚举题。首先要注意翻转序列不会重复已经翻过的位置,因为重复翻同一位置, 相当于取消这个操作,这样得到的翻转序列更短。其次是翻转序列是无序的,也就是是 说每一个位置要么被翻转过,要么没有。一共16个位置,只要从16个位置中选0个或者 1个或者2个,。。。,进行翻转,当出现成功时就停止。这时候问题其实转化为在0到15 个数中任意选k个数(0<=k<=15)。我的代码是基于以上思路。

参考了其他人的思路,大部分是通过广度优先搜索做的,还有一个技巧是可以把整个矩阵 看作是一个16位的数字,每位是0或1, 翻转的过程其实是和一个数做异或操作。

3代码

/*
 * Problem: POJ 1753 Flip Game
 * Lang: ANSI C
 * Time: 469MS
 * Memory: 388K
 * Code Length: 1358B
 * Author: minix
 */

#include <stdio.h>
#define N 20
#define M 4

int data[N];
char rect[M][M];

void fan(char tmp[M][M], int i, int j) {
  if (i<0 || j<0 || i>=M || j>=M)
    return;
  if (tmp[i][j] == 'b') tmp[i][j]='w';
  else tmp[i][j] = 'b';
}

void flip(char tmp[M][M], int n) {
  int row=n/M, col=n%M;
  fan(tmp, row, col);
  fan(tmp, row-1, col);
  fan(tmp, row+1, col);
  fan(tmp, row, col-1);
  fan(tmp, row, col+1);
}

int achieve(char tmp[M][M]) {
  int i, j, c;
  c = tmp[0][0];
  for (i=0; i<M; i++)
    for (j=0; j<M; j++)
      if (tmp[i][j]!=c)
        return 0;
  return 1;
}

int cmk (int *p, int start, int end, int n, int begin) {
  int i, j;
  char tmp[M][M];
  if (n == 0) {
    for (i=0; i<M; i++)
      for (j=0; j<M; j++)
        tmp[i][j] = rect[i][j];
    for (i=begin-1; i>=0; i--) {
      flip (tmp, p[i]);
    }
    if (achieve (tmp))
      return 1;
  }
  for (i=start; i<=end; i++) {
    p[begin] = i;
    if (cmk (p, i+1, end, n-1, begin+1) == 1)
      return 1;
  }
  return 0;
}

int main() {
  int i, j;

  for (i=0; i<M; i++) {
    for (j=0; j<M; j++) 
      scanf ("%c", &rect[i][j]);
    getchar();
  }

  for (i=0; i<M*M; i++)
    if (cmk (data, 0, 15, i, 0) == 1)
      break;
  if (i != M*M)
    printf ("%d\n", i);
  else
    printf ("Impossible\n");
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值