【POJ2965】The Pilots Brothers' refrigerator
本题知识点:深度优先搜索 + 暴力枚举 + 回溯 + 栈
如果对以上知识点还不熟悉的同学建议先做做 POJ1753 (本题是1753的提高版)(附 POJ1753博客)
以下默认您已ACPOJ1753
相信AC了Flip Game 这题的你,一看到这题就很自然地想到套 Filp Game 的模板。没错,其实几乎是一模一样的。只是存储数据有了一些不同而已。熟悉栈的话你也很自然想到用栈啦。
这里也是“翻棋”,只不过翻的不只是上下左右了,是要翻一整行列,所以这里要修改一下。
情况也还是分翻与不翻,同样需要回溯。
所以很快就可以AC了。不料我写的时候粗心大意没有把 ‘+’ 翻成 ‘-’ ,单这个小错误耽搁了很长的debug时间quq,大家要细心点呀
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
struct node{
int row, column;
}temp[100];
stack<node> ans, cnt;
char bri[10][10];
void init(){
while(!cnt.empty()) cnt.pop();
node a;
a.row = 1; a.column = 1;
for(int i = 0; i < 100; i++) ans.push(a);
}
bool check(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(bri[i][j] != '-') return false;
}
}
return true;
}
void show(){
for(int i = 0; i < 4; i++){
printf("%s\n", bri[i]);
} putchar('\n');
getchar();
}
void turn(int h, int w){
// row
for(int i = 0; i < 4; i++){
if(bri[h][i] == '+') bri[h][i] = '-';
else bri[h][i] = '+';
}
// column
for(int i = 0; i < 4; i++){
if(i == h) continue;
if(bri[i][w] == '+') bri[i][w] = '-';
else bri[i][w] = '+';
}
}
void dfs(int h, int w){
if(check()){
if(ans.size() > cnt.size()){
while(!ans.empty()) ans.pop();
int len = cnt.size();
for(int i = 0; i < len; i++){
temp[i] = cnt.top();
cnt.pop();
}
for(int i = len - 1; i >= 0; i--){
ans.push(temp[i]);
cnt.push(temp[i]);
}
}
}
if(h == 4) return ;
// change
turn(h, w);
node a;
a.row = h + 1; a.column = w + 1;
cnt.push(a);
if(w == 3) dfs(h + 1, 0);
else dfs(h, w + 1);
turn(h, w);
cnt.pop();
if(w == 3) dfs(h + 1, 0);
else dfs(h, w + 1);
}
int main()
{
init(); // 初始化
for(int i = 0; i < 4; i++) scanf("%s", bri[i]);
dfs(0, 0);
printf("%d\n", ans.size());
while(!ans.empty()){
node go = ans.top(); ans.pop();
printf("%d %d\n", go.row, go.column);
}
return 0;
}
【POJ2965】The Pilots Brothers' refrigerator的更多相关文章
- 【POJ 2965】 The Pilots Brothers' refrigerator
[题目链接] http://poj.org/problem?id=2965 [算法] 位运算 [代码] #include <algorithm> #include <bitset&g ...
- POJ2965——The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: following the stripy el ...
- 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 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- The Pilots Brothers' refrigerator 分类: POJ 2015-06-15 19:34 12人阅读 评论(0) 收藏
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20304 ...
- The Pilots Brothers' refrigerator
2965 he Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
随机推荐
- P1108 低价购买 (DP)
题目 P1108 低价购买 解析 这题做的我身心俱惫,差点自闭. 当我WA了N发后,终于明白了这句话的意思 当二种方案"看起来一样"时(就是说它们构成的价格队列一样的时候),这2种 ...
- Java自学-控制流程 结束外部循环
Java中结束外部循环 Java中如何结束外部for循环? 示例 1 : 结束当前循环 break; 只能结束当前循环 public class HelloWorld { public static ...
- Swiper4的基本使用
基本介绍: 中文文档地址:https://www.swiper.com.cn/ 它是一个开源,免费,强大的触摸滑动插件. 它是用纯Javascript打造的滑动特效插件,既可用于PC端,也可用于移动端 ...
- Devops Reference
摘自 https://www.cnblogs.com/yibutian/p/9561657.html DevOps 企业实践 实施DevOps的核心目标是加速团队.企业的IT精益运行,从根本上提升IT ...
- time的基本使用介绍
1.获取当前时间并格式化输出 import time t=time.gmtime() tplt='%Y-%m-%d %H:%M:%S' info=time.strftime(tplt,t) print ...
- Walle实现自动发布
walle是啥?能干啥?有啥用?这些我都不会去一一道来,如果你还没有明白前面提出的三个问题就不用往下看了,这里这回将walle安装了怎么去使用.如果都要面面俱到不是一两篇博客可以解决的问题,如果希望将 ...
- sublime text3常用的一些快捷键
--------------------------------下面的内容可以打印出来贴在电脑旁提醒自己-------------------- Ctrl + Shift + D 快速复制当前的一行 ...
- JS正则表达式提取数字
/** * [参数str] * @type {var String} * return 30 */ var str = "ren民BI30kuai" console.log(str ...
- 性能测试基础---URL和HTTP协议
·URL和HTTP协议: ·URL构成: URL是web应用进行资源访问的主要方式.一般来说,由五个部分构成: 示例:http://192.168.2.212/phpwind1/searcher.ph ...
- 微信小程序之执行环境
明白了小程序中的 JavaScript 同浏览器以及NodeJS有所不同后,开发者还需要注意到另外一个问题,不同的平台的小程序的脚本执行环境也是有所区别的. 小程序目前可以运行在三大平台: iOS平台 ...