两个剪枝问题

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. 使用TFA启动需要知道哪些概念?

    1. Boot stage BL1,BL2,BL31,BL32,BL33 2. Exception level EL3, EL1S, EL2 3. 那么放在表格里比较一下咯 Boot stage Ex ...

  2. #软件更新#Visual Studio更新到16.3.8

    #软件更新#Visual Studio更新到16.3.8 此次更新包括以下内容:(1)支持Xcode 11.2.(2)修复无法从System.String类型转化的bug.(3)修复UWP开发中,加载 ...

  3. openresty开发系列29--openresty中发起http请求

    openresty开发系列29--openresty中发起http请求 有些场景是需要nginx在进行请求转发 用户浏览器请求url访问到nginx服务器,但此请求业务需要再次请求其他业务:如用户请求 ...

  4. osg gdal加载tif数据文件

    osg加载.tif地形数据文件 #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <iostream> ...

  5. win10网上邻居看不到别的共享电脑怎么样办

    https://jingyan.baidu.com/article/4853e1e5b714aa1909f72600.html

  6. 报错:Unable to read additional data from client sessionid 0x36ab52d38c20b20, likely client has closed socket

    报错背景: CDH集群中,将kafka和Flume整合,将kafka的数据发送给Flume消费. 启动kafka的时候正常,但是启动Flume的时候出现了报错现象. 但是我检查了Flume,Flume ...

  7. 【缺少kubernetes权限】 namespaces "xxx" is forbidden: User "xxx" cannot xxx resource "xxx" in API group "xxx" in the namespace "xxx"

    需要添加权限,添加权限方式: https://github.com/argoproj/argo/issues/1068

  8. selenium IDE下载安装(For Chrome and firefox)

    安装好Firefox/cheome之后,接下来就到了正式安装Selenuim IDE的时候了. 步骤一:下载Selenuim IDE       方法一:之前从网上查到很多安装教程,都是从http:/ ...

  9. django2.x报错No module named 'django.core.urlresolvers'

    解决方法如下 from django.urls import reverse 最近从django1.9迁移到django2.0中出现一个意外的报错: No module named 'django.c ...

  10. Django orm练习

    ORM练习题 models生成 from django.db import models # Create your models here. # 书籍管理 class Book(models.Mod ...