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. 【Luogu】P3205合唱队(区间DP)

    题目链接 通过这题我发现我已经不会DP了 区间DP,f[i][j]是从左面转移来的,d[i][j]是从右面转移来的 然后DP方程是 ]) f[i][j]+=f[i+][j]; ][j]; f[i][j ...

  2. [UOJ#276]【清华集训2016】汽水

    [UOJ#276][清华集训2016]汽水 试题描述 牛牛来到了一个盛产汽水的国度旅行. 这个国度的地图上有 \(n\) 个城市,这些城市之间用 \(n−1\) 条道路连接,任意两个城市之间,都存在一 ...

  3. volatile的用法

    在再有人问你Java内存模型是什么,就把这篇文章发给他中我们曾经介绍过,Java语言为了解决并发编程中存在的原子性.可见性和有序性问题,提供了一系列和并发处理相关的关键字,比如synchronized ...

  4. 内存分配(new/delete,malloc/free,allocator,内存池)

    以下来源http://www.cnblogs.com/JCSU/articles/1051826.html 程序员们经常编写内存管理程序,往往提心吊胆.如果不想触雷,唯一的解决办法就是发现所有潜伏的地 ...

  5. Failed to apply plugin [id 'com.gradle.build-scan']

    把spring源码clone下来之后,使用gradle编译不通过,异常日志如下: FAILURE: Build failed with an exception. * Where: Build fil ...

  6. APUE 学习笔记(十一) 网络IPC:套接字

    1. 网络IPC 套接字接口既可以用于计算机之间进程通信,也可以用于计算机内部进程通信   套接字描述符在Unix系统中是用文件描述符实现的   /* 创建一个套接字 */ #include < ...

  7. 2018.8.8 Noip2018模拟测试赛(二十一)

    日期: 八月七号  总分: 300分  难度: 提高 ~ 省选    得分: 112分(OvO) 题目目录: T1:幸福的道路 T2:Solitaire T3:Flags 赛后心得: 第一题裸树d啊! ...

  8. select函数与stdio混用的不良后果 (转)

    出自:http://www.cppblog.com/mysileng/archive/2013/01/15/197284.html 今天在看UNP6.5节,学习到了select与stdio混用的后果. ...

  9. 我的logging 配置

    #encoding=utf-8 import logging.config logging.config.dictConfig({ 'version': 1, 'disable_existing_lo ...

  10. AC日记——Propagating tree Codeforces 383c

    C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...