枚举 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的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
随机推荐
- springmvc 定时器
CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , - * ...
- Unable to execute dex: Multiple dex files define
这是一个编译错误,在ADT的编译器和SDK的工具有差异或是版本不一致时常会出现的一个问题,解决的方案如下: 第一步: updated eclipse (Help->Check for updat ...
- SGU 170 Particles(规律题)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=170 解题报告:输入两个由'+'和'-'组成的字符串,让你判断第二个串能不能由第一个 ...
- Android通过URL加载网络图片
public static Bitmap getBitmap(String path) throws IOException { URL url = new URL(path); HttpURLCon ...
- 【OpenStack】OpenStack系列3之Swift详解
Swift安装部署(与keystone依赖包有冲突,需要安装不同版本eventlet) 参考:http://www.server110.com/openstack/201402/6662.html h ...
- vi命令的基础知识
vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,先说说一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相同的,因此您 ...
- com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...
- 使用 TRegistry 类[1]: 显示各主键下的项
使用 TRegistry 类[1]: 显示各主键下的项 {XP 注册表中的主键} HKEY_CLASSES_ROOT {文件类型信息} HKEY_CURRENT_USER {当前用户信息} ...
- Java for LeetCode 149 Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 中石油—2的幂次方(power)
问题 E: 2的幂次方(power) 时间限制: 1 Sec 内存限制: 64 MB提交: 38 解决: 19[提交][状态][讨论版] 题目描述 任何一个正整数都可以用2的幂次方表示.例如:13 ...