有两点需要注意,第一点就是,题意是指在第T秒时门才打开,并不是越早到门口就能越早出去,而是只要存在指定长的路径即可以出去。
第二点,每次深搜发现答案不符都必须将点重新初始化。
#include<iostream>
#include<utility>
#define INF 10000
using namespace std;
char maze[100][7];
int d[100][7];
int n,m,t,start_i,start_j,end_i,end_j;
int dx[]= {0,1,0,-1},dy[]= {1,0,-1,0};
bool dfs(int i,int j);
int main()
{
    while(cin>>n>>m>>t && n &&m
            && t)
    {
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                cin>>maze[i][j];
                if(maze[i][j]=='S')
                {
                    start_i=i;
                    start_j=j;
                }
                if(maze[i][j]=='D')
                {
                    end_i=i;
                    end_j=j;
                }
            }
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                d[i][j]=INF;
        d[start_i][start_j]=0;
        if(dfs(start_i,start_j))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
bool dfs(int i,int j)
{
    if(i==end_i && j==end_j)
    {
        if(d[i][j]==t)
        {
            d[i][j]=INF;
            return true;
        }
        else
        {
            d[i][j]=INF;
            return false;
        }
    }
    else
    {
        for(int k=0; k<4; k++)
        {
            int nx=dx[k]+i,ny=dy[k]+j;
            if( nx>=0 &&nx < n &&ny>=0 && ny<m && (d[nx][ny]==INF) &&((maze[nx][ny]=='.')||(maze[nx][ny]=='D')))
            {
                    d[nx][ny]=d[i][j]+1;
                    if(dfs(nx,ny))
                    {
                        d[nx][ny]=INF;
                        return true;
                    }
                    else
                        d[nx][ny]=INF;
            }
        }
        d[i][j]=INF;
        return false;
    }
}
 
 
 
 
 
 
 

HDOJ--1010题C++的更多相关文章

  1. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

  2. HDU 4768 Flyer (2013长春网络赛1010题,二分)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. HDU 4664 Triangulation(2013多校6 1010题,博弈)

    Triangulation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. HDOJ 1004题 Let the Balloon Rise strcmp()函数

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  5. HDOJ 1312题Red and Black

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  6. HDOJ 1013题Digital Roots 大数,9余数定理

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  7. hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  9. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  10. HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

随机推荐

  1. Win7+VS2010 环境配置

    最后再次总结一些,Win7下的VS2010总共有三个变量配置: 1. 变量名:path 变量值:D:\Program Files\Microsoft Visual Studio 10.0\VC\bin ...

  2. 如何给img标签指定默认显示的图片?(已解决)

    1. 使用场景 页面上有很多图片,或者图片很大,这都会使加载的时候出现大片空白,影响用户体验. 2. 解决办法 在CSS里给img指定默认显示的图片,以下是代码: { //**** backgroun ...

  3. A - Yet Another Tetris Problem

    A - Yet Another Tetris Problem 思路:判读一堆数字是不是同奇数偶数,写一个函数,循环遍历,然后判断是否同为奇数偶数. 代码: #include<iostream&g ...

  4. CSS的拾遗(1)

    CSS的拾遗(1) 1.padding: (1)定义:在一个声明中设置所有内边距属性 (2)用法: 例子 1:上,右,下,左 padding:10px 5px 15px 20px; 上内边距是 10p ...

  5. 【Word】如何批量导出ppt中的备注

    [Word]如何批量导出ppt中的备注 文件 | 导出 | 创建讲义 | 备注在幻灯片旁 在word中删除左边两列,复制剩下的表格 | 粘贴-只保留文本

  6. django中读取settings中的相关参数

    from django.conf import settings print(settings.IP_LOCAL)

  7. 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的互转 JAVA

    package com.asiabasehk.cgg.util;   /**火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的互转  * Created by macremote on 1 ...

  8. C# 数据结构之嵌套加法、嵌套乘法

    复杂性度量问题 1.大O复杂度:嵌套加法 找出以下代码片段的 Big O 复杂度. using System; namespace Chapter_1 { class Challenge_1 { st ...

  9. Nlog连接密码隐藏

  10. debian11下载软件包及依赖(本地使用)

    记录下实践情况,原文: https://blog.csdn.net/zgp210317/article/details/120586189?spm=1001.2101.3001.6650.2& ...