背景:和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相关推荐

  1. POJ-2965-The Pilots Brothers\' refrigerator

    Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24651 Accepted: 9504 Special Judge Descri ...

  2. POJ 2965.The Pilots Brothers‘ refrigerator

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

  3. poj 2965 The Pilots Brothers' refrigerator

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

  4. ACM POJ 2965 The Pilots Brothers' refrigerator

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

  5. B - The Pilots Brothers' refrigerator

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

  6. POJ2965 The Pilots Brothers‘ refrigerator

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

  7. The Pilots Brothers‘ refrigerator(思维)

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

  8. POJ-2965 The Pilots Brothers' refrigerator

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

  9. The Pilots Brothers' refrigerator - poj 2965

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20325   Accepted: 7830   Special Judge ...

最新文章

  1. web前端之JavaScript
  2. netstat -an中state含义
  3. 动态控制jQuery easyui datagrid工具栏显示隐藏
  4. VHDL六层电梯控制器及仿真
  5. hades武器第四形态解锁_凯多的第四个技能预告——冰冻!
  6. 6位密码锁的c语言编程,51单片机4*3矩阵键盘6位密码锁c语言程序
  7. 百度echars 插件 横坐标信息倾斜显示
  8. SVN报错:can't open file db/txn-current-lock:permission denied
  9. AttributeError: 'WebDriver' object has no attribute 'switchTo'
  10. 人工智能系统Google开源的TensorFlow官方文档中文版
  11. python.day.04——常用数据类型(一):strList
  12. 【githubboy】比较实用的 Python 翻译工具库,集成了谷歌、必应、有道、百度等多个翻译平台 API
  13. tomcat8.0安装及配置
  14. WPS2000的目录提取(转)
  15. 海尔电视 android,海尔电视遥控器
  16. 18个谷歌搜索技巧,让你受用终身
  17. charles问题?chls.pro/ssl证书安装后还是出现unknown问题?
  18. 【支付】微信小程序 微信支付 uniapp
  19. SpringBoot之模板引擎
  20. 想要搭建自己的腾讯云服务器怎么做

热门文章

  1. 用Requests下载百度一页图片,以下载芳心纵火犯为例
  2. 人机大战硝烟再起:阿尔法狗升级了 柯洁拼了
  3. 对比Google翻译、百度翻译和有道翻译
  4. linux 将sda分区扩容
  5. linux操作系统-----用户与组管理(3)
  6. python与会计数据_Python与财务【上】--数据采集篇
  7. 淘宝小部件在 2021 双十一中的规模化应用
  8. android WIFI连接,解决6.0及以上无法连接问题
  9. 亚马逊测评如何做?亚马逊测评的获得方法?
  10. 下水盖为什么是圆的?