博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flip Game
阅读量:5236 次
发布时间:2019-06-14

本文共 2837 字,大约阅读时间需要 9 分钟。

描述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).题意:给你一张4*4的方格,每个格子里都有一枚两面分别为黑白两色的棋子,每个棋子一开始的状态不一样,每次 操作一个棋子,可以使该棋子与其周边棋子翻面,问你需要最少多少次操作才可以使所有棋子颜色一样(可以是黑色,也可以是白色)。

这道题我们采用二进制枚举,枚举所有可能的操作情况,然后比较输出最小的方案即可。

话不多说,看代码

#include <iostream>

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
char ch[10][10];
int a[10][10],t[10][10];
int b[17];
int ans=16;
bool found=false;
void fan(int r,int c)
{
t[r][c]=!t[r][c];
t[r-1][c]=!t[r-1][c];
t[r][c+1]=!t[r][c+1];
t[r+1][c]=!t[r+1][c];
t[r][c-1]=!t[r][c-1];
}
int main()
{
std::ios::sync_with_stdio(false);
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
cin>>ch[i][j];
if(ch[i][j]=='w')a[i][j]=0;
if(ch[i][j]=='b')a[i][j]=1;
}
int sum=0;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
sum+=a[i][j];
if(sum==0||sum==16)//如果一开始就不用操作,直接输出
{
cout<<0<<endl;
return 0;
}
while(b[0]!=1)
{
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
t[i][j]=a[i][j];
int cnt=0;
for(int i=1;i<=16;i++)
{
if(b[i]==1)
{
int r=(ceil)(i/4.0);//计算行 
int c=(i%4==0?i%4+4:i%4);计算列
fan(r,c);//操作
cnt++;//计数
}
}
sum=0;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
sum+=t[i][j];
if(sum==0||sum==16) //如果成功,就进行比较,取较小的;found放成true.
{
found=true;
if(cnt<ans)ans=cnt;
}
int k=16;
while(b[k]!=0)k--;
b[k]=1;
for(int i=k+1;i<=16;i++)b[i]=0;//二进制枚举
}
if(!found)
cout<<"Impossible"<<endl;//没有这种情况
else
cout<<ans<<endl;
return 0;
}

转载于:https://www.cnblogs.com/new-hand/p/7230979.html

你可能感兴趣的文章
【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
查看>>
.net 文本框只允许输入XX,(正则表达式)
查看>>
Python 第四十五章 MySQL 内容回顾
查看>>
实验2-2
查看>>
MongoDB遇到的疑似数据丢失的问题。不要用InsertMany!
查看>>
android smack MultiUserChat.getHostedRooms( NullPointerException)
查看>>
IOS Google语音识别更新啦!!!
查看>>
[置顶] Linux终端中使用上一命令减少键盘输入
查看>>
BootScrap
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
大数据学习之一——了解简单概念
查看>>
[Linux]PHP-FPM与NGINX的两种通讯方式
查看>>
Java实现二分查找
查看>>
架构图-模型
查看>>
黑马程序员_Java基础枚举类型
查看>>
UIImage 和 iOS 图片压缩UIImage / UIImageVIew
查看>>
疯狂JAVA16课之对象与内存控制
查看>>
django ORM创建数据库方法
查看>>