算法问题实战策略 PICNIC
2024-09-03 07:34:22
下面是另一道搜索题目的解答过程
题目是《算法问题实战策略》中的一题
oj地址是韩国网站 连接比较慢 https://algospot.com/judge/problem/read/PICNIC
大意如下
输入 输出
也是上来就撸一把DFS
全部能够匹配完成则计数增加1
但是有很多重复计算
我试过记录关系对的时候 以数值大小为序 只能排除一部分重复计算
错误的代码:
#include <iostream>
#include <vector>
#include <algorithm> using namespace std; /*
3
2 1
0 1
4 6
0 1 1 2 2 3 3 0 0 2 1 3
6 10
0 1 0 2 1 2 1 3 1 4 2 3 2 4 3 4 3 5 4 5
//====================================
1
3
4
*/ int t;
int a, b;
int n, m;
typedef pair<int, int> PII; bool isFriend(int i, int j, const vector<PII>& friendv)
{
int minIdx = min(i, j);
int maxIdx = max(i, j); for (auto& e : friendv) {
if (e.first == minIdx && e.second == maxIdx)
return true;
} return false;
} int dfs(bool isChecked[],const vector<PII>& friendv)
{
int firstFree = -;
for (int i = ; i < n; i++) {
if (isChecked[i] == false){
firstFree = i;
break;
}
} if (- == firstFree)
return ; int ret = ; for (int secondFree = firstFree + ; secondFree < n; secondFree++) {
if (firstFree != secondFree && isChecked[firstFree] == false && isChecked[secondFree] == false
&& isFriend(firstFree, secondFree, friendv)) {
isChecked[firstFree] = true; isChecked[secondFree] = true;
ret += dfs(isChecked, friendv);
isChecked[firstFree] = false; isChecked[secondFree] = false;
}
} return ret;
} int main()
{
cin >> t; while (t--) {
cin >> n >> m;
vector<PII> friendv;
bool isChecked[] = {false};
while (m--) {
cin >> a >> b;
friendv.push_back({min(a,b),max(a,b)});
}
sort(friendv.begin(), friendv.end());
cout << dfs(isChecked, friendv);
} return ;
}
经过调试 发现DFS中双重循环有很大问题
i=0时候检测出 0 1配对 然后检测出2 3 配对.
但是当i=2时 也能检测2 3 配对 以及 0 1 配对。
于是做了以下修改,解决重复问题
ac代码
#include <iostream>
#include <vector>
#include <algorithm> using namespace std; /*
3
2 1
0 1
4 6
0 1 1 2 2 3 3 0 0 2 1 3
6 10
0 1 0 2 1 2 1 3 1 4 2 3 2 4 3 4 3 5 4 5
//====================================
1
3
4
*/ int t;
int a, b;
int n, m;
typedef pair<int, int> PII; bool isFriend(int i, int j, const vector<PII>& friendv)
{
int minIdx = min(i, j);
int maxIdx = max(i, j); for (auto& e : friendv) {
if (e.first == minIdx && e.second == maxIdx)
return true;
} return false;
} int dfs(bool isChecked[],const vector<PII>& friendv)
{
int firstFree = -;
for (int i = ; i < n; i++) {
if (isChecked[i] == false){
firstFree = i;
break;
}
} if (- == firstFree)
return ; int ret = ; for (int secondFree = firstFree + ; secondFree < n; secondFree++) {
if (firstFree != secondFree && isChecked[firstFree] == false && isChecked[secondFree] == false
&& isFriend(firstFree, secondFree, friendv)) {
isChecked[firstFree] = true; isChecked[secondFree] = true;
ret += dfs(isChecked, friendv);
isChecked[firstFree] = false; isChecked[secondFree] = false;
}
} return ret;
} int main()
{
cin >> t; while (t--) {
cin >> n >> m;
vector<PII> friendv;
bool isChecked[] = {false};
while (m--) {
cin >> a >> b;
friendv.push_back({min(a,b),max(a,b)});
}
sort(friendv.begin(), friendv.end());
cout << dfs(isChecked, friendv)<<endl;
} return ;
}
最新文章
- Java2_java入门时的一些基本概念的理解(j2ee,j2se,j2me,jdk,sdk,jre,jvm,跨平台)
- 360demo--关于WM_GETMINMAXINFO
- matlab练习程序(Sepia Tone滤镜)
- String数组转List,List转String数组
- 【boost】使用装饰者模式改造boost::thread_group
- Linux基本服务命令
- bzoj 2876: [Noi2012]骑行川藏 拉格朗日数乘
- 使用Idea编写javaweb以及maven的综合(一)
- Cookie 添加,读取,删除
- Android项目外接高德地图代码混淆注意事项
- 优化UI控件 【译】
- java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】
- JSR系列开篇
- php表单提交时获取不到post数据的解决方法
- 剑指Offer-删除链表中重复的结点
- 百度map 3.0初探
- db2字段修改
- 083_Remove Duplicates from Sorted List
- sqlserver——cube:多维数据集
- 一次SQLServer数据库宕机问题
热门文章
- How to: Display a List of Non-Persistent Objects in a Popup Dialog 如何:在弹出对话框中显示非持久化对象列表
- 松软科技web课堂:随机Math.random()
- SVN异常,Previous operation has not finished; run &#39;cleanup&#39; if it was interrupted
- JavaScript高阶函数(Heigher-order function)
- DOMContentLoaded vs jQuery.ready vs onload, How To Decide When Your Code Should Run
- Cesium专栏-填挖方分析(附源码下载)
- CSS 学习手册
- golang.org 安装脚本
- C# SendAysnc 超时
- PDF软件