博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1753——Flip Game(枚举+dfs)
阅读量:2344 次
发布时间:2019-05-10

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

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:

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

bwwb

bbwb
bwwb
bwww
Sample Output

4

要解这道题,首先得弄明白一个重要的定理:一个格子上的棋子最多翻转一次就够了。因此最多翻转的次数就是把所有棋子都翻一遍,也就是16遍

一开始我还想不通为什么分类是枚举,感觉要用搜索,但搜索又太麻烦,好像还没有上界,看了解题报告才知道要枚举遍数。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 100010#define Mod 10001using namespace std;char a[10][10];bool flag;int step;bool check(){ char c=a[1][1]; for(int i=1;i<=4;++i) for(int j=1;j<=4;++j) if(a[i][j]!=c) return false; return true;}void change(int x,int y){ if(a[x][y]=='b') a[x][y]='w'; else a[x][y]='b';}void turn(int x,int y){ change(x,y); if(x-1>=1) change(x-1,y); if(y-1>=1) change(x,y-1); if(x+1<=4) change(x+1,y); if(y+1<=4) change(x,y+1);}void dfs(int x,int y,int deep){ if(deep==step) { flag=check(); return; } if(x<1||y<1||x>4||y>4||flag) return; turn(x,y); if(y<4) { dfs(x,y+1,deep+1); turn(x,y); dfs(x,y+1,deep); } else { dfs(x+1,1,deep+1); turn(x,y); dfs(x+1,1,deep); } return;}int main(){ for(int i=1;i<=4;++i) for(int j=1;j<=4;++j) cin>>a[i][j]; for(step=0;step<=16;++step) { dfs(1,1,0); if(flag) break; } if(flag) printf("%d\n",step); else printf("Impossible\n"); return 0;}

转载地址:http://mtcvb.baihongyu.com/

你可能感兴趣的文章
我要学ASP.NET MVC 3.0(十): MVC 3.0 使用 Forms身份验证
查看>>
我要学ASP.NET MVC 3.0(十一): MVC 3.0 使用筛选器
查看>>
ASP.NET MVC3、Pager 分页
查看>>
在 ASP.NET MVC 中创建自定义 HtmlHelper 控件
查看>>
MSDN---扩展方法 (C# 方法中的this参数)
查看>>
我要学ASP.NET MVC 3.0(十四): MVC 3.0 实例系列之创建数据表格
查看>>
我要学ASP.NET MVC 3.0(十五): MVC 3.0 实例系列之表格的排序
查看>>
我要学ASP.NET MVC 3.0(十七): MVC 3.0 实例之表格中数据的筛选
查看>>
Displaying a Sorted, Paged, and Filtered Grid of Data in ASP.NET MVC
查看>>
C#中的操作符
查看>>
ADO.NET Ling to Sql 语法
查看>>
ASP.NET MVC 2博客系列之一:强类型HTML辅助方法
查看>>
详解Asp.net MVC DropDownLists
查看>>
Asp.net MVC防止图片盗链的实现方法,通过自定义RouteHandler来操作
查看>>
VS2010的智能提示没有了的可能原因
查看>>
Creating a Cascading Dropdown in ASP.net MVC 3 and jQuery (1)
查看>>
创建联动的 DropdownList in ASP.net MVC 3 and jQuery (2)
查看>>
HTTP触发Jenkins参数化构建(CORS Plugin)
查看>>
来自 Serenity 的 Java 8 的一些使用技巧
查看>>
ubuntu12.04--中文输入法ibus安装
查看>>