pOJ 2965 The Pilots Brothers' refrigerator
来源:互联网 发布:mac air散热器 编辑:程序博客网 时间:2024/06/04 19:13
同样是一道枚举题,但是自己过不去,代码的掌控能力不太好。。。
#include<stdio.h>#include<string.h>bool map[5][5];int vis[5][5];int flag;int step;void cha(int x,int y){ for(int i=1; i<5; i++) map[i][y]=!map[i][y]; for(int i=1; i<5; i++) map[x][i]=!map[x][i]; map[x][y]=!map[x][y];}bool pd(){ for(int i=1; i<5; i++) for(int j=1; j<5; j++) if(map[i][j]!=true) return false; return true;}void dfs(int x,int y,int deep){ if(deep==step) { flag=pd(); return ; } if(x>=5) return ; cha(x,y); vis[x][y]=1; if(y<4) dfs(x,y+1,deep+1); else dfs(x+1,1,deep+1); if(flag) return ; cha(x,y); vis[x][y]=0; if(x<4) dfs(x,y+1,deep); else dfs(x+1,1,deep); if(flag) return ;}int main(){ char c; for(int i=1; i<5; i++) { for(int j=1; j<5; j++) { scanf("%c",&c); if(c=='-') map[i][j]=true; else map[i][j]=false; } getchar(); } int i; for(i=1; i<=16; i++) { step=16; memset(vis,0,sizeof(vis)); dfs(1,1,0); if(flag) break; } printf("%d\n",step); for(int i=1; i<5; i++) for(int j=1; j<5; j++) if(!vis[i][j]) printf("%d %d\n",i,j); return 0;}
借鉴了别人的代码,还有一种思路为:
在数据中如果存在一个‘+’ 则所在行、列分别自加一,最后统计奇数的个数即可,便为最后需要翻转个数。。。
代码如下:
#include<iostream>#include<cstring>using namespace std;bool mark[4][4];char s[4][4];int is[16];int js[16];int main(){ memset(mark,0,sizeof(mark)); for(int i=0; i<4; i++) cin>>s[i]; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(s[i][j]=='+') { mark[i][j]=!mark[i][j]; for(int k=0; k<4; k++) { mark[i][k]=!mark[i][k]; mark[k][j]=!mark[k][j]; } } } } int count=0; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(mark[i][j]) { is[count]=i+1; js[count]=j+1; count++; } } } cout<<count<<endl; for(int i=0; i<count; i++) { cout<<is[i]<<" "<<js[i]<<endl; } return 0;};
0 0
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965:The Pilots Brothers' refrigerator
- Poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator(枚举)
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- 验证一个正整数各各位上是否有指定的数字
- 42 Android fragmentManager 获取fragment
- Android中Bitmap、Drawable、byte[]转换
- Access2003和2007/2010中这样执行SQL语句
- cocos2dx 官方示例学习(二), Vector
- pOJ 2965 The Pilots Brothers' refrigerator
- eclipse里找不到android sdk manager图标
- coreutils-stty 初始设置和echo 16进制modBus数据至串口
- 【数据结构】二叉查找树
- struts2 标签比较日期
- 快捷方式的建立-shell 编程
- 变态的libDispatch结构分析-dispatch_object_s
- eclipse里创建android项目时R文件不能自动创建
- memcache安装