POJ2965 The Pilots Brothers' refrigerator 枚举

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

 

这道题是在枚举列表里的。

 

刚开始想到的也就是对每一个位置都枚举一次。

 

然后写写写。越写越乱。

 

然后调试调试调试。然后没调试出来。

 

搜了一下解题报告。

 

发现了一种非枚举的解法。

 

链接一下某个博客的地址:http://blog.csdn.net/loveshuang263146/archive/2010/08/26/5839765.aspx

 

然后自己实现了一下。

 

#include <iostream> using namespace std; int main() { int map[4][4]; int i,j,k; char str[6]; memset(map, 0, sizeof(map)); for (i=0; i<4; i++) { scanf("%s", str); for (j=0; j<4; j++) { if (str[j]=='+') { for (k=0; k<4; k++) { map[i][k]++; map[k][j]++; } map[i][j]--; } } } k = 0; for (i=0; i<4; i++) { for (j=0; j<4; j++) { map[i][j] %= 2; if (map[i][j]==1) k++; } } printf("%d/n", k); for (i=0; i<4; i++) { for (j=0; j<4; j++) { if (map[i][j]==1) printf("%d %d/n", i+1, j+1); } } return 0; }

 

其实还有看到一种枚举法,看是大概看懂,但是没有实现。

 

然后反思自己的枚举为什么DEBUG不出来。

 

然后看着想着改着就出来了……汗……

 

不过时间上的差距是可想而知的,还好一次就AC掉,不然可真得折腾死我。

 

#include <iostream> using namespace std; bool open[4][4]; int n = 4; int x[100],y[100],tx[100],ty[100],ct; bool check(bool map[][4]) { int i,j; for (i=0; i<n; i++) { for (j=0; j<n; j++) { if (!map[i][j]) return false; } } return true; } void copy(int t) { int i; for (i=0; i<t; i++) { x[i] = tx[i]; y[i] = ty[i]; } } void change(bool map[][4], int si, int sj) { int i; for (i=0; i<n; i++) { if (map[si][i]) map[si][i] = false; else map[si][i] = true; } for (i=0; i<n; i++) { if (map[i][sj]) map[i][sj] = false; else map[i][sj] = true; } if (map[si][sj]) map[si][sj] = false; else map[si][sj] = true; } void dfs(bool map[][4], int si, int sj, int t) { int i,j; if (check(map)) { if (t<ct) { copy(t); ct = t; } } else { change(map, si, sj); tx[t] = si; ty[t] = sj; t++; if (check(map)) { if (t<ct) { copy(t); ct = t; } return; } if (sj<3) { j = sj+1; i = si; } else if (si<3) { j = 0; i = si+1; } else { change(map, si, sj); return; } dfs(map, i, j, t); change(map, si, sj); t--; dfs(map, i, j, t); } } int main() { int i,j; char str[6]; for (i=0; i<n; i++) { scanf("%s", str); for (j=0; j<n; j++) { if (str[j]=='-') open[i][j]=true; else open[i][j] = false; } } ct = 100; dfs(open,0,0,0); printf("%d/n", ct); for (i=0; i<ct; i++) { printf("%d %d/n", x[i]+1, y[i]+1); } return 0; }

 

破烂而冗长的枚举……

你可能感兴趣的:(2010)