POJ 1753-Flip Game(列舉&&DFS)
Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 38472 | Accepted: 16721 |
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:
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.
- 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
Source
Northeastern Europe 2000
題目意思:
有一個4*4的棋盤上有黑b白w兩種顏色的棋子,翻轉一個即改變它的顏色的時候而且其上下左右顏色都會變化。
求最少需要翻動多少個棋子就可以使棋盤上所有的棋子顏色相同。
解題思路:
用1表示白色w,0表示黑色b來簡化;
有4*4=16個位置可以翻轉,遍歷搜尋一遍,求出最小的翻動次數輸出即可。
題目意思:
有一個4*4的棋盤上有黑b白w兩種顏色的棋子,翻轉一個即改變它的顏色的時候而且其上下左右顏色都會變化。
求最少需要翻動多少個棋子就可以使棋盤上所有的棋子顏色相同。
解題思路:
用1表示白色w,0表示黑色b來簡化;
有4*4=16個位置可以翻轉,遍歷搜尋一遍,求出最小的翻動次數輸出即可。
/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:filp game.cpp
* 作 者:單昕昕
* 完成日期:2016年4月26日
* 版 本 號:v1.0
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int p[4][4];//儲存棋盤
int Min=0x7fffffff;
int check()//檢查棋子是否同色
{
int i,j;
for(i=0; i<4; ++i)
for(j=0; j<4; ++j)
if(p[i][j]!=p[0][0])
return -1;
return 1;
}
void change(int i,int j)//改變自己和上下左右的顏色
{
p[i][j]=!p[i][j];
if(i-1>=0)
p[i-1][j]=!p[i-1][j];
if(i+1<4)
p[i+1][j]=!p[i+1][j];
if(j-1>=0)
p[i][j-1]=!p[i][j-1];
if(j+1<4)
p[i][j+1]=!p[i][j+1];
}
void dfs(int d,int step)//列舉深搜
{
int x,y;
if(d==16)
{
if(check()==1)
if(step<Min)
Min=step;
return;
}
else
{
x=d/4;//行
y=d%4;//列
dfs(d+1,step);
change(x,y);//不行就翻回來
dfs(d+1,step+1);
change(x,y);
}
}
int main()
{
int i,j;
char t;
memset(p,0,sizeof(p));
for(i=0; i<4; ++i)
for(j=0; j<4; ++j)
{
cin>>t;
if(t=='w')
p[i][j]=1;//1表示白色w,0表示黑色b
}
dfs(0,0);
if(Min==0x7fffffff)//0x7fffffff是int的最大值
cout<<"Impossible"<<endl;
else
cout<<Min<<endl;
return 0;
}
相關文章
- POJ 1753 Flip Game(列舉變元的高斯消元)GAM
- Flip Game(POJ 1753)GAM
- POJ 3321 Apple Tree(dfs+樹狀陣列)APP陣列
- POJ3321 Apple Tree(DFS序 + 樹狀陣列)APP陣列
- POJ 3080 Blue Jeans (KMP+暴力列舉)【模板】KMP
- POJ3279 Fliptile【狀態壓縮+DFS】
- POJ 1691 Painting A Board(dfs搜尋)AI
- UVALive 7410 && POJ 5583 Kingdom of Black and White (列舉)
- poj3080-kmp+列舉子串 求最長公共子串KMP
- Java 列舉、JPA 和 PostgreSQL 列舉JavaSQL
- POJ 1699 二進位制表示狀態+dfs
- 列舉和列舉的取值範圍
- POJ 2718簡單列舉貪心演算法(好久沒寫程式碼了。。)演算法
- Java列舉Java
- Swift,列舉Swift
- poj 2481 樹狀陣列陣列
- POJ 1305-Fermat vs. Pythagoras(本原的畢達哥拉斯三元組+列舉)Go
- C# 列舉與位列舉概述C#
- 列舉工具類
- TypeScript 列舉enumTypeScript
- Java 列舉(enum)Java
- Swift-列舉Swift
- 自定義列舉
- java列舉類Java
- TypeScript 列舉指南TypeScript
- Java列舉使用Java
- C#:列舉C#
- 列舉程式 (轉)
- 列舉比較
- 列舉型別型別
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- Java列舉-通過值查詢對應的列舉Java
- Java enum列舉類詳解 列舉的常見用法Java
- POJ 2823 單調佇列佇列
- C/C++列舉enum分別列印輸出列舉子和列舉值的方法C++
- 方格分割 二進位制列舉+DFS(2017 第八屆藍橋杯省賽A組 第4題)
- ENUM列舉型別型別
- Java基礎--列舉Java