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 ...
随机推荐
- 【转】C#中的==、Equal、ReferenceEqual
[转]C#中的==.Equal.ReferenceEqual 转载自: http://www.cnblogs.com/zagelover/articles/2741409.html 1. Refere ...
- git-忽略文件改动不进行提交
命令:git update-index --assume-unchanged 文件名 作用:忽略文件的改动,但是不加入.gitignore 文件中,这样可以达到仅在本地目录中忽略,不影响其他团队成员的 ...
- [SNOI2019]数论
题目 考虑对于每一个\(a_i\)计算有多少个\(0<x\leq T-1\)满足\(x\equiv a_i(mod\ P)\)且\(x\ mod\ Q \in B\) 显然\(x=a_i+k\t ...
- loadClass和forName 的区别
类的加载方式 1.隐式加载:new 2.显式加载,loadClass,forName 等 loadClass 和 forName的区别 类的装载过程 1.加载:通过classLoader加载class ...
- Spring框架 (log4j :WARN No appenders could be found for logger log4j:WARN Please initialize the log4j system properly.)问题解决
Spring框架需要的jar包 1.Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/ ...
- 分享点干货(此this非彼this)this的详细解读
在javascript编程中,this关键字经常让初学者感到迷惑,这里,针对此this根据查阅的资料和个人的理解分享一下,纯手打,大神勿喷. 首先先说一下this的指向,大致可以分为以下四种. 1.作 ...
- visual studio中使用vim快捷键
vsvim下载链接: https://marketplace.visualstudio.com/items?itemName=JaredParMSFT.VsVim 下载,关闭visual studio ...
- vue element-ui中引入第三方icon
vue element-ui中引入第三方icon 把图标加入项目 设置项目名称,下载项目(项目名称自定义) 解压项目到开发目录 在main.js中全局引入css文件 修改下载下来的项目中的css文件, ...
- solr之windws下搭建solr服务
安装Solr 首先保证已经正确安装了Java 下载Solr,当前最新版6.1.0 Solr各个版本下载地址 Solr从6.0之后需要Java1.8所以如果使用Solr6.0及其以上版本,请确保Java ...
- 【C#】读书笔记
一,C#对象初始化语法: Product p = new Product() { Name = "小黄人", Price = , Description = "机智&qu ...