Syins写的搜索优化策略-1

学完dfs是不是感觉好像很占内存,而且非常容易超时,好像dfs就适合极小的数据计算似的,简而言之很容易炸。
当然,就简单的dfs而言确实如此,那它有没有什么优化能节约一些内存,减少运行时间呢?
(其实很多优化,在刷题过程中,不知不觉就用了,为了去掉一些不必要的情况)
先来看看这道经典题目:
来自:ZJCPC2004

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 169001 Accepted Submission(s): 44888

Problem Description

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X’: a block of wall, which the doggie cannot enter;
‘S’: the start point of the doggie;
‘D’: the Door; or
‘.’: an empty block.

The input is terminated with three 0’s. This test case is not to be processed.

Output

For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.

Sample Input

4 4 5
S.X.
…X.
…XD

3 4 5
S.X.
…X.
…D
0 0 0

Sample Output

NO
YES
如果你没用一些剔除的条件,那么炸时间是很正常的;
在这里插入图片描述
!!!
写出像这样完全历遍的代码:

#include<iostream>
using namespace std;
int n,m,t,eh,el,f;
char a[8][8];
void dfs(int h,int l,int s);
int main()
{
    int h,l;
    while(cin>>n>>m>>t){
        f=0;
        if(n==0) break;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>a[i][j];
                if(a[i][j]=='S'){
                    h=i;
                    l=j;
                }
                if(a[i][j]=='D'){
                    eh=i;
                    el=j;
                    a[i][j]='.';
                }
            }
        }
        dfs(h,l,0);
    if(f)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    }
    return 0;
}
void dfs(int h,int l,int s)
{
    int i;
    if(s==t){
        if(h==eh&&l==el) f=1;
        return;
    }
    if(h-1>=0&&a[h-1][l]=='.'){//这里向4个方向移动其实可以用一个循环和一个数组实现,这里是因为当时新学的dfs对数据的处理还比较死板(下面的代码也同样如此,因为懒得改);
        a[h-1][l]='X';
        dfs(h-1,l,s+1);
        a[h-1][l]='.';
    }
    if(l-1>=0&&a[h][l-1]=='.'){
        a[h][l-1]='X';
        dfs(h,l-1,s+1);
        a[h][l-1]='.';
    }
    if(h+1<n&&a[h+1][l]=='.'){
        a[h+1][l]='X';
        dfs(h+1,l,s+1);
        a[h+1][l]='.';
    }
    if(l+1<m&&a[h][l+1]=='.'){
        a[h][l+1]='X';
        dfs(h,l+1,s+1);
        a[h][l+1]='.';
    }
    return;
}

相必dfs一炸就会考虑到是不是有一些情况可以去除,因为完全不可能成立;

#include<iostream>
using namespace std;
int n,m,t,eh,el,f;
char a[8][8];
void dfs(int h,int l,int s);
int main()
{
    int h,l;
    while(cin>>n>>m>>t){
        f=0;
        if(n==0) break;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>a[i][j];
                if(a[i][j]=='S'){
                    h=i;
                    l=j;
                }
                if(a[i][j]=='D'){
                    eh=i;
                    el=j;
                    a[i][j]='.';
                }
            }
        }
        if(((((h+l)%2)+((eh+el)%2)))!=(t%2))cout<<"NO"<<endl;//增加部分
        else{
            dfs(h,l,0);
            if(f)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}
void dfs(int h,int l,int s)
{
    int i;
    if(s==t){
        if(h==eh&&l==el) f=1;
        return;
    }
    if((eh-h)+(el-l)+s>t)return;//增加部分(这里其实没有也可以过)
    if(h-1>=0&&a[h-1][l]=='.'){
        a[h-1][l]='X';
        dfs(h-1,l,s+1);
        a[h-1][l]='.';
    }
    if(l-1>=0&&a[h][l-1]=='.'){
        a[h][l-1]='X';
        dfs(h,l-1,s+1);
        a[h][l-1]='.';
    }
    if(h+1<n&&a[h+1][l]=='.'){
        a[h+1][l]='X';
        dfs(h+1,l,s+1);
        a[h+1][l]='.';
    }
    if(l+1<m&&a[h][l+1]=='.'){
        a[h][l+1]='X';
        dfs(h,l+1,s+1);
        a[h][l+1]='.';
    }
    return;
}

然后就发现ac了,这里用到了两个折枝方法
1:前面那串增加代码(奇偶折枝)
这是什么意思?
先看
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
你可以得到从1-1或者0-0所走的步数必然是偶数;
而1-0或0-1必然是奇数;
所以直接计算起点-终点和能走的时间,如果发现不对应,直接砍掉大枝条,输出no;
当然也可以这样:如果,(T-step-s)是奇数,则剪掉(奇偶剪枝)。step为当前步数,s为最短距离,T为时间;
2:如果T < s+step,则剪掉,即为第二段增加代码
3:(路径剪枝)
如果地图中,可走的点的数目(xnum) < 要求的时间T,则剪掉
这个实现起来比较复杂~~(我比较懒)~~ ,所以没有打上去,就这题而言其实用处不是很大。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值