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

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中正确运行。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/sinat_29485667/article/details/78236387
今日推荐