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 ...
随机推荐
- poptip 外面 放 input 使用 iview vue
外层套的是 <FormItem prop="name" label="姓名:"> <Input v-model="tFill.nam ...
- docker 框架概述
docker的框架 docker 使用传统的client-server架构模式,用户端通过docker client 与docker daemon 建立通信,并将请求发送给后者,而docker后端时 ...
- faster rcnn训练过程讲解
http://blog.csdn.net/u014696921/article/details/60321425
- Bézier surface(贝塞尔曲面)
Bézier surface(贝赛尔曲面) 贝塞尔曲面是一种用于计算机图形学.计算机辅助设计和有限元建模的数学样条.与贝塞尔曲线一样,贝塞尔曲面由一组控制点定义.与插值在许多方面相似,一个关键的区别是 ...
- C-基础:memcpy、memset、memmove、memcmp、memchr
一,原型 void * memcpy ( void * destination, const void * source, size_t num ); 功能:将以source作为起始地址的数据复制nu ...
- 《3+1团队》【Alpha】Scrum meeting 2
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...
- Ubuntu 18的网络配置
包括Ubuntu 18.04和18.10,设置为静态IP及DNS. sudo vim /etc/netplan/50-cloud-init.yaml network: ethernets: enp4s ...
- xshell全局设置配色方案
新建XTerm1.xcs文件,将以下内容黏贴进去,保存退出 [XTerm] text=839496 cyan(bold)=93a1a1 text(bold)=408080 magenta=dd3682 ...
- HTML5新增的主体元素article、section、nav、aside、time元素和pubdate属性
article artticle元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容.它可以是一篇博客或者报刊中的文章,一篇论坛帖子,一段用户评论或者独立的插件或其他任何独立的内容. ...
- MVC常见框架
Struts Struts是Apache软件基金下Jakarta项目的一部分.Struts框架的主要架构设计和开发者是Craig R.McClanahan.Struts 是Java Web MVC框架 ...