poj2965
来源:互联网 发布:淘宝网购物女装皮衣 编辑:程序博客网 时间:2024/05/06 16:29
#include <iostream>#include <stdio.h>using namespace std;struct game{ char p[4][4]; char f[4][4]; int n,i,j,up;};game dl[100000],a;char fan(char kaig){ if (kaig=='-') return '+';else return '-';}game flip(game b,int i,int j){ int k; for (k=0;k<=3;k++) b.p[i][k]=fan(b.p[i][k]); for (k=0;k<=3;k++) b.p[k][j]=fan(b.p[k][j]); b.p[i][j]=fan(b.p[i][j]); b.i=i;b.j=j; int m,n; for (m=0;m<=i-1;m++) //按一定顺序翻 翻过位置做标记 for (n=0;n<=3;n++) b.f[m][n]='1'; for (n=0;n<=j;n++) b.f[i][n]='1'; return b;}bool judge(game a){ int i,j; for (i=0;i<=3;i++) for (j=0;j<=3;j++) if (a.p[i][j]!='-') return 0; return 1;}int main(){ int i,j,c1,c2; //freopen("in.txt","r",stdin); for (i=0;i<=3;i++) { scanf("%s",a.p[i]); } for (i=0;i<=3;i++) for (j=0;j<=3;j++) a.f[i][j]='0'; c1=1;c2=1; dl[1]=a;a.n=0;a.up=0; if(judge(a)){ printf("0\n"); return 0; } while (c1<=c2) { for (i=0;i<=3;i++) for (j=0;j<=3;j++) if (dl[c1].f[i][j]=='0') { c2++; dl[c2]=flip(dl[c1],i,j); dl[c2].up=c1; dl[c2].n=(dl[c1].n)+1; if (judge(dl[c2])) { game d; printf("%d\n",dl[c2].n); d=dl[c2]; while (d.n!=0) { printf("%d %d\n",d.i+1,d.j+1); d=dl[d.up]; } break; } } c1++; } return 0;}
题目大意:一个冰箱有16个开关,呈方形分布(4*4矩阵),“+”表示关闭,“-”表示开着,当所有的开关为“+”时冰箱才能打开。当去翻转一个开关时,在该开关所在列和行的所有开关都要翻转,即开变关,关变开。问至少需要多少次才能打开冰箱。
BFS
0 0
- POJ2965
- poj2965
- poj2965
- POJ2965
- poj2965
- poj2965
- poj2965
- POJ2965
- POJ2965
- poj2965
- poj2965
- POJ2965
- POJ2965
- POJ2965
- poj2965
- POJ2965
- poj2965
- poj2965
- Lua脚本中使用Unicode
- MySQL 正则表达式
- PHP mcrypt 加密解密函数
- Android activity的启动模式
- Yahoo!团队:网站性能优化的35条黄金守则
- poj2965
- IOS开发语言Swift入门连载---初见
- PHP curl get post 提交函数
- hdu 1702 ACboy needs your help again!(模拟栈和队列)
- Lua源文件目录分析
- 嵌入式学习笔记004-裸奔篇之SDRAM
- tomcat配置数据库连接池
- javascript常用 正则表达式
- Git学习笔记(五)---修改管理