zoj 2110 Tempter of the Bone(DFS+奇偶剪枝及优化操作)

2023-11-08 12:08

本文主要是介绍zoj 2110 Tempter of the Bone(DFS+奇偶剪枝及优化操作),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1110

2、题目大意:

Tempter of the Bone

           


           

Time Limit: 2 Seconds                                    Memory Limit: 65536 KB                           

           


           

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

 

2、AC代码:

#include<stdio.h>
#include<math.h>
char map[9][9];
int n,m,sx,sy,ex,ey,t;
int dir[4][2]={1,0,-1,0,0,-1,0,1};
int flag;
void DFS(int ssx,int ssy,int cnt)
{//此代码会导致返回的上一结点,不能通过flag=1直接返回
//    if(ssx==ex && ssy==ey && cnt==t)
//    {
//        flag=1;
//        return;
//    }if(ssx==ex && ssy==ey && cnt==t)flag=1;if(flag)return;if(ssx<=0 || ssx>n || ssy<=0 || ssy>m)return;int tmp=t-cnt-fabs(ex-ssx)-fabs(ey-ssy);if(tmp<0 || tmp%2!=0)//奇偶剪枝return;for(int i=0;i<4;i++){int tx=ssx+dir[i][0];int ty=ssy+dir[i][1];if( map[tx][ty]!='X'){map[tx][ty]='X';DFS(tx,ty,cnt+1);map[tx][ty]='.';}}return ;
}
int main()
{while(scanf("%d%d%d",&n,&m,&t)!=EOF){getchar();int wall=0;if(n==0 && m==0 && t==0)break;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){scanf("%c",&map[i][j]);if(map[i][j]=='S'){sx=i;sy=j;}else if(map[i][j]=='D'){ex=i;ey=j;}else if(map[i][j]=='X')wall++;}getchar();}if(n*m-wall<=t)//优化操作,如果能走的个数小,直接返回NOprintf("NO\n");else{flag=0;map[sx][sy]='X';DFS(sx,sy,0);if(flag)printf("YES\n");elseprintf("NO\n");}}return 0;
}


 

这篇关于zoj 2110 Tempter of the Bone(DFS+奇偶剪枝及优化操作)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/369747

相关文章

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

Java Stream.reduce()方法操作实际案例讲解

《JavaStream.reduce()方法操作实际案例讲解》reduce是JavaStreamAPI中的一个核心操作,用于将流中的元素组合起来产生单个结果,:本文主要介绍JavaStream.... 目录一、reduce的基本概念1. 什么是reduce操作2. reduce方法的三种形式二、reduce

MySQL表空间结构详解表空间到段页操作

《MySQL表空间结构详解表空间到段页操作》在MySQL架构和存储引擎专题中介绍了使用不同存储引擎创建表时生成的表空间数据文件,在本章节主要介绍使用InnoDB存储引擎创建表时生成的表空间数据文件,对... 目录️‍一、什么是表空间结构1.1 表空间与表空间文件的关系是什么?️‍二、用户数据在表空间中是怎么

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Mysql数据库中数据的操作CRUD详解

《Mysql数据库中数据的操作CRUD详解》:本文主要介绍Mysql数据库中数据的操作(CRUD),详细描述对Mysql数据库中数据的操作(CRUD),包括插入、修改、删除数据,还有查询数据,包括... 目录一、插入数据(insert)1.插入数据的语法2.注意事项二、修改数据(update)1.语法2.有

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

PyTorch高级特性与性能优化方式

《PyTorch高级特性与性能优化方式》:本文主要介绍PyTorch高级特性与性能优化方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、自动化机制1.自动微分机制2.动态计算图二、性能优化1.内存管理2.GPU加速3.多GPU训练三、分布式训练1.分布式数据

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

Java实现MinIO文件上传的加解密操作

《Java实现MinIO文件上传的加解密操作》在云存储场景中,数据安全是核心需求之一,MinIO作为高性能对象存储服务,支持通过客户端加密(CSE)在数据上传前完成加密,下面我们来看看如何通过Java... 目录一、背景与需求二、技术选型与原理1. 加密方案对比2. 核心算法选择三、完整代码实现1. 加密上