当前位置: 首页 > 工具软件 > Bone > 使用案例 >

Tempter of the bone

胡桐
2023-12-01

题目

题目描述:
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读取输入。


思路

  1. dfs+剪枝
  2. 一定要用scanf("%s")接收输入,我这里wa了n次

AC代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct point {
    int x, y, time;
} point;
 
#define MAXN 40
 
char matrix[MAXN][MAXN];
int mark[MAXN][MAXN];
int success;
 
int dir[4][2] = 
{
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0}
};
 
int overmap(int x, int y, int n, int m)
{
    if (x >= 0 && x < n && y >= 0 && y < m) {
        return 1;       
    } else {
        return 0;
    }
}
 
void dfs(point st, point ed, int n, int m, int t)
{
    int i, distance, tmp;
    point new;
 
    if (success) {
        return;
    }
 
    if (st.x == ed.x && st.y == ed.y && st.time == t) {
        success = 1;
        return;
    }
     
    distance = abs(ed.x - st.x) + abs(ed.y - st.y);
    tmp = t - st.time - distance;
    if (tmp < 0 || tmp % 2) {
        return;
    }
 
    for (i = 0; i < 4; i ++) {
        new.x = st.x + dir[i][0];
        new.y = st.y + dir[i][1];
        new.time = st.time + 1;
 
        if (overmap(new.x, new.y, n, m) && new.time <= t && matrix[new.x][new.y] != 'X' && mark[new.x][new.y] == 0) {
            mark[new.x][new.y] = 1;
            dfs(new, ed, n, m, t);
            mark[new.x][new.y] = 0;
        }
    }
    return;
}
 
int main(void)
{
    int i, j, n, m, t, len;
    point st, ed;
 
    while (scanf("%d %d %d", &n, &m, &t) != EOF) {
        // 终止条件
        if (n == 0 && m == 0 && t == 0)
            break;
 
        // 接收变量
        for (i = 0; i < n; i ++) {
            scanf("%s", matrix[i]);
            for (j = 0, len = strlen(matrix[i]); j < len; j ++) {
                if (matrix[i][j] == 'S') {
                    st.x = i;
                    st.y = j;
                    st.time = 0;
                } else if (matrix[i][j] == 'D') {
                    ed.x = i;
                    ed.y = j;
                }
            }
        }
 
        // dfs遍历
        memset(mark, 0, sizeof(mark));
        mark[st.x][st.y] = 1;
        success = 0;
        dfs(st, ed, n, m, t);
 
        if (success)    
            printf("YES\n");
        else
            printf("NO\n");
    }
 
    return 0;
}
 
/**************************************************************
    Problem: 1461
    User: wangzhengyi
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:920 kb
****************************************************************/


 类似资料:

相关阅读

相关文章

相关问答