HDU 1010 Tempter of the Bone (dfs)

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,t;
int sx,sy;
int ex,ey;
int ok;
char mat[10][10];
int vis[10][10];
int op[4][2]={1,0, -1,0, 0,1, 0,-1};

bool isok1(int x,int y,int step)
{
    //printf("\n\n%d %d %d\n",x,y,step);
    if(ok==1) return false;
    if(x<0||x>=n||y<0||y>=m)  return false;
    if(vis[x][y]==1||mat[x][y]==‘X‘) return false;
    if(mat[x][y]==‘D‘&&step!=t) return false;
    int mlen= abs(x-ex)+abs(y-ey);
    int alen=t-step; //ans len
    if(alen<0||alen%2!=mlen%2) return false;
    return true;
}
void dfs(int x,int y,int step)
{
   if(ok==1||step>t) return ;
   if(mat[x][y]==‘D‘&&step==t)
   {
       ok=1;
       return;
   }
   int i;
   for(i=0;i<4;i++)
   {
       int tx=x+op[i][0];
       int ty=y+op[i][1];
       int tstep=step+1;
       //printf("%d %d %d\n",tx,ty,tstep);
       if(isok1(tx,ty,tstep))
       {
           vis[tx][ty]=1;
           dfs(tx,ty,tstep);
           vis[tx][ty]=0;
       }
   }
}
int main()
{
    int i,j,k;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        if(n==0&&m==0&&t==0) break;
        ok=0;
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
        {
            scanf("%s",mat[i]);
            for(j=0;j<m;j++)
            {
                if(mat[i][j]==‘S‘)
                {
                    sx=i;sy=j;
                }
                else if(mat[i][j]==‘D‘)
                {
                    ex=i;ey=j;
                }
            }
        }
        if(isok1(sx,sy,0))
        {
            vis[sx][sy]=1;
            dfs(sx,sy,0);
        }
        if(ok==0)
        {
            printf("NO\n");
        }
        else printf("YES\n");
    }
    return 0;
}
时间: 2024-11-01 05:43:22

HDU 1010 Tempter of the Bone (dfs)的相关文章

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): 90990    Accepted Submission(s): 24752 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): 68206    Accepted Submission(s): 18719 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

hdu 1010 Tempter of the Bone(dfs暴力)

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 despe

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(深度+剪枝)

http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性剪枝: 在这道题目中,如果使用剪枝的话,可以节省不少的时间. 在这道题目中,每次dfs循环时都可以判断一下小狗当前位置与终点所相差的步数,如果不为偶数的话,说明到达不了终点,就可以退出这个循环,不必继续dfs了. 在这道题目中,由于每个格子只能经过一次,所以经过一次后,可以把该点位置改为'X',然后

【解题报告】Tempter of the Bone(DFS)

解题报告——Tempter of the Bon 问题描述 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name: Main The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to

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

【深搜加剪枝五】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