poj算法-使用C语言在VC++6.0实现poj1753(递归枚举)
来源:互联网 发布:房地产公司 水暖 知乎 编辑:程序博客网 时间:2024/06/07 20:48
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中正确运行。
- poj算法-使用C语言在VC++6.0实现poj1753(递归枚举)
- poj算法-使用C语言在VC++6.0实现poj2965(枚举)
- poj算法-使用C语言在VC++6.0实现poj1003
- poj算法-使用C语言在VC++6.0实现poj1004
- poj算法-使用C语言在VC++6.0实现poj1005
- poj算法-使用C语言在VC++6.0实现poj1328(贪心)
- C语言实现递归算法
- poj1753 Flip Game 枚举 暴力 递归
- 算法思考--------汉诺塔c语言实现-------递归
- 组合算法实现C语言,非递归
- 组合算法的实现,递归,C语言
- POJ初级分类 枚举专题 poj1753 poj2965
- C(C语言算法第一篇递归算法的使用)
- 如何使用C语言实现递归调用
- C/C++ 使用递归算法实现汉诺塔
- C/C++ 使用递归算法实现汉诺塔
- poj1753枚举
- poj1753 枚举
- 勾股定理及其逆定理的一种简单证明方法
- 线程、进程和通信
- leetcode之二叉树类之最小公共祖先-----236/235. Lowest Common Ancestor of a Binary/Binary Search Tree
- mybatis学习三之mapper.xml开发方法
- Bode Plot
- poj算法-使用C语言在VC++6.0实现poj1753(递归枚举)
- js获取url传递参数
- 第七周 项目1
- MOBILedit Forensic Express(手机取证工具)官方64位版V4.2.0.10865下载 | MOBILedit Forensic Express破解版
- c++中cin,cin.get()和cin.getline()的比较
- [ACM]CCF CSP[201703-5]E题 引水入城【60分程序】
- Qrcode 在EASYUI 表格中为每一行生成二维码插入表格中
- 2017开发语言排行(本人是搞C#+javascript的)
- 如何查找Tensorflow operation的实现源码