题目传送门

本题知识点:深度优先搜索 + 暴力枚举 + 回溯 + 栈

如果对以上知识点还不熟悉的同学建议先做做 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的更多相关文章

  1. 【POJ 2965】 The Pilots Brothers' refrigerator

    [题目链接] http://poj.org/problem?id=2965 [算法] 位运算 [代码] #include <algorithm> #include <bitset&g ...

  2. POJ2965——The Pilots Brothers' refrigerator

    The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: following the stripy el ...

  3. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16868 ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22057 ...

  6. The Pilots Brothers' refrigerator(dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19718 ...

  7. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  8. 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 ...

  9. The Pilots Brothers' refrigerator

    2965 he Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

随机推荐

  1. 【openshift】OC命令部署Openshift

    OC命令部署Openshift # install openshift wget -c https://github.com/openshift/origin/releases/download/v3 ...

  2. Java 相等判断

    ==的判断机制是:根据两边的内存地址是否相同来判断. equals()是Object类的一个实例方法,判断机制和 == 完全一样. String类重写了equals()方法,是根据数据值来判断的. 总 ...

  3. let 和 var 定义变量的区别

    一.变量提升 var 存在变量提升,而 let 不存在变量提升,所以用 let 定义的变量一定要在声明后再使用,否则会报错. var //var定义的变量存在变量提升,变量会把声明提升到整个作用域的最 ...

  4. CentOS 7 使用 firewalld 打开关闭防火墙与端口

    1.firewalld的基本使用启动: systemctl start firewalld关闭: systemctl stop firewalld查看状态: systemctl status fire ...

  5. React: 无状态组件生成真实DOM结点

    在上一篇文章中,我们总结并模拟了 JSX 生成真实 DOM 结点的过程,今天接着来介绍一下无状态组件的生成过程. 先以下面一段简单的代码举例: const Greeting = function ({ ...

  6. Linux DHCP 中继

    具体到一个公司的网络环境中,不可能只有一个VLAN,更不可能对每个VLAN都架设一个DHCP服务器,这时就要做一个DHCP的中继,使得DHCP的广播可以通过VLAN. 实验拓扑 三层交换机下面连接一台 ...

  7. Linux命令——groups

    groups用于查询当前用户的属组,没有参数.

  8. svn: E155004: Working copy '/data/www' locked.

    svn: run 'svn cleanup' to remove locks (type 'svn help cleanup' for details) svn: E155004: Working c ...

  9. C++(四十九) — set、multiset 容器的基本操作

     1.set的基础知识 set的特性是:所有元素都会根据元素的键值自动排序,set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值就是实值,实值就是键值.set不允 ...

  10. 深度学习Keras框架笔记之TimeDistributedDense类

    深度学习Keras框架笔记之TimeDistributedDense类使用方法笔记 例: keras.layers.core.TimeDistributedDense(output_dim,init= ...