九度OJ-1461:Tempter of the bone

2024-08-23 02:48
文章标签 九度 oj 1461 tempter bone

本文主要是介绍九度OJ-1461:Tempter of the bone,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  深度优先搜索+回溯法的题。关于路径存在性的问题,都考虑使用深度优先搜索+回溯法。

Debug记录:

①visit的时候忘记标记visited数组了导致超时

②关于使用scanf()录入%c:cin>>x无论x是啥类型,'\n'  '\t'   ' '(空白字符)不会被提取进x,一定会留在输入流中,当其后的字符被提取时其被丢弃。而对于scanf(“%口”,&x),其他的与cin一样,唯独x是char类型时,空白字符与其他字符一视同仁,也会被成功录入x中。

故在录入诸如“迷宫”类型的字符流(用二维数组存储)时,要避免使用scanf("%c")(输入流中的换行\n很难处理),可使用scanf("%s",buf[i]);整串录入。示例如下:

		for (int i=0;i<n;i++){scanf("%s",buf[i]);}


题目描述:

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.

输入:

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. 

输出:

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

样例输入:
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
样例输出:
NO
YES
提示:

 用scanf读取输入。

#include <cstdio>
#define MAXSIZE 10
using namespace std;
int n,m,t,time;
bool suc;
char buf[MAXSIZE][MAXSIZE];
bool visited[MAXSIZE][MAXSIZE];void DFS(int x,int y){if ( (buf[x][y]!='X'&&0<=x&&x<n&&0<=y&&y<m)&&(visited[x][y]==false)&&suc==false/*这个suc也是为了剪枝,经测试没有此处的这个suc则直接超时*/){//指针非空(墙或者越界则空) 且没访问过 //visit//要访问了visited[x][y]=true;//访问标记 time++;//加完后为此刻时间 if (time==t){//if(buf[x][y]=='D'){suc=true;}}//DFSelse{//time<t时才需要继续DFS  (剪枝) DFS(x-1,y);DFS(x+1,y);DFS(x,y-1);DFS(x,y+1);} //上面已经遍历完此结点的所有子树了 //return(统一出口) //回溯()time--;visited[x][y]=false;return;}
}int main(){while (scanf("%d%d%d",&n,&m,&t),n||m||t){//initiatetime=-1;suc=false;//input & initiate visited[] for (int i=0;i<n;i++){scanf("%s",buf[i]);for (int j=0;j<m;j++){visited[i][j]=false;}}//searchfor (int i=0;i<n;i++){for (int j=0;j<m;j++){if (buf[i][j]=='S'){DFS(i,j);break;}}}//outputprintf("%s",suc?"YES\n":"NO\n");}return true;
} 


这篇关于九度OJ-1461:Tempter of the bone的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098139

相关文章

九度1077(最大序列和)

题目链接:点击打开链接 解题思路: 很经典的一道题。首先考虑一下细节问题,当序列都是0时,显然最后要输出0;当序列都是负数时,显然要输出最大的数。 细节处理完了,就可以回到正常轨道。我们开两个变量,分别保存当前的序列和与之前的最大值,我们更新当前序列和的条件是如果当前序列和是负数的时候,那我们必须更新,否则一定会使最后结果减小。更新过程中还要更新之前最大值即可。 完整代码:

哈理工OJ 2179(深搜)

组合 Time Limit: 1000 MSMemory Limit: 32768 K Total Submit: 7(5 users)Total Accepted: 6(5 users)Rating: Special Judge: No Description 给出一个正整数N,从集合{1,2,3..N} 中找出所有大小为k的子集, 并按照字典序从小到大输出。 Input 第一行是一个整

每日OJ_牛客_求和(递归深搜)

目录 牛客_求和(递归深搜) 解析代码 牛客_求和(递归深搜) 求和_好未来笔试题_牛客网 解析代码         递归中每次累加一个新的数,如果累加和大于等于目标,结束递归。此时如果累加和正好等于目标,则打印组合。向上回退搜索其它组合。此题本身就是一个搜索的过程,找到所有的组合。 #include <iostream>#include <cmath>#in

HDU 1010 Tempter of the Bone (搜索)

OJ题目 : click here ~~ 大概题意 : 迷宫搜索。从起点到终点 ,不能回头 , 问能不能在恰好在T 时刻,准时到达终点。 本题充分体现了剪枝的重要性: 奇偶性剪枝: 可以把maze看成这样:  0 1 0 1 0 1  1 0 1 0 1 0  0 1 0 1 0 1  1 0 1 0 1 0  0 1 0 1 0 1  从为 0 的格子走一步,必然走向为 1 的格子

OJ-0905

题目 示例1: 输入:10 10 56 34 99 1 87 8 99 3 255 6 99 5 255 4 99 7 255 2 99 9 255 213 4输出:99 示例2: 输入:10 10 255 34 0 1 255 8 0 3 255 6 0 5 255 4 0 7 255 2 0 9 255 213 5输出:255 import java.util.

每日OJ_牛客_Emacs计算器(逆波兰表达式)

目录 牛客_Emacs计算器(逆波兰表达式) 解析代码 牛客_Emacs计算器(逆波兰表达式) Emacs计算器__牛客网 解析代码 逆波兰表达式(后缀表达式)求值,需要借助栈,思路: 循环输入,获取逆波兰表达式,然后进行以下补助,直到测试完所有的测试用例: 遇到数字字符串,将该数字字符串转化为数字然后入栈。遇到操作符时,从栈顶取两个数字,然后进行该运算符所对应运算

西北工业大学oj题-兔子生崽

题目描述: 兔子生崽问题。假设一对小兔的成熟期是一个月,即一个月可长成成兔,每对成兔每个月可以生一对小兔,一对新生的小兔从第二个月起就开始生兔子,试问从一对兔子开始繁殖,一年以后可有多少对兔子? 这道题目一眼看过去就是典型的递归问题,代码如下 public class RabbitReproduction {public static void main(String[] args) {in

★ 算法OJ题 ★ 力扣209 - 长度最小的子数组

Ciallo~(∠・ω< )⌒☆ ~ 今天,简将和大家一起做一道滑动窗口算法题--长度最小的子数组~ 目录 一  题目 二  算法解析 解法⼀:暴力求解 解法二:滑动窗口 三  编写算法 一  题目 209. 长度最小的子数组 - 力扣(LeetCode) 二  算法解析 解法⼀:暴力求解 算法思路: 从前往后枚举数组中的任意⼀个元素,把它当成起始位置

OJ-0903

题目 示例1 输入:30 12 25 8 19输出:15 示例2 输入:10 12 25 8 19 8 6 4 17 19 20 30输出:-1 题解 import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Main {public static

【负载均衡式在线OJ】Compile_server 模块

文章目录 程序源码compile_server整体思路编译(compile.hpp)运行模块编译运行模块编译运行服务 程序源码 https://gitee.com/not-a-stupid-child/online-judge compile_server 整体思路 这个服务要对oj_server 发送过来的代码进行编译和运行,最后把结果返回给oj_server。 所以我