Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40632   Accepted: 17647

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

bwwb
bbwb
bwwb
bwww

Sample Output

4

Source

题目地址:http://poj.org/problem?id=1753

#include<stdio.h>
#include<string.h>
#include<iostream> #include<stdlib.h> using namespace std; #define N 7 bool mat[N][N], flag;
int deep; int dx[] = {, -, , , };
int dy[] = {, , , -, }; void Init()
{
char c;
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
scanf(" %c",&c);
if(c == 'b')
{
mat[i][j] = ;
}
else
{
mat[i][j] = ;
}
}
}
}
void Print()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
printf("%d", mat[i][j]);
printf("\n");
}
} void Change(int x, int y)
{
int next_x, next_y;
for(int k = ; k < ; k++)
{
next_x = x + dx[k];
next_y = y + dy[k];
//if(next_x >= 1 && next_x <=4 && next_y >= 1 && next_y <=4)
//{
mat[next_x][next_y] = !mat[next_x][next_y];
//}
}
}
void Flip(int x)
{
switch(x) //1~16种case代表4*4的16个格子
{ case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break; case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break; case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break; case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
}
}
bool Result()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
if(mat[i][j] != mat[][])
return false;
}
return true;
}
void Dfs(int x, int dp)
{ if(flag || dp > deep || x>) return;
//printf("DFS(%d, %d)\n", x, dp); Flip(x);
//Print();printf("---------\n");
if(dp == deep)
{
flag = (flag || Result());
if(flag)
{
//printf("OK!!!!!!!!!!!!!!");
//exit(0);
goto A;
} }
Dfs(x+, dp+);
Flip(x);
//Print();printf("---------\n");
Dfs(x+, dp);
A: return;
} int main()
{
//每个棋子最多翻一次(其实是奇数次,但是没意义),翻偶数次和没翻一样,所以每个棋子就两种状态,翻或者不翻
//所以一共就有2^16次方种可能,枚举这些可能就行了,DFS
//从0到16,如果16还找不到那就是Impossible
int a, b;
Init();
//Print(); flag = false;
if(Result())
{
cout<<<<endl;
return ;
} for(deep = ; deep <= ; deep++)
{
//cout<<"deep = "<<deep<<endl;
Dfs(, );
if(flag) break;
} if(flag) cout<<deep<<endl;
else cout<<"Impossible"<<endl; // while(cin>>a)
// {
// Flip(a);
// Print();
// } return ;
}
/*
bwwb
bbwb
bwwb
bwww wwww
wwww
wwww
wwww bbbw
wbww
wwww
wwww wwww
wwww
wwwb
wwbb bbww
bwww
wwww
wwww wwbw
bbww
wwww
wwww
*/

POJ 1753 (枚举+DFS)的更多相关文章

  1. POJ 3050 枚举+dfs+set判重

    思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...

  2. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  3. [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)

    先列出题目: 1.POJ 1753 POJ 1753  Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...

  4. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  5. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

  6. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  7. poj 1753 2965

    这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...

  8. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  9. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

随机推荐

  1. 【bzoj3261】最大异或和 可持久化Trie树

    题目描述 给定一个非负整数序列 {a},初始长度为 N.       有M个操作,有以下两种操作类型:1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1.2.Q l r x:询问操 ...

  2. Linux环境CentOS6.9安装配置Elasticsearch6.2.2最全详细教程

    Linux环境CentOS6.9安装配置Elasticsearch6.2.2最全详细教程 前言 第一步:下载Elasticsearch6.2.2 第二步:创建应用程序目录 第四步:创建Elastics ...

  3. Agile工作方法

    [工具] Slack https://slack.com/ 看板 https://trello.com/ 其他TBC

  4. Filter里面实现未登录跳转,已登录权限判断

    package com.erichfund.cljjfof.server.util; import java.io.IOException; /** * @author 作者 zhuzhengquan ...

  5. 转载 关于malloc

    1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返回一个空指针(NULL). 关于分配失败 ...

  6. C、C++变量auto,static,register,extern类型

    auto: 推导类型变量:编译器选项指示编译器如何使用 auto 关键字来声明变量. 如果指定默认选项 /Zc:auto,编译器从其初始化表达式中推导声明的变量的类型. 如果指定 /Zc:auto-, ...

  7. HDU 3068 Manacher

    题目链接:http://hdu.hustoj.com/showproblem.php?pid=3068 今天学习一下马拉车算法,虽然mg讲过,但是没有系统去学. 算法学习:参考博客 马拉车模板题. # ...

  8. js中cookie、sessionStorage、localStorage

    一.cookie <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  9. ES6之Array.includes()函数

    一.定义 includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false. 二.语法 arr.includes(searchElement) arr.includ ...

  10. awk 统计

    命令太多,记不住,组合起来用一把…..示例文件: 1 2 3 4 5 6 7 8 9 10 11 [root@lovedan test]# cat a.txt hello good world hel ...