解题代码

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,a,n) for(int i = a; i <= n; i++)
const int N = 20; int a[N];
int n,ans;
int st[N]; int check(int x){
int y = sqrt(x);
return y * y == x;
} void dfs(int u, int last){
if(u == n){
ans++;
}else{
rep(i,0,n-1){
if(st[i])continue;//如果这个数已经被搜过了,就代表不用搜了
if(i && !st[i - 1] && a[i] == a[i - 1])continue;
//首先比第一个数大,
//然后处于递归的同一层,前一个肯定已经恢复现场了,
//最后判断是否和前一个相等.
if(!check(a[i] + last)) continue;//不满足条件 st[i] = 1;
dfs(u + 1,a[i]);
st[i] = 0;
}
}
} int main(){
cin >> n;
rep(i,0,n - 1){
cin >> a[i];
}
sort(a,a + n);
rep(i,0,n -1){
if(!i|| a[i] != a[i - 1]){
st[i] = 1; dfs(1,a[i]);//因为我们这里已经自己选择了一个数作为第一个元素,所以这里的1代表第二层
//所以递归的出口就是 u == n
st[i] = 0;
}
}
cout << ans << endl;
return 0;
}

dfs去重输出

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,a,n) for(int i = a; i <= n; i++)
const int N = 20; int a[N];
int n,ans;
int st[N];
int path[N];
void dfs(int u, int last){
if(u == n){
rep(i,0,n-1) cout<<path[i]<<" ";
puts("");
}else{
rep(i,0,n-1){
if(st[i])continue;
if(i && !st[i - 1] && a[i] == a[i - 1])continue;
st[i] = 1;
path[u] = a[i];
dfs(u + 1,a[i]);
st[i] = 0;
}
}
}
int main(){
cin >> n;
rep(i,0,n - 1) cin >> a[i];
sort(a,a + n);
rep(i,0,n -1){
if(!i|| a[i] != a[i - 1]){
st[i] = 1;
path[0] = a[i];
dfs(1,a[i]);
st[i] = 0;
}
}
return 0;
}

结果参考

Acwing 正方形数组的数目(dfs去重)的更多相关文章

  1. [Swift]LeetCode996. 正方形数组的数目 | Number of Squareful Arrays

    Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elem ...

  2. php二维数组根据某个字段去重

    php的二维数组根据某个字段去重,在这默认为二维数组的结构是一样的,现在根据二维数组里的id字段去重,把id相同的重复的元素去掉 /** * 二维数组根据某个字段去重 * @param array $ ...

  3. java对一个int数组进行排序、去重

    思路: 1.使用 HashSet 进行去重 2.将 HashSet 变为 TreeSet 3.使用 TreeSet 进行排序 4.将 Set 变为 Integer 数组 5.将 Integer 数组变 ...

  4. poj 1564 Sum It Up (DFS+ 去重+排序)

    http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...

  5. AcWing 228. 异或 (dfs+线性基)打卡

    题目:https://www.acwing.com/problem/content/230/ 题意:有一个图,每条边有一个权值,现在求1-n的一条路径的最大异或和,一条边能经过多次,相应的也要计算那么 ...

  6. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  7. JS 中数组的排序和去重

    在 PHP 中,数组有很多排序方法,不过其他语言的数组中大概是不会像 JS 的数组一样,包罗万象,啥都通吃的.所以 JS 的数组排序情况就略多一些了. 简单粗暴的排序: 赤果果的sort: var   ...

  8. 线性时间常数空间找到数组中数目超过n/5的所有元素

    问题描述: Design an algorithm that, given a list of n elements in an array, finds all the elements that ...

  9. poj 1564 Sum It Up【dfs+去重】

    Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6682   Accepted: 3475 Descrip ...

随机推荐

  1. Unity-2D像素晶格化消融

    效果展示: ShaderLab Shader功能:图像变白+根据顶点的y值作透明裁剪: 才是可操作属性: IsDead: 控制像素变白,片元着色阶段IsDead小于0将颜色改为白色: Percent: ...

  2. IP核的使用(Vivado中的调用,product guide的查询阅读 ,引脚的设置(位宽,个数,算法等),coe文件的初始化 )

    IP核:Intellectual Property core ,即知识产权核.每个IP核可以实现特定功能,我们在设计一个东西时可以直接调用某个IP核来辅助实现功能. 存在形式:HDL语言形式,网表形式 ...

  3. 【新人福利】使用CSDN 官方插件,赠永久免站内广告特权 >>电脑端访问:https://t.csdnimg.cn/PVqS

    [新人福利]使用CSDN 官方插件,赠永久免站内广告特权 >>电脑端访问:CSDN开发助手 [新人福利]使用CSDN 官方插件,赠永久免站内广告特权 >>电脑端访问:https ...

  4. placeholder 设置换行三种方式

    在 html 中编写代码时保留代码换行 <textarea name="" id="" cols="30" rows="10 ...

  5. beego下让swagger按照更新了controllers的接口信息自动更新commentsRouter_controllers.go

    beego下让swagger按照更新了controllers的接口信息自动更新commentsRouter_controllers.go (1)在beego环境中,当更新了controllers目录下 ...

  6. Python 中MATLABspline函数的替代函数

    调用scipy模块,其中有对应的函数UnivariateSpline.与MATLAB中spline函数不同的是,这个函数返回值是一个插值函数,而非插值结果. import scipy spline = ...

  7. BZOJ3224/LuoguP3369 普通平衡树 (splay)

    终末のcode #include <iostream> #include <cstdio> #include <cstring> #include <algo ...

  8. Java源码分析 | Object

    本文基于 OracleJDK 11, HotSpot 虚拟机. Object 定义 Object 类是类层次结构的根.每个类都有 Object 类作为超类.所有对象,包括数组等,都实现了这个类的方法. ...

  9. React报错之React hook 'useState' is called conditionally

    正文从这开始~ 总览 当我们有条件地使用useState钩子时,或者在一个可能有返回值的条件之后,会产生"React hook 'useState' is called conditiona ...

  10. 快速掌握 Base 64 | 学 Java 密码系列

    Java 密码系列 - Java 和 JS Base 64 Base 64 不属于密码技术,仅是编码方式.但由于在 Java.JavaScript.区块链等出现的频率较高,故在本系列文章中首先分享 B ...