标签:hit 存储 ini ios c99 ever you 结束 follow
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
Output
#include <stdio.h> #include <stdlib.h> #include <cstring> #include <iostream> using namespace std; class Chess { private: int result[17]; //存储点 char map[4][4]; //方格 4 * 4 char temp[4][4]; //存储转换后的方格状态 public: void init() //初始化输入并检查是否已经纯色 { for (int i = 0; i < 4; i++) scanf("%s", map[i]); //输入方格的初始状态 if(check(map)) //检查 { printf("0\n"); exit(0); } } void reverse(char temp[][4], int x, int y)//单次反转动作 { if (temp[x][y] == ‘b‘) temp[x][y] = ‘w‘; //白色变黑色 else temp[x][y] = ‘b‘; //黑色变白色 } void apply(int n) //应用组合数进行反转 { for (int i = n - 1; i >= 0; i--) { int x = result[i] / 4; //row int y = result[i] % 4; //column reverse(temp, x, y); if(x > 0) reverse(temp, x - 1, y); if(x < 3) reverse(temp, x + 1, y); if(y > 0) reverse(temp, x, y - 1); if(y < 3) reverse(temp, x, y + 1); } } int check(char temp[][4]) //检查是否反转成功 { int flag = 0; for(int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (temp[i][j] == ‘b‘) flag++; //全为白色的状态 else flag--; //全为黑色的状态 } } if(flag == 16 || flag == -16) return 1; //翻转成功 else return 0; //翻转失败 } void meiju(int start, int left, int num) //枚举从16个数中取num个的所有组合 { for(int i = start; i <= 16 - left; i++) { result[left - 1] = i; if(left == 1) { for(int j = 0; j < 4; j++) for(int k = 0; k < 4; k++) temp[j][k] = map[j][k];
apply(num); if(check(temp)) //判断翻转是否成功 { printf("%d\n", num); exit(0); //一旦找到必定是最优解,可以直接结束程序 } } else meiju(i + 1, left - 1, num); } } }; int main() { Chess a; //创建对象 a.init(); //初始化 for (int i = 1; i <= 16; i++) a.meiju(0, i, i); printf("Impossible\n"); return 0; }
标签:hit 存储 ini ios c99 ever you 结束 follow
原文地址:https://www.cnblogs.com/Edviv/p/11614739.html