枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址: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的更多相关文章
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
- 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 ...
- 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 ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
随机推荐
- Spring+Quartz实现定时任务的配置方法
1.Scheduler的配置 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ...
- 第7章 使用RAID与LVM磁盘阵列技术
章节简述: 您好,此章节为新增加的知识内容,正在努力的排版完善,预习2016年9月中旬完成,感谢您的支持,QQ群:340829. 7.1 磁盘冗余阵列 1988年由加利福尼亚大学伯克利分校发表的文章 ...
- Linux SUID SGID 讲解
SUID属性 UNIX的内核是根据什么来确定一个进程对资源的访问权限的呢? 是这个进程的运行用户的(有效)ID,包括user id和group id.用户可以用id命令来查到自己的或其他用户的user ...
- ubuntu14.04安装chrome
到https://www.google.com/chrome/browser/desktop/index.html可下载指定版本的deb文件. 32bit: wget https://dl.googl ...
- JQgrid for asp.net
转载自http://blog.csdn.net/shiworkyue/article/details/8283716 JQgrid for asp.net 网上资料较少,自己总结了些不全,能用到的可 ...
- 《ASP.NET1200例》当前上下文中不存在名称configurationmanager
当前上下文中不存在名称ConfigurationManager的解决方法 今晚做项目在DBHelper.cs类中的数据库连接要改到web.config里面调用,结果在编译的时候却发现提示错误: 当前上 ...
- 百度编辑器ueditor每次编辑后多一个空行的解决办法
用ueditor进行编辑文章时,每次编辑后文章前面都会多出一个空行. <script id="editor" type="text/plain" styl ...
- [编解码] 关于base64编码的原理及实现
转载自: http://www.cnblogs.com/hongru/archive/2012/01/14/2321397.html [Base64]-base64的编码都是按字符串长度,以每3个8b ...
- struts1老古董配置
<!--Struts1 struts-config.xml Demo --><?xml version="1.0" encoding="UTF-8&qu ...
- Code(poj 1850)
大致题意:(与POJ1496基本一致) 输出某个str字符串在字典中的位置,由于字典是从a=1开始的,因此str的位置值就是 在str前面所有字符串的个数 +1 规定输入的字符串必须是升序排列.不降序 ...