poj.2965
来源:互联网 发布:qt高级编程 pdf 编辑:程序博客网 时间:2024/05/22 14:06
这道题与poj1753基本上一致,核心问题就是矩阵的每一个点最多只能主动翻转一次,并且顺序不影响结果,那么总共就有2^16中可能了,不同点是需要记录过程中的翻转点,这是问题的关键,只需采用一个结构体数组即可,个数为16(注意体会),其他的就基本上和poj1753一样了,由于后面的情况会影响到前面的数组所以需要重新创建数组来记录新的变化。下面是代码:
#include <stdio.h> #include <stdlib.h> typedef struct{int row;int col;}node;int s[4][4];void Init(){ char ch[4][4]; int i,j; for(i=0;i<4;i++) scanf("%s",ch[i]); for(i=0;i<4;i++) for(j=0;j<4;j++){ if(ch[i][j]=='-') s[i][j]=1; else s[i][j]=-1; } } bool Is_right(int (*t)[4]){ int i,j; for(i=0;i<4;i++) for(j=0;j<4;j++) if(t[i][j]!=1) return false; return true; } bool dfs(int (*t)[4],node *next,int x,int y,int deep){ if(Is_right(t)){ printf("%d\n",deep);for(int i=0;i<deep;i++)printf("%d %d\n",next[i].row,next[i].col); return true; } if(x>=4) return false; int record[4][4],i,j; for(i=0;i<4;i++) for(j=0;j<4;j++) record[i][j]=t[i][j]; for(i=0;i<4;i++){record[x][i]=-record[x][i];record[i][y]=-record[i][y];}record[x][y]=-record[x][y];/*node p[16];for(i=0;i<deep;i++)p[i]=next[i]; p[deep].row=x+1;p[deep].col=y+1;*/ if(y<=2){ if(dfs(t,next,x,y+1,deep))return true; /*if(dfs(record,p,x,y+1,deep+1))return true;*/next[deep].row=x+1;next[deep].col=y+1;if(dfs(record,next,x,y+1,deep+1))return true; } else{ if(dfs(t,next,x+1,0,deep)) return true; /* if(dfs(record,p,x+1,0,deep+1)) return true;*/next[deep].row=x+1;next[deep].col=y+1;if(dfs(record,next,x+1,0,deep+1))return true; } return false;} int main() { Init(); node temp[16]; dfs(s,temp,0,0,0); return 0; }
- POJ 2965
- POJ 2965
- poj-2965
- poj 2965
- poj 2965
- poj-2965
- POJ 2965
- POJ 2965
- poj.2965
- poj 2965
- poj 2965
- POJ 2965
- poj - 2965
- poj 2965
- POJ 2965
- poj 2965
- poj 2965
- POJ 2965
- nginx中配置域名跳转示例
- android:targetSdkVersion相关
- ARC最佳实践
- 【玩转Mac OS】02. Mac上设置iTerm2可以显示和输入中文
- 主席树学习笔记
- poj.2965
- apt-get、aptitude和synaptic软件管理工具比较
- Jvisualvm 远程连接java程序 分析内存
- Oracle11g登录密码不区分大小写
- C#事件与委托简单实现
- NET软件工程与项目案例教程.刘光洁(奋斗的小鸟)_PDF 电子书
- 构造DLGTEMPLATE
- jquery实现页面显示实时时间
- 【玩转Mac OS】03. Mac上给iTerm2中的vim上点颜色