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 文件上传数据库
存储文件的数据库类型: 1.oracle :Blob,bfile类型 2.mysql:longblob类型 3.sqlserver :varbinary(Max)类型 文件都是以二进制流存入数据库的, ...
- Node.js 本地Xhr取得Node.js服务端数据的例子
本以为用XHR取Nodejs http出的一段文字很简单,因为xhr取值和nodejs http出文字都是好弄的,谁知一试不是这回事,中间有个关键步骤需要实现. nodejs http出文字显示在浏览 ...
- POJ 1040 Transportation
链接:http://poj.org/problem?id=1040 Transportation Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- iOS实录:GCD使用小结(一)
导语:在iOS中,多线程方案有四种:pthread.NSThread.NSOperation & NSOperationQueue 和 GCD,但是开发中GCD使用得最多,本文主要总结一下我使 ...
- Activity启动活动最佳写法
一,在被启动的Activity中新加一个静态方法public static void actionStart(Context context, String data1, String data2) ...
- jQuery 技术揭秘
1.平时jquery调用页: index.html : <!DOCTYPE html> <html lang="en"> <head> < ...
- jquery文件上传控件 Uploadify(转)
原文:http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同时上 ...
- [译] 通过 contentEditable 属性创建一个所见即所得的编辑器(富文本编辑器)
译者注 这只是一篇入门教程,介绍了一些基础知识,仅供参考,切不可因此觉得富文本编辑器很简单. 创建富文本编辑器是一个非常复杂的工程,需要考虑到方方面面,也有很多坑(请参考原文第一条评论). 为免误导大 ...
- MQTT--linux安装部署(CentOS)
OS环境:CentOS6.5 1.安装依赖 yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake ...
- Https单向认证和双向认证介绍
一.Http HyperText Transfer Protocol,超文本传输协议,是互联网上使用最广泛的一种协议,所有WWW文件必须遵循的标准.HTTP协议传输的数据都是未加密的,也就是明文的,因 ...