POJ 1753 枚举
来源:互联网 发布:绵阳编程培训学校 编辑:程序博客网 时间:2024/06/05 01:56
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal 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:
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.
- Choose any one of the 16 pieces.
- 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
bwwbbbwbbwwbbwww
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
- POJ 1753 枚举
- poj 1753 枚举+深搜
- poj 1753 flip[ 枚举 ]
- POJ 1753 枚举
- poj 1753 枚举
- POJ 1753 递归枚举
- POJ 1753(枚举)
- POJ-1753(递归枚举)
- POJ 1753 dfs+枚举
- poj 1753 枚举+dfs
- POJ 1753 Flip Game 枚举
- POJ 1753 枚举二进制+BFS
- POJ 1753 Flip Game (枚举)
- poj 1753 枚举加dfs
- POJ 1753 Flip Game 枚举
- poj 1753Flip Game---枚举
- POJ 1753 Flip Game【枚举】
- POJ 1753 Flip Game (枚举)
- 设置SOCKET的发送与接收缓冲区
- wamp配置多目录访问方法
- Effective C++读书笔记
- 《Python核心编程》第10章 习题
- 关于socket buffer size的调优
- POJ 1753 枚举
- 中文知识图谱构建方法研究3
- iOS中得block代码块的定义及使用
- 1137. 河床
- XML/JSON 后台数据
- mac android studio please check subversion ssl setting
- HDU 过山车(二分匹配)
- 结构体的题
- HDU-1754-线段树-I Hate It