POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
题目:http://poj.org/problem?id=2965
来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#problem/B
题意:
----
----
----状态
算法:
思路:
code:
#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】的更多相关文章
- 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 (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- 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 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- 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 ...
随机推荐
- Java三大器之过滤器(Filter)的工作原理和代码演示
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术之一,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp,Servlet, 静 ...
- eclipse显示包的层次关系
如何在eclipse中显示包的层次关系呢?如下图所示
- Mybatis 批量插入数据
--mybatis 批量插入数据 --1.Oracle(需要测试下是否支持MySQL) < insert id ="insertBatch" parameterType=&q ...
- linux与开发板串口通信
研究了一天的linux串口,结果改了树莓派的系统配置文件config.txt给改了导致系统崩溃....其实我感觉网上的大多数方法都是不符合新版本树莓派的,网上的方法是通过修改系统配置文件后安装mini ...
- 基于Android Classic Bluetooth的蓝牙聊天软件
代码地址如下:http://www.demodashi.com/demo/12133.html BluetoothChat 基于Android Classic Bluetooth的蓝牙聊天软件,目前仅 ...
- 应用程序之UIWebView的使用
UIWebView简介 知识点总结 代码实现 一.UIWebView简介 1.是iOS内置的浏览器控件,可以浏览网页.打开文档等2.能够加载html/htm.pdf.docx.txt等格式的文件3.系 ...
- kafka 0.8.1 新producer 源码简单分析
1 背景 最近由于项目需要,需要使用kafka的producer.但是对于c++,kafka官方并没有很好的支持. 在kafka官网上可以找到0.8.x的客户端.可以使用的客户端有C版本客户端,此客户 ...
- JDBC技术总结(三)
1. 数据库连接池 JDBC部分的前两个总结主要总结了一下JDBC的基本操作,而且有个共同点,就是应用程序都是直接获取数据库连接的.这会有个弊端:用户每次请求都需要向数据库获得连接,而数据库创建连接通 ...
- MQTT--Mosquitto的配置文件
Mosquitto的配置文件存放在/etc/mosquitto/mosquitto.conf 配置文件具体的配置内容为: # ===================================== ...
- MongoDB 的聚集操作
聚合引言 聚集操作就是出来数据记录并返回计算结果的操作.MongoDB提供了丰富的聚集操作.可以检測和执行数据集上的计算.执行在mongod上的数据聚集简化了代码和资源限制. 像查询一样,在Mongo ...