迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大

题目:题目链接

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set> #define FRER() freopen("in.txt", "r", stdin);
#define INF 0x3f3f3f3f using namespace std; const int maxn = + ; int num[maxn], vis[maxn], temp[], maxd, n; bool dfs(int, int); int main()
{
//FRER()
ios::sync_with_stdio();
cin.tie(); temp[] = ;
for(int i = ; i < ; ++ i)
temp[i] = temp[i - ] << ;
while(cin >> n && n) {
memset(vis, , sizeof(vis));
for(maxd = ; ; ++maxd)
if(dfs(, )) break;
cout << maxd << endl;
}
return ;
} bool dfs(int cur, int s) {
if(cur == maxd && s == n) return true;
if(cur > maxd) return false;
if(s * temp[maxd - cur] < n) return false;
num[cur] = s;
vis[s] = ;
for(int i = ; i <= cur; ++i) {
int u = num[i] + s;
if(!vis[u]) {
vis[u] = ;
if(dfs(cur + , u)) return true;
vis[u] = ;
}
u = abs(num[i] - s);
if(!vis[u]) {
vis[u] = ;
if(dfs(cur + , u)) return true;
vis[u] = ;
}
}
vis[s] = ;
return false;
}

Power Calculus UVA - 1374 迭代加深搜索的更多相关文章

  1. 【算法•日更•第三十九期】迭代加深搜索:洛谷SP7579 YOKOF - Power Calculus 题解

    废话不多说,直接上题: SP7579 YOKOF - Power Calculus 题意翻译 (略过没有营养的题干) 题目大意: 给出正整数n,若只能使用乘法或除法,输出使x经过运算(自己乘或除自己, ...

  2. UVA-1374 Power Calculus (迭代加深搜索)

    题目大意:问最少经过几次乘除法可以使x变成xn. 题目分析:迭代加深搜索. 代码如下: # include<iostream> # include<cstdio> # incl ...

  3. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  4. UVA - 11214 Guarding the Chessboard(迭代加深搜索)

    题目: 输入一个n*m的棋盘(n,m<10),某些格子有标记,用最少的皇后守卫(即占据或攻击)所有的标记的格子.输出皇后的个数. 思路: 一开始没有想到用迭代加深搜索,直接dfs结果还没写完就发 ...

  5. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

    迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...

  6. UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

    解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...

  7. UVA 11212 Editing a Book [迭代加深搜索IDA*]

    11212 Editing a Book You have n equal-length paragraphs numbered 1 to n. Now you want to arrange the ...

  8. UVA - 11214 Guarding the Chessboard(守卫棋盘)(迭代加深搜索)

    题意:输入一个n*m棋盘(n,m<10),某些格子有标记.用最少的皇后守卫(即占据或者攻击)所有带标记的格子. 分析:因为不知道放几个皇后可以守卫所有带标记的格子,即回溯法求解时解答树的深度没有 ...

  9. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

随机推荐

  1. 基于vuecli3构建一个快速开发h5 APP的模板

    基于vuecli3构建的一个快速开发h5 APP的模板,集成了高德地图.mint-ui,以及antv-f2可视化框架 vue-cli3安装 查看vue cli版本 vue --version 要求no ...

  2. setTimout( , 0) 详解

    setTimout( , 0) 一.前言 前端工程师们工作久了,一般都会在某些地方看见过这样的代码: setTimeout(function(){ // TODO }, 0); 举个实例,移动端我们经 ...

  3. 【转】"超时时间已到。在操作完成之前超时时间已过或服务器未响应"的解决方法

    方法有以下三种: 1.原因应该在数据访问有问题,可以把连接时间设置长些,在数据库连接字符串,加上Connect Timeout=18000,单位毫秒 2,在web.config中加上以下语句: < ...

  4. Java中protected方法访问权限的问题

    先看Test.java 此时出现上文提到的错误:The method clone from the type Object is not visiuable. 我们已经清楚Object.clone() ...

  5. linux yum安装指定版本mysql

    1.下载mysql rpm包 cd /usr/local/src wget https://dev.mysql.com/get/mysql80-community-release-el7-.noarc ...

  6. 1968: C/C++经典程序训练6---歌德巴赫猜想的证明

    1968: C/C++经典程序训练6---歌德巴赫猜想的证明 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1165  Solved: 499[Submi ...

  7. Form组件的验证流程及扩展(钩子)

    Form组件的验证流程及扩展(钩子) 常用的form class TestForm(Form): t1 = fields.CharField( widget=widgets.Textarea # 输入 ...

  8. c#自定义类型之间的转换(强制类型转换)

    public class ResultModel { public string PlateNumber { get; set; } public int PlateColor { get; set; ...

  9. Bootstrap历练实例:默认的进度条

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. 01_12_JSP简介

    01_12_JSP简介 1. JSP简介 JSP---Java Server Pages 拥有servlet的特性与优点(本身就是一个servlet) 直接在HTML中内嵌JSP代码 JSP程序有JS ...