题目链接:点击查看

题目大意:给出一个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+路径输出/思维+位运算)相关推荐

  1. POJ 2965.The Pilots Brothers‘ refrigerator

    POJ 2965.The Pilots Brothers' refrigerator Ideas 题意:给你4*4的矩阵.每个点有两种状态,+代表关,-代表开.每个点有一个操作就是该点所在行列所有状态 ...

  2. ACM POJ 2965 The Pilots Brothers' refrigerator

    http://poj.org/problem?id=2965 The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65 ...

  3. poj 2965 The Pilots Brothers' refrigerator

    http://poj.org/problem?id=2965 poj 1753扩展,dfs+枚举,不过加了一个路径. The Pilots Brothers' refrigerator Time Li ...

  4. Poj 2965 The Pilots Brothers‘ refrigerator

    The game "The Pilots Brothers: following the stripy elephant" has a quest where a player n ...

  5. POJ 2965 The Pilots Brothers' refrigerator

    这个题是一个枚举题. 用的是八皇后算法. 具体操作不说了. 下面是代码: #include <stdio.h> struct node {int x,y; }de[200],dr[200] ...

  6. poj 2965 The Pilots Brothers#39; refrigerator

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

  7. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj ...

  8. poj 1606 Jugs(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1606 此题和poj ...

  9. B - The Pilots Brothers' refrigerator

    B - The Pilots Brothers' refrigerator 文章目录 B - The Pilots Brothers' refrigerator 题目描述: Input: Output ...

最新文章

  1. [CF125E]MST Company
  2. puppet 基础篇
  3. HDU - 6746 Civilization(贪心+模拟)
  4. 数据结构——绪论以及线性表的顺序表示
  5. php对接钉钉_php实现钉钉业务报警机器人
  6. 使用ajax怎么解决乱码问题,一句话解决AJAX中文乱码问题[推荐]
  7. 线程进程通信和同步方式
  8. 【poj3263】Tallest Cow(差分数组)
  9. 无线传感器网络(一)基于无锚节点的WSN系统设计
  10. 再次回归 IDEA 的部分修改记录...
  11. Word转html实现在线预览
  12. android模拟器克隆app,易语言一键克隆/启动安卓模拟器
  13. Python开发游戏自动化后台脚本
  14. 前沿重器[26] | 预训练模型的领域适配问题
  15. 基于ttcrpy的跨孔CT高斯牛顿算法及python代码分享(2)
  16. centos7的scp命令_Linux scp命令
  17. 梁念坚漫步“云+端”
  18. 数字统计-c语言-求特殊自然数
  19. Fedora Core 6安装手册
  20. lol-----寒冰射手-----艾希

热门文章

  1. MySQL模糊查询—is null关键字
  2. ObjectFactory 的create()方法什么时候被调用?
  3. mybatis-物理翻页
  4. 基于Xml 的IOC 容器-分配注册策略
  5. Spring-Cloud中的统一配置中心
  6. 整合Swagger2文档api
  7. 微服务网关Gateway-PrefixPath讲解
  8. Ant Design入门之开始使用
  9. spring基于注解的声明式事务控制
  10. 使用#传递参数防御SQL注入攻击