【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 ...
随机推荐
- Windows10简单启动项目添加方法
1,cmd输入 shell:startup 打卡启动项文件夹 2,将需要启动执行的项目的快捷方式放入此文件夹 3,重新启动系统就可以知道效果了
- 源码解析-url状态检测神器ping-url
前言 ping-url是我最近开源的一个小工具,这篇文章也是专门写它设计理念的科普文. 为什么会做这个ping-url开源工具呢? 起因是:本小哥在某天接到一个特殊的需求,要用前端的方式判断任意一个u ...
- linux安全加固项目
分享一个Linux加固脚本项目,可快速对服务器进行安全加固,顺便做下备忘,安全人员必须熟悉运维相关的知识! 支持的操作系统平台: Amazon 2013.03 Amazon 2013.09 Amazo ...
- python私有工具库小结
1.一些试用py工具清单 https://www.zhihu.com/question/60402355/answer/752917744?utm_source=wechat_session& ...
- Web开发之跨域问题
最近在工作上遇到了跨域方面的问题,借此温习巩固. 跨域是受到浏览器的同源策略引起的,为了防止某些文档或脚本加载别的域下的未知内容造成泄露隐私,破坏系统等安全行为. 那什么是同源的呢? 同源是指:应用协 ...
- medusa工具破解Linux系统
Sept1.安装medusa工具我这里直接yum安装 yum install -y medusa Step2.扫描网段里开发的22端口主机 nmap -sV -p22 -oG ssh 47.244.x ...
- Linux实验:NTP网络时间服务器
[实验目的] 1)掌握时间服务器的搭建方法 2)掌握客户机与时间服务同步时间的方法[实验原理] NTP服务器[Network Time Protocol(NTP)]是用来使计算机时间 ...
- Cloudera Manager 5.15.1忘记密码怎么破?
Cloudera Manager 5.15.1忘记密码怎么破? 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.登陆CM修改默认密码 1>.使用默认密码登陆CM界面 2&g ...
- root用户被提示:Operation not permitted
一.问题 今天为了删除一个多余的的软件,在删除该软件安装目录时,提示rm: cannot remove ‘.user.ini’: Operation not permitted,root权限都不能删除 ...
- python测试开发django-rest-framework-64.序列化(serializers.Serializer)
前言 REST framework中的serializers与Django的Form和ModelForm类非常像.我们提供了一个Serializer类,它为你提供了强大的通用方法来控制响应的输出, 以 ...