OpenJudge/Poj 1753 Flip Game
1.链接地址:
http://bailian.openjudge.cn/practice/1753/
http://poj.org/problem?id=1753
2.题目:
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 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.- 输入
- 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- 来源
- Northeastern Europe 2000
3.思路:
4.代码:
#include "assert.h"
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
using namespace std; const int MAX_STATE = ;
const int ALL_WHILE_STATE = ;
const int ALL_BLACK_STATE = ;
const int WIDTH_OF_BOARD = ;
const int SIZE_OF_BOARD = WIDTH_OF_BOARD * WIDTH_OF_BOARD; // 4 * 4 int ConvertPieceColorToInt(char color)
{
switch(color)
{
case 'b':
return ;
case 'w':
return ;
}
} int FlipPiece(int state_id, int position)
{
state_id ^= ( << position); // up
if(position - >= )
state_id ^= ( << (position - ));
// down
if(position + < SIZE_OF_BOARD)
state_id ^= ( << (position + ));
// left
if(position % != )
state_id ^= ( << (position - ));
// right
if(position % != )
state_id ^= ( << (position + )); return state_id;
} int main()
{
int current_state_id = ;
int state[MAX_STATE];
queue<int> search_queue; memset(state, -, sizeof(state)); char color; for(int i = ; i < SIZE_OF_BOARD; ++i)
{
cin >> color;
current_state_id += ConvertPieceColorToInt(color) << i;
} if(current_state_id == ALL_WHILE_STATE
|| current_state_id == ALL_BLACK_STATE)
{
cout << "" << endl;
return ;
} state[current_state_id] = ;
search_queue.push(current_state_id); int next_state_id; while(!search_queue.empty())
{
current_state_id = search_queue.front();
search_queue.pop(); for(int i = ; i < SIZE_OF_BOARD; ++i)
{
next_state_id = FlipPiece(current_state_id, i);
if(next_state_id == ALL_WHILE_STATE
|| next_state_id == ALL_BLACK_STATE)
{
cout << state[current_state_id] + << endl;
return ;
}
assert(next_state_id < MAX_STATE);
if(state[next_state_id] == - /* not visited */)
{
state[next_state_id] = state[current_state_id] + ;
search_queue.push(next_state_id);
}
}
} cout << "Impossible" << endl;
return ;
}
OpenJudge/Poj 1753 Flip Game的更多相关文章
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- POJ 1753 Flip Game(二进制枚举)
题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...
- POJ 1753 Flip Game(bfs+位压缩运算)
http://poj.org/problem?id=1753 题意:一个4*4的棋盘,只有黑和白两种棋子,每次翻转一个棋子,并且其四周的棋子也跟着翻转,求棋盘全为黑或全为白时所需的最少翻转次数. 思路 ...
随机推荐
- iOS FONT字体名
下面这段代码可以查看ios中可用的字体,具体那些字体长什么样,可以查看字体册工具. NSArray *familyArray = [UIFont familyNames]; for (id famil ...
- Windows Phone-框架结构和启动过程
上一篇文章介绍了Windows Phone的开发环境和一个简单的Windows Phone程序的演示和结构,这一篇文章要深入一点,介绍Windows Phone的框架结构和程序启动的过程. 一 Win ...
- android116 轮播 viewPager实现
布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- C#_datatable 写入大量数据_BulkCopy
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Configuration; u ...
- Mysql在windows下的免安装配置步骤和重新安装的步骤
windows下mysql免安装配置 1. 下载mysql免安装压缩包 下载mysql-5.6.22-winx64.zip 解压到本地D:\mysql-5.6.22-winx64 2. 修改配置文件 ...
- 【区间选点问题】uva 10148 - Advertisement
区间选点问题,即数轴上有n个闭区间[l1i, ri],取尽量少的点,使得每个区间内都至少有一个点. The Department of Recreation has decided that it m ...
- Ubuntu 安装php mcrypt
sudo apt-get install php5-mcrypt libmcrypt4 libmcrypt-dev sudo php5enmod mcrypt sudo /etc/init.d/apa ...
- SSRS集成至Web
分几步进行,第一步要实现:IReportServerCredentials 接口第二步:拖入ReportViewer控件第三步:进行报表传参控制.具体如下图描述....代码部分参考下附件 using ...
- 为 ASP.NET Web API 创建帮助页
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages 以前实例 ...
- ThinkPHP的配置
ThinkPHP配置:conf目录下 'URL_PATHINFO_DEPR'=>'-',//修改URL的分隔符, 'TMPL_L_DELIM'=>'<{', //修改左定界符 'TM ...