poj2965 【枚举】
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+--
----
----
-+--
Sample Output
6
1 1
1 3
1 4
4 1
4 3
4 4
思路:dfs,加个数组保存路径
实现代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<list>
using namespace std;
#define ll long long
#define sd(x) scanf("%d",&x)
#define sdd(x,y) scanf("%d%d",&x,&y)
#define sddd(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define sf(x) scanf("%s",x)
#define ff(i,x,y) for(int i = x;i <= y;i ++)
#define fj(i,x,y) for(int i = x;i >= y;i --)
#define mem(s,x) memset(s,x,sizeof(s));
#define pr(x) printf("%d",x);
const int Mod = 1e9+;
const int inf = 1e9;
const int Max = 1e5+;
void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=;y=;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;}
int gcd(int a,int b) { return (b>)?gcd(b,a%b):a; }
int lcm(int a, int b) { return a*b/gcd(a, b); }
//int mod(int x,int y) {return x-x/y*y;}
char s[];
int mp[][];
int ans = inf,i,j;
int rankx[],ranky[],fx[],fy[];
int check()
{
for(i=;i<;i++){
for(j=;j<;j++){
if(mp[i][j]!=)
return ;
}
}
return ;
} void fan(int x,int y){
for(i=;i<;i++){
mp[x][i] = !mp[x][i];
mp[i][y] = !mp[i][y];
}
mp[x][y] = !mp[x][y];
} int dfs(int x,int y,int t)
{
if(check()){
if(ans>t){
ans = t;
for(i=;i<t;i++){
rankx[i] = fx[i];
ranky[i] = fy[i];
}
}
return ;
}
if(x>=||y>=)
return ;
int nx = (x+)%;
int ny = y+(x+)/;
dfs(nx,ny,t);
fan(x,y); fx[t] = x+;
fy[t] = y+; dfs(nx,ny,t+);
fan(x,y); return ;
}
int main()
{
for(i=;i<;i++){
cin>>s;
for(j=;j<;j++){
if(s[j]=='+')
mp[i][j] = ;
else
mp[i][j] = ;
}
}
dfs(,,);
cout<<ans<<endl;
for(i=;i<ans;i++){
cout<<rankx[i]<<" "<<ranky[i]<<endl;
}
return ;
}
poj2965 【枚举】的更多相关文章
- poj2965枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20398 ...
- 假期训练八(poj-2965递归+枚举,hdu-2149,poj-2368巴什博奕)
题目一(poj-2965):传送门 思路:递归+枚举,遍历每一种情况,然后找出最小步骤的结果,与poj-1753类似. #include<iostream> #include<cst ...
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem? id=2965 ---- ...
- dfs+枚举,flip游戏的拓展POJ2965
POJ 2965 The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: fo ...
- POJ-2965 The Pilots Brothers' refrigerator---思维题
题目链接: https://vjudge.net/problem/POJ-2965 题目大意: 一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,此开关的同一行.同一列 ...
- poj2965 The Pilots Brothers' refrigerator —— 技巧性
题目链接:http://poj.org/problem?id=2965 题解:自己想到的方法是枚举搜索,结果用bfs和dfs写都超时了.网上拿别人的代码试一下只是刚好不超时的,如果自己的代码在某些方面 ...
- Swift enum(枚举)使用范例
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)
建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...
- Objective-C枚举的几种定义方式与使用
假设我们需要表示网络连接状态,可以用下列枚举表示: enum CSConnectionState { CSConnectionStateDisconnected, CSConnectionStateC ...
随机推荐
- Codechef TAPAIR Counting the important pairs 随机化、树上差分
传送门 题意:给出一个$N$个点.$M$条边的无向连通图,求有多少组无序数对$(i,j)$满足:割掉第$i$条边与第$j$条边之后,图变为不连通.$N \leq 10^5 , M \leq 3 \ti ...
- es5中for...in 和es6中 for..of遍历
//定义一个数组 var arr=['A','B','C']; //定义一个对象 var obj={name:'张三',age:20} // for..in 遍历数组 得到索引 for(var x i ...
- Luogu P3953 逛公园
不管怎么说,这都是一道十分神仙的NOIp题 你可以说它狗,但不可以否认它就是NOIp的难度 首先这道题很显然是道图论题还是一道图论三合一(最短路+拓扑+图上DP) 先考虑最短路,我们分别以\(1\)和 ...
- Elasticsearch5.5.1插件开发指南
Elasticsearch5.5.1插件开发指南 原文地址: https://www.elastic.co/guide/en/elasticsearch/plugins/5.5/plugin-auth ...
- [C#]SQLite执行效率优化结论
一.如要使用SQLite,可以从Visual Studio中的“程序包管理器控制台”输入以下命令完成安装: PM> Install-Package System.Data.SQLite.Core ...
- D. Cutting Out
---恢复内容开始--- 链接 [https://codeforces.com/contest/1077/problem/D] 题意 给你n,k,n个数,找出长度为k,的子串(不需连续),使得该子串数 ...
- HDOJ2025_查找最大元素
一道简单题 HDOJ2025_查找最大元素 #include<stdio.h> #include<stdlib.h> #include<ctype.h> #incl ...
- 微软开源的Trill是什么?
微软开源的Trill是什么? https://www.cnblogs.com/CattaC/p/10143445.html 微软开源的Trill是什么? 以下是一篇15年的文章的译文:https:// ...
- jQuery(五)
wrap <script> //wrap:包装 //wrapAll:整体包装 //wrapInner:内部包装 //unwrap:删除包装(删除父级,不包括body) $(function ...
- Oracle11.2.0.1 升级Oracle11.2.0.4 后 listener 的端口改变需要处理
1. 因为Oracle采取了异地升级 发现 Oracle的端口号发生了变化: 2. 如果之前的连接还需要用的话 需要禁用 dbhome1 里面的监听 只打开dbhome2的监听 并且修改 监听的端口号 ...