http://poj.org/problem?id=2965

题目跟1753 差不多,这是是说当前位置如果翻转的话,则其所在的行和所在的列都需要翻转,最后求都翻转为1的最小步数。

思路还是用二进制来表示每一种状态, 即用一个数组枚举16个位置的16种情况,打表。 还有就是输出路径,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int sd,key=0;
int vis[1<<16];
int dir[16] = {4383,8751,17487,34959,4593,8946,17652,35064,7953,12066,
20292,36744,61713,61986,62532,63624};
struct node
{   //sum 表示当前状态,num表示有多少次翻转,step表示经过了步数int sum,num,step;
}cur;
struct path
{   //x表示存当前点的下标,pre是当前点的前驱int x,pre;
}p[100001];
void f(int index)//递归输出路径
{if(index){f(p[index].pre);  //感觉用的很巧妙,除以4+1得到了当前下标的行数,%4+1得到了当前下标的列数printf("%d %d\n",p[index].x/4+1,p[index].x%4+1);}
}
void bfs()
{int i,j,k;cur.sum=sd;cur.num=0;cur.step=0;p[1].pre=0;queue<node>q;q.push(cur);while(!q.empty()){node b=q.front();q.pop();if(b.sum==0) //全部状态一样,即异或为0{printf("%d\n",b.step);f(b.num);return;}for(i=0;i<16;i++){node a=b;a.sum^=dir[i];if(!vis[a.sum])  //没有访问,置标志{a.num=++key;a.step++;p[key].pre=b.num;p[key].x=i;q.push(a);vis[a.sum]=1;}}}
}/*
int num[16];        //¼´Çóchange[]µÄ·½·¨void init(){for(int i=0;i<16;i++){num[i]=0;for(int j=0;j<16;j++)if((i/4==j/4) || (i%4==j%4))num[i]=(num[i]<<1)+1;elsenum[i]<<=1;}
}
*/int main()
{int i,j;char s[5];for(i=0;i<4;i++){scanf("%s",s);for(j=0;j<4;j++){if(s[j]=='+'){sd+=(1<<i*4+j);}}}bfs();return 0;
}

用dfs差点超时 ,仿照上一题解法。就是记录路径的时候需要多开了两个数组,然后翻转的时候记录,不反转不记录。

#include<cstdio>
int s[5][5],r[17],w[17],step,flag;
int range()
{for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(s[i][j]!=1)return 0;return 1;
}void solve(int x,int y)
{s[x][y]=!s[x][y];for(int i=0;i<4;i++){s[i][y]=!s[i][y];s[x][i]=!s[x][i];}return;
}void dfs(int row,int col,int deep)
{if(deep==step){flag=range();return;}if(flag||row==4) return;solve(row,col);r[deep]=row;w[deep]=col;if(col<3) dfs(row,col+1,deep+1);else dfs(row+1,0,deep+1);solve(row,col);if(col<3) dfs(row,col+1,deep);else dfs(row+1,0,deep);return;
}int main()
{char c;for(int i=0;i<4;i++){for(int j=0;j<4;j++){scanf("%c",&c);if(c=='-') s[i][j]=1;}getchar();}for(step=0;step<=16;step++){flag=0;dfs(0,0,0);if(flag) break;}printf("%d\n",step);for(int i=0;i<step;i++)printf("%d %d\n",r[i]+1,w[i]+1);return 0;
}

poj-2905 The Pilots Brothers' refrigerator相关推荐

  1. POJ 2965.The Pilots Brothers‘ refrigerator

    POJ 2965.The Pilots Brothers' refrigerator Ideas 题意:给你4*4的矩阵.每个点有两种状态,+代表关,-代表开.每个点有一个操作就是该点所在行列所有状态 ...

  2. poj 2965 The Pilots Brothers' refrigerator

    http://poj.org/problem?id=2965 poj 1753扩展,dfs+枚举,不过加了一个路径. The Pilots Brothers' refrigerator Time Li ...

  3. ACM POJ 2965 The Pilots Brothers' refrigerator

    http://poj.org/problem?id=2965 The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65 ...

  4. Poj 2965 The Pilots Brothers‘ refrigerator

    The game "The Pilots Brothers: following the stripy elephant" has a quest where a player n ...

  5. POJ - 2965 The Pilots Brothers' refrigerator(bfs+路径输出/思维+位运算)

    题目链接:点击查看 题目大意:给出一个4*4的矩阵,每个点都代表一个开关,'+'代表关,'-'代表开,每次操作可以任意改变一个开关(x,y)的状态,但代价是x行和y列的开关都要一起改变状态,题目要求将 ...

  6. POJ 2965 The Pilots Brothers' refrigerator

    这个题是一个枚举题. 用的是八皇后算法. 具体操作不说了. 下面是代码: #include <stdio.h> struct node {int x,y; }de[200],dr[200] ...

  7. poj 2965 The Pilots Brothers#39; refrigerator

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

  8. B - The Pilots Brothers' refrigerator

    B - The Pilots Brothers' refrigerator 文章目录 B - The Pilots Brothers' refrigerator 题目描述: Input: Output ...

  9. POJ2965 The Pilots Brothers‘ refrigerator

    POJ2965 The Pilots Brothers' refrigerator 题干 Description Input Output Sample Input Sample Output 题意 ...

  10. The Pilots Brothers‘ refrigerator(思维)

    题面:The Pilots Brothers'refrigerator 题目大意 "飞行员兄弟"这个游戏,需要玩家打开一个有着16把手的冰箱. 每个把手有且只有两种状态:打开(−- ...

最新文章

  1. Spring Cloud 中国社区一周年技术沙龙-北京站
  2. 2019-03-18-算法-进化(实现strStr())
  3. php 算法 二进制文件,关于PHP二进制流 逐bit的低位在前算法(详解)_PHP教程
  4. 阿里文娱技术专家战獒: 领域驱动设计详解之What, Why, How?
  5. mysql二进制格式_二进制格式安装 MySQL
  6. 流媒体传输协议详解之---RTSP认证
  7. python reduce函数怎么用的_我如何仅通过使用reduce函数在python中创建单...
  8. 什么是5G?居然有人用漫画把它讲得如此接地气!
  9. 2.2 获取图像感兴趣区域_超火的机器视觉OpenCVSharp学习笔记3——图像形态学处理...
  10. 25_android下文件访问的权限
  11. loadrunner 检查点
  12. Java资源大全中文版(Awesome最新版)(转载)
  13. list集合排序-lambda表达式实现
  14. Tess4J OCR简单使用教程
  15. Office 与 Visio安装冲突
  16. 单片机程序编写常使用的程序架构
  17. 用python爬取网易云音乐评论
  18. java单击按钮实现窗口隐藏
  19. python并行编程 - 线程篇
  20. 保险业的5项CX预测

热门文章

  1. Android进阶之光 读书笔记
  2. 某微信公众号运营数据分析报告
  3. 浙江大学远程教育计算机应用基础第4次,浙江大学远程教育计算机应用基础2013年秋-4.Excel知识题...
  4. 问题:设计一个大学教师和学生管理程序, 教师包括 编号、姓名、职称和教研室 数据的输入输出; 大学生包括编号、姓名、性别、班号、英语、高等数学和数据结构三门课程成绩的输入输出和计算平均分; 研究生包
  5. 一次UDP收不到问题排查
  6. 二十、从句_限定性定语从句
  7. 【无线】【流程】QCA无线驱动收包流程分析
  8. np.random.seed()函数
  9. python个税计算器代码_Python实现的个人所得税计算器示例
  10. 科林明伦杯哈尔滨理工大学第六届程序设计团队赛-Team模拟