POJ 1753 DFS
思路:
有过两个裸搜的思路,第一个一个无限TLE,第二个还不慢。。。
错误思路:迭代加深搜索,枚举翻第几个棋,挂的原因:16的16次方,不挂就怪了。
错误代码见下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char q[6][6],vis[6][6],flag=false;
int tot=0,ans=0x3f3f3f3f,xx[]={1,-1,0,0,0},yy[]={0,0,1,-1,0},ansb,answ,step;
void dfs(int a,int b,int an_b,int an_w,int t)
{
if(an_b==16||an_w==16){flag=1;return;}
if(flag||t==step) {return;}
for(int dx=1;dx<=4;dx++){
for(int dy=1;dy<=4;dy++){//错误的枚举
if(vis[dx][dy])continue;
for(int i=0;i<=4;i++)
{
if(q[dx+xx[i]][dy+yy[i]]=='b')an_b--,an_w++,q[dx+xx[i]][dy+yy[i]]='w';
else if(q[dx+xx[i]][dy+yy[i]]=='w')an_w--,an_b++,q[dx+xx[i]][dy+yy[i]]='b';
}
vis[dx][dy]=1;
// printf("dx=%d dy=%d an_b=%d an_w=%d\n",dx,dy,an_b,an_w);
dfs(dx,dy,an_b,an_w,t+1);
vis[dx][dy]=0;
for(int i=0;i<=4;i++){
if(q[dx+xx[i]][dy+yy[i]]=='w')an_w--,an_b++,q[dx+xx[i]][dy+yy[i]]='b';
else if(q[dx+xx[i]][dy+yy[i]]=='b')an_b--,an_w++,q[dx+xx[i]][dy+yy[i]]='w';
}
}
}
}
int main()
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
cin>>q[i][j];
if(q[i][j]=='b') ansb++;
else if(q[i][j]=='w') answ++;
}
}
for(step=0;step<=15;step++)
{
memset(vis,0,sizeof(vis));
dfs(1,1,ansb,answ,0);
if(flag) {
printf("%d",step);
break;
}
}
if(!flag)printf("Impossible");
}
正确思路(但不是最好的思路):
枚举翻或者不翻。2^16,可以接受
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char q[6][6];
int map[17][3];
int tot=0,xx[]={1,-1,0,0,0},yy[]={0,0,1,-1,0},ansb,answ,flag=0x3f3f3f;
void dfs(int t,int an_b,int an_w,int flip)
{
if(an_b==0||an_w==0)flag=min(flag,flip);
if(t>tot) return;
int dx=map[t][1],dy=map[t][2];
dfs(t+1,an_b,an_w,flip);
for(int i=0;i<=4;i++)
if(q[dx+xx[i]][dy+yy[i]]=='b')an_b--,an_w++,q[dx+xx[i]][dy+yy[i]]='w';
else if(q[dx+xx[i]][dy+yy[i]]=='w')an_w--,an_b++,q[dx+xx[i]][dy+yy[i]]='b';
dfs(t+1,an_b,an_w,flip+1);
for(int i=0;i<=4;i++)
if(q[dx+xx[i]][dy+yy[i]]=='b')an_b--,an_w++,q[dx+xx[i]][dy+yy[i]]='w';
else if(q[dx+xx[i]][dy+yy[i]]=='w')an_w--,an_b++,q[dx+xx[i]][dy+yy[i]]='b';
}
int main()
{
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++){
cin>>q[i][j];
if(q[i][j]=='b') ansb++;
else if(q[i][j]=='w') answ++;
}
for(int j=1;j<=4;j++){
for(int i=1;i<=4;i++){
tot++;
map[tot][1]=j;
map[tot][2]=i;
}
}
dfs(1,ansb,answ,0);
if(flag==0x3f3f3f)printf("Impossible");
else printf("%d\n",flag);
}
POJ 1753 DFS的更多相关文章
- poj—1753 (DFS+枚举)
...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- [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 1753 2965
这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- 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 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- 穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753
下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A mes ...
随机推荐
- Git学习总结四(删除)
一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了: $ rm test.txt 这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻 ...
- altera quartus 百度云分享 quartus prime 17.1 16.1 13.0
quartus prime 17.1 标准版 链接:https://pan.baidu.com/s/10QWejKdDobVxDSqnVPJ0xQ 提取码:hhvj 复制这段内容后打开百度网盘手机Ap ...
- G. I love Codeforces
G. I love Codeforces 题目大意:给你n个字符串,以及m个喜欢关系,如果u喜欢v,这时候u会把它的用户名改为 I_love_ 加上v当时的用户名 Examples input 5an ...
- / Vijos / 题库 /1250 / 最勇敢的机器人
/ Vijos / 题库 /1250 / 最勇敢的机器人 借鉴博客:http://www.cnblogs.com/chty/p/5830516.html 背景 Wind设计了很多机器人.但是它们都认为 ...
- [luogu2594 ZJOI2009]染色游戏(博弈论)
传送门 Solution 对于硬币问题,结论是:当前局面的SG值等于所有背面朝上的单个硬币SG值的异或和 对于求单个背面朝上的硬币SG值...打表找规律吧 Code //By Menteur_Hxy ...
- 爬虫系列(三) urllib的基本使用
一.urllib 简介 urllib 是 Python3 中自带的 HTTP 请求库,无需复杂的安装过程即可正常使用,十分适合爬虫入门 urllib 中包含四个模块,分别是 request:请求处理模 ...
- Django - 日志工作中常用配置
工作中常用配置 # 日志配置 BASE_LOG_DIR = os.path.join(BASE_DIR, "log") LOGGING = { 'version': 1, # 保留 ...
- RestEasy用户指南---第6章.@QueryParam
转载说明出处:http://blog.csdn.net/nndtdx/article/details/6870391 原文地址 http://docs.jboss.org/resteasy/docs/ ...
- hdu 3657 最大点权独立集变形(方格取数的变形最小割,对于最小割建图很好的题)
转载:http://blog.csdn.net/cold__v__moon/article/details/7924269 /* 这道题和方格取数2相似,是在方格取数2的基础上的变形. 方格取数2解法 ...
- P1265 公路修建 洛谷
https://www.luogu.org/problem/show?pid=1265 题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公 ...