poj2965The Pilots Brothers' refrigerator
背景:和poj1753一样,用dfs就可以做出来,只是和1753相比较得输出一些步骤,不过这也不麻烦,直接用两个数组就可以存储了。不过记住当递归回来的时候记住把数组里面对应位置的元素清零。
思路:同上一篇1753.
#include <stdio.h>
#include <string.h>
int q[4][4],ok=0,r[16],c[16];
int iswin(void)
{for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(q[i][j]!=1) return 0;return 1;
}
void change(int i,int j)
{q[i][j]=!q[i][j];for(int k=0;k<4;k++){q[i][k]=!q[i][k];q[k][j]=!q[k][j];}
}
void dfs(int m,int sum,int a,int b)
{if(ok) return;else if(m==sum){if(iswin()){ok=1;printf("%d\n",sum);for(int k=0;k<sum;k++)printf("%d %d\n",r[k],c[k]);}else return;}else{for(int i=a;i<4;i++){for(int j=b;j<4;j++){r[m]=i+1;c[m]=j+1;change(i,j);if(j<3) dfs(m+1,sum,i,j+1);else {dfs(m+1,sum,i+1,0);b=0;}change(i,j);r[m]=0;c[m]=0;}}return;}
}
int main(void)
{char ch;memset(r,0,sizeof(r));memset(c,0,sizeof(c));for(int i=0;i<4;i++)for(int j=0;j<4;j++){scanf("%c",&ch);if(j==3) getchar();q[i][j]=(ch=='+')?0:1;}if(iswin()) {printf("%d\n",0);return 0;}for(int k=1;k<=16;k++){dfs(0,k,0,0);if(ok) break;}return 0;
}
在网上看到有这样用递归的,感觉好高大上。
虽然他写的代码,我有一些地方不懂,不过这些都是我要学习的东西。
他的思路:
1.一个元素翻转奇数次状态不变,翻转偶数次状态改变
由1容易推到出2
2:要想把第Sij翻转,同时保持第i行和第j列其他元素状态不变,sij本身翻转两次,第i和第j列的其他元素翻转一次.
由把所有状态为关的元素都做一次2操作,记录那些元素状态变化过,变化过的元素个数即最小步骤数.
由以上分析得出,各个元素的步骤顺序其实是无所谓的.把变化的过元素按任意顺序输出即步骤.
附代码:
#include <iostream>
#include <queue>
bool mark[4][4];
struct Pos
{ int x; int y;
};
std::queue<Pos> Ans;
int input()
{ memset(mark,0,sizeof(mark)); int i ,j,k; char ch; for (i = 0 ;i < 4 ; ++ i) { for (j = 0 ;j < 4 ; ++j) { ch = getchar(); if ('+' == ch) { mark[i][j] = !mark[i][j]; for(k = 0 ; k < 4 ; k ++) { mark[i][k] = !mark[i][k]; mark[k][j] = ! mark[k][j]; } } } getchar(); } return 0;
}
int main()
{ int nStep = 0; int i,j; input(); for ( i = 0 ; i < 4; ++i) { for (j = 0 ; j < 4 ; ++j) { if (mark[i][j]) { nStep ++; Pos pos; pos.x = i + 1; pos.y = j + 1; Ans.push(pos); } } } std::cout <<nStep<<std::endl; while (!Ans.empty()) { Pos pos = Ans.front(); Ans.pop(); std::cout<<pos.x<<" "<<pos.y<<std::endl; } return 0;
}
poj2965The Pilots Brothers' refrigerator相关推荐
- POJ-2965-The Pilots Brothers\' refrigerator
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24651 Accepted: 9504 Special Judge Descri ...
- POJ 2965.The Pilots Brothers‘ refrigerator
POJ 2965.The Pilots Brothers' refrigerator Ideas 题意:给你4*4的矩阵.每个点有两种状态,+代表关,-代表开.每个点有一个操作就是该点所在行列所有状态 ...
- poj 2965 The Pilots Brothers' refrigerator
http://poj.org/problem?id=2965 poj 1753扩展,dfs+枚举,不过加了一个路径. The Pilots Brothers' refrigerator Time Li ...
- ACM POJ 2965 The Pilots Brothers' refrigerator
http://poj.org/problem?id=2965 The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65 ...
- B - The Pilots Brothers' refrigerator
B - The Pilots Brothers' refrigerator 文章目录 B - The Pilots Brothers' refrigerator 题目描述: Input: Output ...
- POJ2965 The Pilots Brothers‘ refrigerator
POJ2965 The Pilots Brothers' refrigerator 题干 Description Input Output Sample Input Sample Output 题意 ...
- The Pilots Brothers‘ refrigerator(思维)
题面:The Pilots Brothers'refrigerator 题目大意 "飞行员兄弟"这个游戏,需要玩家打开一个有着16把手的冰箱. 每个把手有且只有两种状态:打开(−- ...
- POJ-2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27917 A ...
- The Pilots Brothers' refrigerator - poj 2965
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20325 Accepted: 7830 Special Judge ...
最新文章
- python 虚拟环境 tensorflow GPU
- sea.js学习网址和书籍
- 无锡朗贤获B+轮融资,辰韬资本、兴韬投资领投
- flutter - 将数字转换为科学记数法并获得指数 flutter
- 【Python】文本进度条
- JS 动态清除Div中内容
- Linux虚拟网络设备之tun/tap
- 法兴用数字证明中国期货市场是真正的投机市
- javaweb课程设计房屋租赁系统
- CPU数据总线和地址总线 内存和外存
- La3713 Astronauts 2-Sat
- 简单人物画像_简易人物画像图
- c语言偶数求和while,C语言中编程计算1至100以内的奇数和偶数并分别求和,求代码...
- 计算机高级工程师职称评定条件,高级工程师职称评定条件是什么
- python换行输入数据_python 对比两个文件内容或字符串内容时的换行符/交作业检测小程序...
- WPF中的MVVM模式
- 诛仙哪里炼器服务器最稳定,告别炼器难 《诛仙2》顶级炼器师云集诛仙电台
- 经典管理学书籍推荐:《经理人参阅:企业管理实务》
- 为什么要通过金融直播来获客?这可以从其优势得知
- 单相交流电和三相交流电概念扫盲