两个剪枝问题

1. 当两点的距离(需要走的步数)大于剩下的时间时 剪去

2.奇偶剪枝问题

如果起点到终点所需走的步数的奇偶性与时间奇偶性不同的时候 剪去

起点到终点步数的奇偶性的判断

首先 明确点的奇偶性判断 看起横纵坐标和为奇数还是偶数

如果起点和终点的奇偶性相同 则步数为偶数 否则为奇数

具体的代码实现    (start1+start2+end1+end2+time)%2==1  (如果两个数的奇偶性相同的话 两者和对2取余为0)

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int n,m,time;
int flag;//判断是否有符合条件的搜索
int visit[20][20];
char mapp[20][20];
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};//用于四个方向的遍历
void dfs(int x,int y,int t)
{
 int i;
 if(flag==1) return;//找到一个就退回。。 减少时间的消耗
 if(mapp[x][y]=='D')
 {
    if(t==time)
    flag=1;
    return;  //递归的终止条件 得多注意
    }
 if(t>=time) return;
 for(i=0;i<4;i++)
 {
  int xx,yy;
  xx=x+dir[i][0];
  yy=y+dir[i][1];
  if(xx<0||xx>=n||yy<0||yy>=m||mapp[xx][yy]=='X'||visit[xx][yy]==1) continue;
  visit[xx][yy]=1;
  dfs(xx,yy,t+1);//此时的i如果为全局变量的话 这里就对i的值做了改变
  visit[xx][yy]=0;
 }
}

int main()
{
   int start1,start2,i,j,end1,end2;
   while(scanf("%d %d %d",&n,&m,&time)!=EOF)
  {
    if(n==0&&m==0&&time==0) break;
   memset(visit,0,sizeof(visit));
   flag=0;
   for(i=0;i<n;i++)
   {
    for(j=0;j<m;j++)
    {
       cin>>mapp[i][j];
    }
   }
   for(i=0;i<n;i++)//找到初始位置 终止位置 便于剪枝的进行
   {
    for(j=0;j<m;j++)
    {
     if(mapp[i][j]=='S')
     {
      start1=i;
      start2=j;
     } 
     if(mapp[i][j]=='D')
  {
    end1=i;
   end2=j;
  }
    }
   }
   if((abs(start1-end1)+abs(start2-end2))>time||(start1+start2+end1+end2+time)%2==1)//剪枝咯 第一个剪枝为如果剩下的距离大于时间 剪去 第二个剪枝是奇偶减枝
   {
   printf("NO\n");
   continue;
   }
   visit[start1][start2]=1;
   dfs(start1,start2,0);
   if(flag==1) printf("YES\n");
   else printf("NO\n");
 }
 return 0;
}

这次优化主要就是应用了剪枝  加油

dfs的剪枝优化的更多相关文章

  1. hdu 4109 dfs+剪枝优化

    求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己添加的)出发,0到1~n个节点之间的距离为1.mt[i]表示从0点到第i个节点眼下所得的最长路径 #include<iost ...

  2. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

  3. 图解Leetcode组合总和系列——回溯(剪枝优化)+动态规划

    Leetcode组合总和系列--回溯(剪枝优化)+动态规划 组合总和 I 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 ...

  4. HDU2433 最短路 + 剪枝优化

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2433 ,最短路(SPFA或优化过的Dijstra) + 剪枝优化 这道题关键还是在几个剪枝上面,没有剪 ...

  5. hdoj--1010<dfs+奇偶剪枝>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...

  6. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  7. TensorFlow官方发布剪枝优化工具:参数减少80%,精度几乎不变

    去年TensorFlow官方推出了模型优化工具,最多能将模型尺寸减小4倍,运行速度提高3倍. 最近现又有一款新工具加入模型优化"豪华套餐",这就是基于Keras的剪枝优化工具. 训 ...

  8. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  9. 带分数dfs+剪枝优化

    #include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>using name ...

随机推荐

  1. laravel 通过ftp上传的时候报错 Use of undefined constant FTP_BINARY - assumed 'FTP_BINARY

    用Laravel中的filesystems里面的ftp上传文件时报错.在windows上开发,文件上传的时候碰到上面的问题,搜了些资料,发现是php7的ftp拓展默认未开启. filesystems是 ...

  2. RabbitMQ之Fanout交换器模式开发

    Fanout模式,即广播模式,一个发送到交换机的消息会被转发到与该交换机绑定的所有队列上. 一.Provider 配置文件 spring.application.name=provider sprin ...

  3. android -------- java.net.UnknownServiceException

    最近升级了Android的API版本时 ,导致我的网络请求失败了, 出现了这个错误 java.net.UnknownServiceException, 这个错误,我在网上查到这个主要是由于,我们的Ok ...

  4. Spring的Bean的生命周期方法执行顺序测试

    通过一个简单的Maven工程来演示Spring的Bean生命周期函数的执行顺序. 下面是工程的目录结构: 直接贴代码: pom.xml文件内容: <?xml version="1.0& ...

  5. 006-guava 集合-集合工具类-集合扩展工具类

    一.概述 需要实现自己的集合扩展.也许你想要在元素被添加到列表时增加特定的行为,或者你想实现一个Iterable,其底层实际上是遍历数据库查询的结果集 二.使用 2.1.ForwardingList装 ...

  6. Flutter中的Stack、Align、Positioned的使用

    import 'package:flutter/material.dart'; import 'package:flutter_testdemo001/res/listData.dart'; void ...

  7. 全面系统Python3入门+进阶_汇总

    https://coding.imooc.com/class/136.html#Anchor 全面系统Python3入门+进阶-1-1 导学 全面系统Python3入门+进阶-1-2 Python的特 ...

  8. Python动态构造类:借助强悍的内建 type()

    内建的 type() 函数带三个参数时, 将作为强悍的动态类构造器. 如下: type(name, bases, dict) 返回一个新的type对象. 基本上是 class 语句的动态形式. 参数: ...

  9. clientHeight,offsetHeight,scrollHeight迷一样的三个值

    https://blog.csdn.net/qq_39083004/article/details/78498178 https://www.imooc.com/article/17571  推荐 o ...

  10. docker build时改变docker中的apt源

    # Ali apt-get source.list RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \ echo & ...