hdu1010-深搜+奇偶剪枝-Tempter of the Bone

在迷宫中寻找生存之路,狗狗面临重重挑战。文章详细介绍了迷宫的构造,狗狗如何通过策略和时间计算成功逃脱。面对复杂的路径选择和时间限制,狗狗展现出了非凡的智慧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<div class="plm" style="text-align: center; font-size: 12pt; color: rgb(34, 34, 34); clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;">Tempter of the Bone</div></div><div style="color: rgb(34, 34, 34); font-family: Verdana, Arial, sans-serif; font-size: 14.3999996185303px; width: 960px; margin: auto;"><div class="hiddable" id="vj_description" style="font-family: 'times new roman'; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p></div></div>
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
第一次用深搜写超时了,后来知道用奇偶剪枝,结果交上去wa了,,后来再搜了别人的代码,原来是存地图时用了%c,用%s就对了,其实用%c也行,是getchar()加错了地方。。具体见代码吧
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;
int n,m,T,flag;
int vis[10][10];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct Info
{
    char c;
    int x,y,time;
}s[10][10];
void dfs(Info t)
{
    int i,xx,yy;
    if(t.c=='D')
    {
        if(t.time==T)
        {
            flag=1;
            printf("YES\n");
        }
        return;
    }
    for(i=0;i<4;i++)
    {
        xx=t.x+dir[i][0];
        yy=t.y+dir[i][1];
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy].c!='X'&&vis[xx][yy]==0)
        {
            vis[xx][yy]=1;
            s[xx][yy].time=t.time+1;
            dfs(s[xx][yy]);
            if(flag==1)
                return;
            vis[xx][yy]=0;
        }
    }
}
int main()
{
    int i,j,x1,y1,x2,y2,cnt,ans;
    char cc[10][10];
    while(scanf("%d%d%d",&n,&m,&T)!=EOF&&(n||m||T))
    {
        flag=0;cnt=0;
        memset(vis,0,sizeof(vis));
        //getchar();
        for(i=0;i<n;i++)
        {
            scanf("%s",cc[i]);
            for(j=0;j<m;j++)
            {
                //scanf("%c",&s[i][j].c);
                //getchar();
                s[i][j].c=cc[i][j];
                s[i][j].x=i;
                s[i][j].y=j;
                s[i][j].time=0;
                if(s[i][j].c=='S')
                {
                    x1=i;
                    y1=j;
                }
                else if(s[i][j].c=='D')
                {
                    x2=i;
                    y2=j;
                }
            }
        }
        ans=fabs(x1-x2)+fabs(y1-y2);
        if((T-ans)%2!=0)
        {
            printf("NO\n");
            continue;
        }
        vis[x1][y1]=1;
        dfs(s[x1][y1]);
        if(flag==0)
        {
            printf("NO\n");
        }

    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值