题目地址:http://poj.org/problem?id=2965

 /*
题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+'记为1, '-'记为0
1. 从(1, 1)走到(4, 4),每一点DFS两次(改点反转或不反转);used记录反转的位置
详细解释:http://poj.org/showmessage?message_id=346723
2. 比较巧妙的解法:抓住'+'位置反转,'-'位置相对不反转的特点,从状态上考虑
详细解释:http://poj.org/showmessage?message_id=156561
3. 枚举步骤数(1~16),暴力解法,耗时大
详细解释:http://poj.org/showmessage?message_id=343281
4. 网上还有其他解法:高斯消元,BFS,+位运算等等 注意:反转时十字形中心位置多反转了两次,要再反转一次 我还是DFS写不出来。。。
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int a[][];
int used[][]; bool ok(void)
{
for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j)
if (a[i][j] == ) return false;
} return true;
} void change(int x, int y)
{
for (int i=; i<=; ++i)
{
a[x][i] = a[x][i] ? : ;
a[i][y] = a[i][y] ? : ;
}
a[x][y] = a[x][y] ? : ;
used[x][y] = used[x][y] ? : ;
} bool DFS(int x, int y)
{
if (x == && y == )
{
if (ok ()) return true; change (x, y);
if (ok ()) return true; change (x, y);
return false;
}
int nx, ny;
if (y == ) nx = x + , ny = ;
else nx = x, ny = y + ; if (DFS (nx, ny)) return true; change (x, y);
if (DFS (nx, ny)) return true; change (x, y);
return false;
} void work(void)
{
if (DFS (, ))
{
int ans = ;
for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j)
{
if (used[i][j]) ans++;
}
}
printf ("%d\n", ans);
for (int i=; i<=; ++i)
for (int j=; j<=; ++j)
if (used[i][j]) printf ("%d %d\n", i, j);
}
else printf ("WA\n");
} int main(void) //POJ 2965 The Pilots Brothers' refrigerator
{
//freopen ("B.in", "r", stdin); char ch;
for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j)
{
scanf ("%c", &ch);
a[i][j] = (ch == '-') ? : ;
//printf ("%d ", a[i][j]);
}
getchar (); //puts ("");
} work (); return ;
} /*
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
using namespace std; const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
bool con[5][5];
int a[5][5];
struct NODE
{
int x, y;
}node[MAXN]; void work(void)
{
for (int i=1; i<=4; ++i)
{
for (int j=1; j<=4; ++j)
{
if (a[i][j] == 1)
{
con[i][j] = !con[i][j];
for (int k=1; k<=4; ++k)
{
con[i][k] = !con[i][k];
con[k][j] = !con[k][j];
}
}
}
}
int ans = 0;
for (int i=1; i<=4; ++i)
{
for (int j=1; j<=4; ++j)
{
if (con[i][j] == true)
{
ans++; node[ans].x = i; node[ans].y = j;
}
}
}
printf ("%d\n", ans);
for (int i=1; i<=ans; ++i)
printf ("%d %d\n", node[i].x, node[i].y);
} int main(void) //POJ 2965 The Pilots Brothers' refrigerator
{
//freopen ("B.in", "r", stdin); char ch;
for (int i=1; i<=4; ++i)
{
for (int j=1; j<=4; ++j)
{
scanf ("%c", &ch);
a[i][j] = (ch == '-') ? 0 : 1;
}
getchar ();
} work (); return 0;
}
*/ /*
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
using namespace std; const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int a[5][5];
struct NODE
{
int x, y;
}node[MAXN];
int k;
bool flag; bool ok(void)
{
for (int i=1; i<=4; ++i)
{
for (int j=1; j<=4; ++j)
if (a[i][j] == 1) return false;
} return true;
} void change(int x, int y)
{
for (int i=1; i<=4; ++i)
{
a[x][i] = !a[x][i];
a[i][y] = !a[i][y];
}
a[x][y] = !a[x][y];
} void DFS(int x, int y, int num, int cnt, int kk)
{
if (num == cnt)
{
flag = ok ();
k = kk;
return ;
}
for (int i=x; i<=4; ++i)
{
int j;
if (i == x) j = y + 1;
else j = 1;
for (; j<=4; ++j)
{
node[kk].x = i;
node[kk].y = j;
change (i, j);
DFS (i, j, num+1, cnt, kk+1);
if (flag) return ;
change (i, j);
}
}
} void work(void)
{
int cnt;
for (cnt=1; cnt<=16; ++cnt)
{
flag = false; k = 0;
DFS (1, 0, 0, cnt, 1);
if (flag) break;
} printf ("%d\n", cnt);
for (int i=1; i<=k - 1; ++i)
printf ("%d %d\n", node[i].x, node[i].y);
} int main(void) //POJ 2965 The Pilots Brothers' refrigerator
{
//freopen ("B.in", "r", stdin); char ch;
for (int i=1; i<=4; ++i)
{
for (int j=1; j<=4; ++j)
{
scanf ("%c", &ch);
a[i][j] = (ch == '-') ? 0 : 1;
}
getchar ();
} work (); return 0;
}
*/

枚举 POJ 2965 The Pilots Brothers' refrigerator的更多相关文章

  1. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

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

  2. POJ 2965 The Pilots Brothers' refrigerator 位运算枚举

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

  3. poj 2965 The Pilots Brothers' refrigerator (dfs)

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

  4. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

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

  5. POJ 2965 The Pilots Brothers' refrigerator (DFS)

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

  6. POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)

    The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...

  7. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

  8. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  9. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

随机推荐

  1. XSS的DOS攻击之 server limit dos

    墨西哥同学周末很郁闷的在宾馆上网,发现youtube被ban了,于是写个了tool解决这个问题.顺带想到了一种利用 google 统计的漏洞,写在这里了 http://sirdarckcat.blog ...

  2. Android 遍历界面控件

    //遍历界面上的控件 fubin.pan LinearLayout sLinerLayout = (LinearLayout)findViewById(R.id.layout_scr); for (i ...

  3. mongo数据库的导入导出

    http://www.iwangzheng.com/ [root@a02]$show dbs; changhong_tv_cms 0.078GB [root@a02]$ mongodump -d ch ...

  4. hiho #1288 微软2016.4校招笔试题 Font Size

    #1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...

  5. opencv中,c和c++版本区别体验

    参考:http://www.cnblogs.com/tornadomeet/archive/2012/04/29/2476277.html

  6. linux zookeeper 原理详解

    http://cailin.iteye.com/blog/2014486  直接打开此链接即可 --------------------------------------------------   ...

  7. linux /etc/rc.d/目录的详解

    rc.d的内容如下: init.d/ :各种服务器和程序的二进制文件存放目录. rcx.d/: 各个启动级别的执行程序连接目录.里头的东西都是指向init.d/的一些软连接.具体的后边叙述. 还有三个 ...

  8. 15 day 1代碼

    第一题 用堆维护. #include <cstdio> #include <algorithm> #include <queue> int n,i,f[400000 ...

  9. 已知局域网IP地址,如何查看mac

    arp -a 加对方IP是查对方的MAC地址 转自: http://zhidao.baidu.com/link?url=8sRdpGcjfGQ-C1F9zNub49Mxe3DAR-RCAHDkHvKC ...

  10. Excel Sheet Column Title & Excel Sheet Column Number

    Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...