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

相关文章

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷