链接:poj 2965

题意:给定一个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&#39; refrigerator(dfs 枚举 +打印路径)的更多相关文章

  1. poj 2965 The Pilots Brothers&#39; refrigerator

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18040 ...

  2. POJ - 2965 - The Pilots Brothers&#39; refrigerator (高效贪心!!)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19356 ...

  3. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  4. POJ 2965:The Pilots Brothers&#39; refrigerator

    id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. poj 2965 The Pilots Brothers' refrigerator (dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17450 ...

  6. POJ 2965 The Pilots Brothers' refrigerator (DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15136 ...

  7. poj2965 The Pilots Brothers&#39; refrigerator(直接计算或枚举Enum+dfs)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem? id=2965 ---- ...

  8. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  9. 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 ...

随机推荐

  1. Spring JDBC 例子

    http://www.yiibai.com/spring/spring_jdbc_example.html 要了解有关Spring JDBC框架与JdbcTemplate类的概念,让我们写这将实现所有 ...

  2. 两个乒乓球队进行比赛,各出三人。 甲队为a,b,c三人,乙队为x,y,z三人。 已抽签决定比赛名单。 有人向队员打听比赛的名单。 a说他不和x比,c说他不和x,z比, 请编程序找出三队赛手的名单。

    题目:两个乒乓球队进行比赛,各出三人. 甲队为a,b,c三人,乙队为x,y,z三人. 已抽签决定比赛名单. 有人向队员打听比赛的名单. a说他不和x比,c说他不和x,z比, 请编程序找出三队赛手的名单 ...

  3. PHP02 PHPStrom2018.X与WAMPServer3.0.6的集成

    脚本运行环境设置:设置PHPStorm中的脚本在PHP解析器上运行 1.进入Filie>>>setting>>languages and FrameWorks 选择php ...

  4. 洛谷 P3131 子共七

    看到这一题第一印象就是暴力好打,$O(n^2)$,预计得分$70$分 这明显满足不了啊,我们要用到前缀和. $sum[i]$记录到i的前缀和,区间$[a,b]$的和就是$sum[b]-sum[a-1] ...

  5. [LUOGU] P3871 [TJOI2010]中位数

    题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...

  6. [模板] LIS

    树状数组优化LIS到nlogn,网上找了好多,感觉讲得都不是很明白,正好自己复习整理一下. 基本的DP方程 f[i]=max(f[i],f[j]+1) (j<i且a[j]<a[i]) 定义 ...

  7. ubuntu18.04 server配置静态ip

    最新发布的ubuntu18.04 server,启用了新的网络工具netplan,对于命令行配置网络参数跟之前的版本有比较大的差别,现在介绍如下:1.其网络配置文件是放在/etc/netplan/50 ...

  8. bacula快速部署

    快速部署: Server端:DD.SD.Monitor.Console均部署在Server上Client端:FD Server端部署:上传事先下载的源码包 tar xvf bacula-9.2.0.t ...

  9. bootstrap3之栅格系统

    原理 栅格系统的核心就是媒体查询.指定的尺寸都是百分比,也就是流式布局. 查看bootstrap中的源码可以发现,对样式的定义次序全都是依次 xs.sm.md.lg,如: // grid-framew ...

  10. cetos7 安装telnet

    先执行命令 rpm -qa | grep telnet rpm -qa | grep telnet-server rpm -qa | grep xinetd 如果返回空白就是没有安装 yum inst ...