POJ 1753 (枚举+DFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40632 | Accepted: 17647 |
Description
- 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
Output
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)的更多相关文章
- POJ 3050 枚举+dfs+set判重
思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)
先列出题目: 1.POJ 1753 POJ 1753 Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- 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 ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- poj 1753 2965
这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...
- 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:// ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
随机推荐
- 九度oj 题目1184:二叉树遍历
题目描述: 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的先序遍历字符串:ABC##DE#G##F###其中“#”表示的是空格,空格字符代表空 ...
- Unity3D - UGUI组件的中英文对照
- lambda遍历的精简
本文转自 http://it.deepinmind.com/java%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/2014/03/15/Java%E5%8 ...
- 设计模式(二 & 三)工厂模式:1-简单工厂模式
模拟场景: 需要构造一个运算器(Operation),分别负责加减乘除的运算功能. 思想: 这里需要构造四个 Operation,可以使用 Factory 去统一创建这四个对象. 所需要构造的对象是运 ...
- 许式伟看 Facebook 发币(上): 区块链, 比特币与 Libra 币
你好,我是七牛云许式伟. Facebook(脸书)于6月18日发布了其加密数字货币项目白皮书.该数字货币被命名为 Libra(天秤座),象征着平衡与公正.此前,BBC 报道说这个数字货币叫 Globa ...
- 【bzoj3166】[Heoi2013]Alo 可持久化Trie树+STL-set
题目描述 Welcome to ALO ( Arithmetic and Logistic Online).这是一个VR MMORPG ,如名字所见,到处充满了数学的谜题.现在你拥有n颗宝石,每颗宝石 ...
- java面试题之死锁产生的条件,以及如何避免死锁,银行家算法,产生死锁后如何解决(阿里面试题)
死锁产生的四个必要条件: 互斥:一个资源每次只能被一个进程使用(资源独立) 请求与保持:一个进程因请求资源而阻塞时,对已获得的资源保持不放(不释放锁) 不剥夺:进程已获得的资源,在未使用之前,不能强行 ...
- python的dbutil
目录机构如下: dbutil代码如下: #!/usr/bin/python # -*- coding:utf-8 -*- import configparser import pymysql clas ...
- 如何选择IO流
1)确定是数据源和数据目的(输入还是输出) 源:输入流 InputStream Reader 目的:输出流 OutputStream Writer 2)明确操作的数据对象是否是纯文本 是:字符流Rea ...
- jmeter登录禅道案例
下载jmeter,配置环境变量 变量名:JMETER_HOME 变量值:C:\Program Files\apache-jmeter-2.11 变量名:CLASSPATH 变量值:%JMETER_HO ...