POJ - 2965 The Pilots Brothers' refrigerator(bfs+路径输出/思维+位运算)
题目链接:点击查看
题目大意:给出一个4*4的矩阵,每个点都代表一个开关,'+'代表关,'-'代表开,每次操作可以任意改变一个开关(x,y)的状态,但代价是x行和y列的开关都要一起改变状态,题目要求将所有的开关都打开,问最少操作次数以及哪些点需要操作
题目分析:思维不够,暴力来凑,看到这个题目只有4*4=16个格子,也就是一共只有2^16种情况,直接上bfs遍历所有状态就好了,可以用一个数字保存状态矩阵,用二进制优化一下,然后就能无压力的A掉了
但其实这个题目是个简单思维题,因为开关两个状态的互相转换,相当于取异或,对于每个初始时为'+'的开关,在其所在的行和列的操作数之和必须为奇数才能将当前开关变为'-',又因为异或操作,若对于一个相同的点操作两次,相当于没有操作过,因为互相抵消了,所以我们可以对于每个'+'所在的点的行和列记录操作次数,并记得对2取模,最后统计一下有多少个点的权值为1,说明这些点需要翻转一次
代码:
bfs:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=(1<<16)+100;int vis[N];struct Node
{int x,y,state;Node(int X,int Y,int STATE){x=X;y=Y;state=STATE;}Node(){}
}pre[N];void bfs(int st)
{queue<int>q;q.push(st);vis[st]=true;while(q.size()){int cur=q.front();q.pop();if(!cur)return;for(int i=0;i<16;i++){int x=i/4;int y=i%4;int next=cur^(1<<(x*4+y));for(int j=0;j<4;j++){next^=(1<<(x*4+j));next^=(1<<(j*4+y));}if(vis[next])continue;vis[next]=true;pre[next]=Node(x,y,cur);q.push(next);}}
}int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);int st=0;for(int i=0;i<4;i++)//'+':1 '-':0{ char s[10];scanf("%s",s); for(int j=0;j<4;j++){int pos=i*4+j;if(s[j]=='+')st|=(1<<pos);}}bfs(st);stack<pair<int,int> >s;int k=0;while(k!=st){s.push(make_pair(pre[k].x,pre[k].y));k=pre[k].state;}printf("%d\n",s.size());while(s.size()){printf("%d %d\n",s.top().first+1,s.top().second+1);s.pop();}return 0;
}
思维:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;int maze[10][10];char s[10][10];int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);for(int i=1;i<=4;i++)scanf("%s",s[i]+1);for(int i=1;i<=4;i++)for(int j=1;j<=4;j++){if(s[i][j]=='+'){maze[i][j]=(maze[i][j]+1)%2;for(int k=1;k<=4;k++){maze[i][k]=(maze[i][k]+1)%2;maze[k][j]=(maze[k][j]+1)%2;}}}int ans=0;for(int i=1;i<=4;i++)for(int j=1;j<=4;j++)if(maze[i][j])ans++;printf("%d\n",ans);for(int i=1;i<=4;i++)for(int j=1;j<=4;j++)if(maze[i][j])printf("%d %d\n",i,j);return 0;
}
POJ - 2965 The Pilots Brothers' refrigerator(bfs+路径输出/思维+位运算)相关推荐
- POJ 2965.The Pilots Brothers‘ refrigerator
POJ 2965.The Pilots Brothers' refrigerator Ideas 题意:给你4*4的矩阵.每个点有两种状态,+代表关,-代表开.每个点有一个操作就是该点所在行列所有状态 ...
- ACM POJ 2965 The Pilots Brothers' refrigerator
http://poj.org/problem?id=2965 The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65 ...
- poj 2965 The Pilots Brothers' refrigerator
http://poj.org/problem?id=2965 poj 1753扩展,dfs+枚举,不过加了一个路径. The Pilots Brothers' refrigerator Time Li ...
- Poj 2965 The Pilots Brothers‘ refrigerator
The game "The Pilots Brothers: following the stripy elephant" has a quest where a player n ...
- POJ 2965 The Pilots Brothers' refrigerator
这个题是一个枚举题. 用的是八皇后算法. 具体操作不说了. 下面是代码: #include <stdio.h> struct node {int x,y; }de[200],dr[200] ...
- poj 2965 The Pilots Brothers#39; refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj ...
- poj 1606 Jugs(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1606 此题和poj ...
- B - The Pilots Brothers' refrigerator
B - The Pilots Brothers' refrigerator 文章目录 B - The Pilots Brothers' refrigerator 题目描述: Input: Output ...
最新文章
- [CF125E]MST Company
- puppet 基础篇
- HDU - 6746 Civilization(贪心+模拟)
- 数据结构——绪论以及线性表的顺序表示
- php对接钉钉_php实现钉钉业务报警机器人
- 使用ajax怎么解决乱码问题,一句话解决AJAX中文乱码问题[推荐]
- 线程进程通信和同步方式
- 【poj3263】Tallest Cow(差分数组)
- 无线传感器网络(一)基于无锚节点的WSN系统设计
- 再次回归 IDEA 的部分修改记录...
- Word转html实现在线预览
- android模拟器克隆app,易语言一键克隆/启动安卓模拟器
- Python开发游戏自动化后台脚本
- 前沿重器[26] | 预训练模型的领域适配问题
- 基于ttcrpy的跨孔CT高斯牛顿算法及python代码分享(2)
- centos7的scp命令_Linux scp命令
- 梁念坚漫步“云+端”
- 数字统计-c语言-求特殊自然数
- Fedora Core 6安装手册
- lol-----寒冰射手-----艾希