poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
题意:给定一个4*4矩阵状态,代表门的16个把手。‘+’代表关,‘-’代表开。当16个把手都为开(即‘-’)时。门才干打开,问至少要几步门才干打开
改变状态规则:选定16个把手中的随意一个,能够改变其本身以及同行同列的状态(即若为开,则变为关,若为关,则变为开),这一次操作为一步.
分析:这题与poj 1753思路差点儿相同,每一个把手最多改变一次状态,
全部整个矩阵最多改变16次状态
思路:直接dfs枚举全部状态,直到找到目标状态
可是要打印路径,全部应在dfs时记录路径
注意:能够用位运算操作。这样比較快。若直接推断‘+’‘-’状态变换,非常可能超时
<pre name="code" class="cpp">#include<stdio.h>
int s[5][5],step,r[20],c[20],flag;
int judge() //推断把手是否全开
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(s[i][j])
return 0;
return 1;
}
void flip(int i,int j) //改变状态
{
int k;
s[i][j]=!s[i][j];
for(k=0;k<4;k++){
s[i][k]=!s[i][k];
s[k][j]=!s[k][j];
}
}
void dfs(int i,int j,int num)
{
if(num==step){
flag=judge();
return ;
}
if(flag||i==4)
return ;
flip(i,j);
r[num]=i; //记录路径
c[num]=j;
if(j<3)
dfs(i,j+1,num+1);
else
dfs(i+1,0,num+1);
flip(i,j); //回溯
if(j<3)
dfs(i,j+1,num);
else
dfs(i+1,0,num);
return ;
}
int main()
{
int i,j;
char t;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
scanf("%c",&t);
if(t=='+')
s[i][j]=1; //用0标记开,1标记关,方便位运算
else
s[i][j]=0;
}
getchar();
}
for(step=0;step<=16;step++){
dfs(0,0,0);
if(flag)
break;
}
printf("%d\n",step);
for(i=0;i<step;i++)
printf("%d %d\n",r[i]+1,c[i]+1);
return 0;
}
poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)的更多相关文章
- poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19356 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- POJ 2965:The Pilots Brothers' refrigerator
id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem? id=2965 ---- ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
随机推荐
- uva1614 Hell on the Markets
贪心部分的理论依据:前i个数可以凑出1-sum[i]的所有整数. 证明:第二类数学归纳,n=1时成立,假设n=k之前所有项都成立,当n=k+1时.sum[k+1]=sum[k]+a[k+1].只需证明 ...
- django URL,views,html请求顺序
进来的请求转入/hello/. Django通过在ROOT_URLCONF配置来决定根URLconf. Django在URLconf中的所有URL模式中,查找第一个匹配/hello/的条目 ...
- numpy次方计算
>>> 2**np.arange(3, 6) array([ 8, 16, 32])
- python基础:函数传参、全局变量、局部变量、内置函数、匿名函数、递归、os模块、time模块
---恢复内容开始--- 一.函数相关: 1.1位置参数: ef hello(name,sex,county='china'): pass #hello('hh','nv') #位置参数.默认参数 1 ...
- 手把手入门docker (好多图)
1.什么是docker? ---->我的理解是将许多应用一起打包成一个镜像,拿这个镜像去其他服务器上运行起来就可以.不需要单个单个去配置啦. 2.怎样在window下的安装. ---->刚 ...
- 微信小程序:this code is a mock one
问题 微信小程序调用wx.login() 的 success 函数带的code 提示this code is a mock one 解决方法 appid和微信小程序开发工具所登陆用户管理的小程序清单不 ...
- spring-mvc junit测试
import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; impor ...
- python_OS 模块
os模块 用于提供系统级别的操作 os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") # 改变当前脚本工作目 ...
- 【MySQL】性能优化之 Index Condition Pushdown
一 概念介绍 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式.a 当关闭ICP时,index ...
- Leetcode 260.只出现一次的数字III
只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5 ...