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 题目1085:求root(N, k) 清华2010年机试题目
题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k).N'为N的k进制表示的各位数字之和.输入x,y,k,输出root(x^y,k)的值 (这里^ ...
- POJ 1656 Counting Black
Counting Black Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9772 Accepted: 6307 De ...
- 【转】深入理解JVM—JVM内存模型
http://www.cnblogs.com/dingyingsi/p/3760447.html#3497199 我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU ...
- 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 ...
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28399 Accepted: 9684 De ...
- BZOJ-3040 最短路
最短路+堆优化. 普通的堆还不行,自己用的是配对堆(貌似斐波那契堆也行?毕竟理论复杂度) 然后发现自己的配对堆比云神的不知快了多少...我照着他的模版打的喂.. 然后发现前T条边不理都能A... 数据 ...
- co模块总结
1.thunk函数 javascript中的thunk函数就是一个单参数函数,且该参数必须是一个callback函数,callback的签名必须为callback(err,args...); 所谓的t ...
- linux--bash: redis-server: 未找到命令
linux 安装redis过程中出现了异常,make不通过,异常如下: [root@localhost redis-2.8.3]# make cd src && make all ma ...
- [深入学习C#]C#实现多线程的方式:Task——任务
简介 .NET 4包含新名称空间System.Threading.Tasks,它 包含的类抽象出了线程功能. 在后台使用ThreadPool. 任务表示应完成的某个单元的工作. 这个单元的工作可以在单 ...
- Tomcat是怎么工作的(2) -- 动手实现山寨版的简单Web Server
本文先讲解一下Java web server都是怎么工作的.web server也叫HTTP server——顾名思义它是用HTTP协议和客户端交互的.客户端一般就是各种各样的浏览器了.相信所有朋友都 ...