题目: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. ES6中Set集合(与java里类似)

    一.引入背景 Set集合是一种无重复元素的列表,开发者们一般不会逐一读取数组中的元素,也不太可能逐一访问Set集合中的每个元素,通常的做法是检测给定的值在某个集合中是否存在 Map集合内含多组键值对, ...

  2. log4j教程 2、安装

    Log4j的API包使用Apache软件许可证,由开源倡议认证一个完全成熟的开源许可证下发布. 最新log4j的版本,包括完整的源代码,类文件和文档可以在这里找到 http://logging.apa ...

  3. Linux查看目录大小

    du -ah --max-depth=1 a表示显示目录下所有的文件和文件夹(不含子目录) h表示以人类能看懂的方式 max-depth表示目录的深度

  4. dom元素父子容器互相调用控制

    在html中普通的父容器调用子容器中的方法十分简单 因为这两个容器的所有方法和属性都在同一个dom模型中 可以直接控制和使用 但是如果子容器中是一个iframe标签又是怎样的情况? iframe请求另 ...

  5. mvn 更改打包的名称

    在pom.xml中加入以下代码 <build> <finalName>moon</finalName> <pluginManagement> <p ...

  6. postman里面的mockserver使用方法

    转载:http://blog.csdn.net/Cloud_Huan/article/details/78326159 首先说下mockserver是干啥的,从英文翻译理解就是模拟一个服务器,通俗点说 ...

  7. Yii2数据库分页操作方法介绍

    本章节将介绍怎样怎样创建一个从数据表 country 中获取国家数据并显示出来的页面. 为了实现这个目标,你将会配置一个数据库连接.创建一个活动记录类,而且创建一个操作及一个视图. 贯穿整个章节,你将 ...

  8. django book学习问题记录

    —————————————————————————————————— 位置:第五章<模型> 问题描述(已解决): >>> p1 = Publisher.objects.c ...

  9. LeetCode题目:Minimum Path Sum

    原题地址:https://leetcode.com/problems/minimum-path-sum/ 大意:给出一个二维数组(int类型),求出从左上角到右下角最短的路径. 解决方法:动态规划 c ...

  10. LeetCode题目:Gray Code

    原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...