HDU 搜索练习 Tempter of the Bone

Tempter of the Bone

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 21 Accepted Submission(s) : 7

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.<br><br>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.<br>

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:<br><br>‘X‘: a block of wall, which the doggie cannot
enter; <br>‘S‘: the start point of the doggie; <br>‘D‘: the Door;
or<br>‘.‘: an empty block.<br><br>The input is terminated with
three 0‘s. This test case is not to be processed.<br>

Output

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

Sample Input

4 4 5

S.X.

..X.

..XD

....

3 4 5

S.X.

..X.

...D

0 0 0

Sample Output

NO

YES

简单题意:

  给出地图,问在规定的步数能否正好到达目标位置,

思路分析:

  开始的时候不知道,能去掉一部分,剪枝的方法,一直超时, 去掉一部分就可以了,,bfs

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int flag, sx, sy, dx, dy, num;
int n, m, t;
char map[10][10];
int is[10][10];
int Dx[4] = {-1,0,1,0};
int Dy[4] = {0,-1,0,1};
void dfs(int nowx,int nowy,int num)
{
    int nextx, nexty;
    if(flag == 1)
        return;
    if(nowx == dx && nowy == dy && num == t)
    {
        flag = 1;
        return;
    }
    int min = abs(nowx - dx) + abs(nowy - dy);
    if(min > t - num || (min + t - num ) % 2 != 0)
        return;
    for(int i = 0; i < 4; i++)
    {
        nextx = nowx + Dx[i];
        nexty = nowy + Dy[i];

        if(nextx >= 0 && nextx < m && nexty >= 0 && nexty < n && !is[nextx][nexty] && map[nextx][nexty] != ‘X‘)
        {
            is[nextx][nexty] = 1;
            dfs(nextx, nexty, num + 1);
            is[nextx][nexty] = 0;
        }
    }
}
int main()
{
    //freopen("aaa.txt", "r", stdin);
    while(scanf("%d %d %d", &m, &n, &t))
    {
        if(n ==0 && m == 0 && t == 0)
            break;
        int d = 0;
        for(int i = 0; i < m; i++)
        {
            scanf("%s", map[i]);
            for(int j = 0; j < n; j++)
            {

                if(map[i][j]==‘S‘)
                {
                    sx = i;
                    sy = j;
                }
                if(map[i][j] == ‘D‘)
                {
                    dx = i;
                    dy = j;
                }
                if(map[i][j] == ‘X‘)
                    d++;
            }
        }
        if(n * m - num - 1 < t)
        {
            printf("NO\n");
            continue;
        }

        flag = 0;
        memset(is, 0, sizeof(is));
        is[sx][sy] = 1;
        dfs(sx, sy, 0);
        if(flag)
           printf("YES\n");
        else
           printf("NO\n");

    }

    return 0;
}
时间: 2024-09-30 00:48:06

HDU 搜索练习 Tempter of the Bone的相关文章

杭电 HDU ACM 1046 Tempter of the Bone

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 83458    Accepted Submission(s): 22740 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

【HDU - 1010】Tempter of the Bone(dfs+剪枝)

Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱!他们想尽一切办法逃出去.迷宫是一个大小为 N*M 的长方形,迷宫中有一扇门.一开始,门是关着的,他会在第 t 秒的时间打开.因为,小明和朋友必须在第 t 秒到大门口.每一秒,他都可以向上下左右四个方向移动一个点.一旦他移动了,他刚才所在的点就消失,(这意味着他不能回到他已经走过的点).他不能在一个

HDU ACM 1010 Tempter of the Bone -&gt;简单搜索

分析:搜索题,注意剪枝. #include<iostream> using namespace std; int dfs(int si,int sj,int ei,int ej,int mt); char map[8][8]; int m,n,fa; int main() { int t,i,j,wall,sti,stj,eni,enj; while(cin>>n>>m>>t &&(n||m||t)) { wall=0; fa=0; for(

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 125945    Accepted Submission(s): 33969 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70665    Accepted Submission(s): 19487 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

Tempter of the Bone HDU - 1010

Tempter of the Bone HDU - 1010 dfs. 几个剪枝: 1.如果当前所用时间加上当前位置到目标的曼哈顿距离之和大于目标时间,那么显然无论如何不能完成.剪掉 2.在搜索前判一下,如果出发位置到目标的曼哈顿距离与目标时间的奇偶性不同,那么显然无论如何不能完成.剪掉 错误原因:未加第二个剪枝,TLE 1 #include<cstdio> 2 char s[10][10]; 3 bool ok; 4 int n,m,t,tx,ty,sx,sy; 5 int abs(int

HDU 1010 Tempter of the Bone heuristic 剪枝法

本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢,仅仅能过掉,由此看来很多人没有做好这道题. 这里我需要更正一下网上流行的说法:奇偶剪枝法. 其实本题使用奇偶剪枝法并不能太大提高速度,只能说仅仅让使用奇偶剪枝过掉.所以网上说本题使用奇偶剪枝的,其实并不能提高速度. 原因: 奇偶剪枝只能剪枝一次,不能在递归的时候剪枝,因为只要初始化位置符合奇偶性,那

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c