博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2965 The Pilots Brothers' refrigerator
阅读量:4671 次
发布时间:2019-06-09

本文共 1339 字,大约阅读时间需要 4 分钟。

题意:有4×4个开关,每改变一个开关的状态,会同时改变同一行和同一列开关的状态,给出初始状态,求最少需要多少步能把所有开关都变成开,并输出方案。

 

解法:枚举+剪枝。直接暴力枚举竟然T了……觉得不太科学……2^16*16的复杂度而已……只好加了一个剪枝,记录当前已经枚举过的最佳答案,后来就只枚举到最佳答案,如果超过了最佳答案的步数说明肯定不是最佳方案。

 

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long longusing namespace std;int main(){ ios :: sync_with_stdio(false); char maze[4][5]; while(cin >> maze[0]) { for(int i = 1; i < 4; i++) cin >> maze[i]; int maxn = 1 << 16; int ans = 1000000, pos = 0; for(int i = 0; i < maxn; i++) { int res = 0; int cnt[4][4] = {0}; int flag = 1; for(int j = 0; j < 16; j++) { if(i & (1 << j)) { res++; if(res > ans)//剪枝 { flag = 0; break; } for(int k = 0; k < 4; k++) { cnt[j / 4][k]++; cnt[k][j % 4]++; } cnt[j / 4][j % 4]--; } } for(int j = 0; j < 4 && flag; j++) for(int k = 0; k < 4 && flag; k++) { int tmp = maze[j][k] == '+' ? 1 : 0; tmp += cnt[j][k]; if(tmp & 1) flag = 0; } if(flag && (res < ans)) { ans = res; pos = i; } } if(ans != 1000000) cout << ans << endl; else cout << "0" << endl; for(int i = 0; i < 16; i++) { if(pos & (1 << i)) { cout << i / 4 + 1 << ' ' << i % 4 + 1 << endl; } } } return 0;}

  

转载于:https://www.cnblogs.com/Apro/p/4859156.html

你可能感兴趣的文章
编写高质量代码改善C#程序的157个建议——建议50:在Dispose模式中应区别对待托管资源和非托管资源...
查看>>
MySQL安装与操作总结
查看>>
python 中time, datetime的用法
查看>>
python中将函数赋值给变量时需要注意的一些问题
查看>>
SAS数据挖掘实战篇【五】
查看>>
如何成为合格的数据分析师
查看>>
ArcGIS10.5资源分享
查看>>
理解http幂等性
查看>>
grep运用
查看>>
logstash收集syslog日志
查看>>
jenkins修改数据存放路径
查看>>
poj2481树状数组解二维偏序
查看>>
软件工程网络15个人阅读作业1(201521123062 杨钧宇)
查看>>
根据控制点坐标对完成坐标转换
查看>>
Boost.ASIO简要分析-4 多线程
查看>>
java调用支付宝接口代码介绍
查看>>
安装apache 后,找不到服务,解决办法
查看>>
linux下tomcat的安装
查看>>
【洛谷 T47488】 D:希望 (点分治)
查看>>
【洛谷 P1772】 [ZJOI2006]物流运输(Spfa,dp)
查看>>