poj算法-使用C语言在VC++6.0实现poj1753(递归枚举)

本文介绍了一种解决FlipGame问题的方法,此游戏的目标是在最少的步骤内将4x4矩阵的所有元素翻转成同一颜色。文章详细解释了游戏规则及算法实现思路。

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

poj1753:

Flip Game

题目描述:
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

题目的意思是:
有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在的话)的格子的颜色也被反转,问至少反转几个格子可以使4*4的正方形变为纯白或者纯黑?

#include <stdio.h>
#include <stdlib.h>

//函数:判断格子是否全部为白色或者黑色
int allWhiteOrBlack(int* pieces,int len){
    int i=0;
    for(i=0;i<len-1;i++)
        if(pieces[i]!=pieces[i+1])
            return 0;
        return 1;
}

//函数:改变一个格子的颜色,并相应改变周围格子的颜色
void changeColor(int* arr,int i){
    int x,y;
    x=i/4;
    y=i%4;
    arr[i]=!(arr[i]);
    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]);
}

//函数:递归判断需要改变格子的数量
void combine(int* arr,int len,int* result,int count,const int NUM,int* last){
    int i;
    int x,y;
    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--){
                changeColor(new_arr,result[j]); 
            }
            if(allWhiteOrBlack(new_arr,16)){
                *last=NUM;
                free(new_arr);
                break;
            }
            free(new_arr);
        }
    }
}

int main(){
    char str[4];//存放输入的四行数据
    int pieces[16];//存放16个黑白格子
    int count=15;//格子的数量
    int lines=4;
    int i,j;
    int last=0;
    int x,y;
    while(lines--){
        scanf("%s",str);
        for(i=0;i<4;i++){
            if(str[i]=='b')
                pieces[count--]=1;
            else
                pieces[count--]=0;
        }
    }
    if(allWhiteOrBlack(pieces,16))
        printf("%d\n",0);
    else{
        //生成pieces数组的副本
        int* new_pieces=(int*)malloc(sizeof(int)*16);
        for(i=0;i<16;i++)
            new_pieces[i]=pieces[i];
        //这里的last是用来接收combine函数里面的NUM,也就是需要的步数
        for(j=1;j<=16;j++){
            int * result=(int*)malloc(sizeof(int)*j);
            combine(new_pieces,16,result,j,j,&last);
            if(last==j){
                printf("%d\n",last);
                break;
            }
            //new_pieces已经被改变,所以要还原为pieces
            for(i=0;i<16;i++)
                new_pieces[i]=pieces[i];
            free(result);
        }
        free(new_pieces);
        if(j==17)
            printf("Impossible\n");
    }
    return 0;
}

代码可以在VC++6.0中正确运行,也可以在poj中正确运行。
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值