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

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

 

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40930   Accepted: 17766

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

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

  

  拿到这道题第一想法就是深搜。

  简单介绍一下题意:

  一个4*4棋盘, 输入为初始状态。flip一颗棋子, 本身和周围的四颗棋子会跟着翻动, 问最短需要翻动几颗棋子可以使棋盘全部黑或者白?

  

  首先看一下从题中得到的信息:

  1. 这个棋盘很小, 4*4

  2. 翻动一个棋子两次相当于没翻, 于是步数最大只能为16

  于是想到可以将深度作为函数的参数, 将从0依次递增的步数作为终止条件, 即当深度等于步数长度的时候就判断当前棋盘状态是否OK,如果不OK就继续回溯, 直至走完当前规定步数的所有情况。

   

#include 
#include
#include
using namespace std;int map[5][5];int step;int flag;const int dx[5] = { 0, 0, 0, 1, -1 };const int dy[5] = { 0, 1, -1, 0, 0 };bool is_ok() { int sum = 0; for( int i = 0; i < 4; i++ ) { for( int j = 0; j < 4; j++ ) { sum += map[i][j]; } } if( sum == 0 || sum == 16 ) return true; else return false;}bool is_valid( int x, int y ) { if( 0 <= x && x <= 3 && 0 <= y && y <= 3 ) return true; else return false;}void flip( int x, int y ) { for( int i = 0; i <= 4; i++ ) { int x2 = x + dx[i]; int y2 = y + dy[i]; if( is_valid( x2, y2 ) ) { map[x2][y2] = !map[x2][y2]; } }}void dfs( int x, int y, int deep ) { if( deep == step ) { flag = is_ok(); return; } if( flag || x == 4 ) return; flip( x, y ); if( y < 3 ) dfs( x, y+1, deep+1 ); else dfs( x+1, 0, deep+1 ); flip( x, y ); if( y < 3 ) dfs( x, y+1, deep ); else dfs( x+1, 0, deep ); return;}int main() { char ch; for( int i = 0; i < 4; i++ ) { for( int j = 0; j < 4; j++ ) { cin >> ch; if( ch == 'b' ) map[i][j] = 0; else map[i][j] = 1; } } for( step = 0; step <= 16; step++ ) { flag = false; dfs( 0, 0, 0 ); if( flag ) break; } if( flag ) { cout << step << endl; } else { cout << "Impossible" << endl; } return 0;}
View Code

 

 

 这本来是道很简单的dfs题, 我却做了挺久, 究其原因, 还是太水。 引用邝神一句话:“人一我百, 人十我万!”

转载于:https://www.cnblogs.com/FriskyPuppy/p/5929149.html

你可能感兴趣的文章
python 高级函数
查看>>
F.Cards with Numbers
查看>>
简单入门Buffer
查看>>
OO第四阶段总结
查看>>
javascript总结02
查看>>
创建windows服务
查看>>
HTML5 入门基础
查看>>
C++文件操作(fstream)
查看>>
用main函数传参做简单的计算器的代码
查看>>
python中struct.unpack的用法
查看>>
体绘制(Volume Rendering)概述之4:光线投射算法(Ray Casting)实现流程和代码(基于CPU的实现)...
查看>>
Python实践之(七)逻辑回归(Logistic Regression)
查看>>
PAT (Advanced Level) 1107. Social Clusters (30)
查看>>
【开源社群系统研发日记五】ThinkSNS+ 是如何计算字符显示长度的
查看>>
Nodejs日志管理log4js
查看>>
python获取昨日日期
查看>>
海康威视 - 萤石云开放平台 js 版
查看>>
关于分销平台
查看>>
剑指offer---12-**--数值的整数次方
查看>>
PAT - L2-010. 排座位(并查集)
查看>>