【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 ...
随机推荐
- Java自学-数组 增强型for循环
Java 中如何使用增强for循环 增强型for循环在遍历一个数组的时候会更加快捷 步骤 1 : 增强型for循环 注:增强型for循环只能用来取值,却不能用来修改数组里的值 public class ...
- rename file
https://askubuntu.com/questions/790633/the-o-parameter-in-aria2c-cant-rename-the-downloaded-file You ...
- Sonatype安装
https://www.cnblogs.com/wotoufahaiduo/p/11223834.html Sonatype Nexus Repository Manage admin ccf0cab ...
- 修复使用sub和sup时的行间距问题
sub和sup元素会轻微地增大行高. 幸好,用一点CSS就可以修复这个问题. 来自Nicolas Gallagher和Jonathan Neal的normalize.css(http://necola ...
- jquery实现倒计时
<html> <head> <meta charset="utf-8"/> <title>jquery实现倒计时</title ...
- 【Excel】【Salesforce】函数拓展
1.if 2.vlookup
- OSX - Mac OS 10.12后Caps lock(大写键)无法使用的解决办法
我在OSX的虚拟机中安装了windows 7 操作系统,但是发现在win7下,大写键不起作用,通过下面方面搞定了! ▲打开设置中的键盘选项,并切换至输入源选项标签, ▲取消勾选“使用大写锁定键来回切换 ...
- k8s日志收集及存档
k8s日志收集架构图 利用阿里开源的工具log-pilot,往kafka内写日志,然后吐一份至es,另外一份用flume消费kafka数据落盘
- Nginx 常用命令并实现最基本的反向代理
nginx 命令 测试配置文件格式是否正确:$ nginx -t 启动:nginx 重启:nginx -s reload 获取nginx进程号: ps -ef|grep nginx 停止进程(mast ...
- 【书评:Oracle查询优化改写】第三章
[书评:Oracle查询优化改写]第三章 BLOG文档结构图 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① 隐含参数 ...