题目:http://poj.org/problem?id=2965

来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#problem/B


题意:

就是把题目中给出的状态图,全部翻转成
----

----

----

----状态
翻转: 每次翻转一个,那么它所在的行和列都要翻转
问最小翻转次数,同时输出翻转路径.

算法:

暴力 + 枚举 + dfs

思路:

可以证明每个把手要么翻转,要么不翻转,那么从左到右,从上到下依次枚举每个把手,同时深搜一遍即可。
和前面的棋盘翻转一样.[POJ 1753 Flip Game] 做了棋盘翻转都纠结了半天才出了这题,实在是弱爆了Orz
具体的过程还是看代码中的注释好了.

正解应该是状态压缩了,前面一直不懂状态压缩 = = 总结完了希望有空可以学一下...

PS:在网上另外看到了一个神解,不过只能解决这种翻转一个就同时翻转同行同列问题了,对于棋盘问题是无法解决的.

code:


2965 Accepted 204K 391MS C++ 1563B

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std; int map[5][5];
int x[20]; // 临时路径
int y[20]; int ansX[20]; // 最终路径
int ansY[20];
int ans = 33; void build()
{
char c;
memset(map, 0, sizeof(map));
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cin>>c;
if(c == '-') map[i][j] = 1; //open
else map[i][j] = 0;
}
}
} void flip(int s)
{
int x1 = s/4; // 行
int y1 = s%4; // 列 for(int i = 0; i < 4; i++)
{
map[i][y1] = !map[i][y1];
map[x1][i] = !map[x1][i];
}
map[x1][y1] = !map[x1][y1];
} bool complete() // 判断是否达到目标
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
if(map[i][j] == 0) return false;
}
return true;
} void dfs(int s, int b) // 遍历到第 s 个, 翻转了 0个
{
if(complete())
{
if(ans > b)
{
ans = b;
for(int i = 1; i <= ans; i++)
{
ansX[i] = x[i];
ansY[i] = y[i];
}
}
return;
} if(s >= 16) return; dfs(s+1, b); //不管第 s 个,直接往下找 flip(s); //翻转第 s 个了再往下找
x[b+1] = s/4+1; //临时记录路径【注意】
y[b+1] = s%4+1; dfs(s+1, b+1); //翻转第 s 个了再找下一个 flip(s); //回溯
} int main()
{
build();
dfs(0, 0);
printf("%d\n", ans);
for(int i = 1; i <= ans; i++)
{
printf("%d %d\n", ansX[i], ansY[i]);
}
return 0;
}


POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】的更多相关文章

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

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

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

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

  3. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  4. 枚举 POJ 2965 The Pilots Brothers' refrigerator

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

  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 位运算枚举

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

  7. POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

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

  8. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

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

  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. Python 并行任务技巧

    FROM:    http://segmentfault.com/a/1190000000382873 Python的并发处理能力臭名昭著.先撇开线程以及GIL方面的问题不说,我觉得多线程问题的根源不 ...

  2. fabricjs 高级篇(自定义类型)

    原文:https://www.sitepoint.com/fabric-js-advanced/ <html> <head> <script src='./js/fabr ...

  3. dmz主机就是DNAT功能的体现

    端口映射和DMZ是提供内网和外网映射的,具体各自如下:DMZ:就相当于DNAT(Destination NAT),只对目的IP地址做地址转换.也就是说,收到目的IP为自己WAN口的包,统统转发给内网的 ...

  4. Java虚拟机内存分配和回收策略

    1 对象优先分配在Eden区 对象优先在Eden进行分配,大多数情况下,对象在新生代Eden区进行分配.当Eden区没有足够的空间进行分配时,虚拟机会发起一次Minor GC. 新生代GC(Ninor ...

  5. 转载:JS进度条

    转载地址:http://blog.csdn.net/treeClimber/article/details/569974 代码在原基础上稍作改动,如下: <!DOCTYPE HTML PUBLI ...

  6. vsftpd 配置:chroot_local_user与chroot_list_enable userlist_enable userlist_deny详解

    默认情况下,如果设置了 userlist_enable=YES,当 userlist_deny 选项设置为 YES 的时候,userlist_file=/etc/vsftpd.userlist 中列出 ...

  7. LeetCode题目:Best Time to Buy and Sell Stock

    原题地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 解决方法:动态规划,minimun存储的是当前价格中最小的. c ...

  8. 使用transform和transition制作CSS3动画

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  9. 在eclipse中使用Maven建web工程的两种方式

    Eclipse版本:Neon Release (4.6.0) Maven版本:3.3.9 第一种方式: 右键新建maven工程,勾选创建一个简单工程 填入信息,注意打包方式要改为war 点击完成,创建 ...

  10. java 中的 i++ 和 ++i

    熟悉c/c++中的i++和++i,那么你知道下面的java代码效果是什么吗? 一 . 代码示例 /** * * @author elelule * */ public class TestPlusPl ...