题目1461:Tempter of the bone(深度优先遍历DFS)

题目链接:http://ac.jobdu.com/problem.php?pid=1461

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1461 Tempter of the bone.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 24/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAX_SIZE 8
 
using namespace std;
 
int n, m, t;
char plot[MAX_SIZE][MAX_SIZE];
bool visited[MAX_SIZE][MAX_SIZE];
 
int change[2][4]={{0,0,-1,1},{-1,1,0,0}};//left right up down
 
struct Pos{
    int x;
    int y;
};
Pos start;
Pos over;
bool flag;
 
void DFS(Pos pos,int time){
    Pos nextP;
    for(int i = 0 ; i < 4 ; i ++){
        nextP.x = pos.x + change[0][i];
        nextP.y = pos.y + change[1][i];
        if(nextP.x<0 || nextP.x>=n || nextP.y<0 || nextP.y>=m) continue;
        if(plot[nextP.x][nextP.y] == 'X' || visited[nextP.x][nextP.y]==true) continue;
        if(plot[nextP.x][nextP.y] == 'D'){
            if(time+1==t){
                flag = true;
                return;
            }
            else{
                continue;
            }
        }
        visited[nextP.x][nextP.y]=true;
        DFS(nextP,time+1);
        visited[nextP.x][nextP.y]=false;
        if(flag==true) return;
    }
     
}
int main(){
    while(scanf("%d%d%d",&n,&m,&t)!=EOF){
         
        if(0==n && 0==m && 0==t) break;
        for(int i = 0 ; i < n ; i++){
            scanf("%s",plot[i]);
        }
        flag = false;
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < m ; j++){
                visited[i][j]=false;
                 
                if(plot[i][j]=='S'){
                    start.x = i;
                    start.y = j;
                }
                if(plot[i][j]=='D'){
                    over.x = i;
                    over.y = j;
                }
            }
        }
        if((start.x+start.y)%2 == ((over.x+over.y)%2+t%2) %2){
            visited[start.x][start.y]=true;
            DFS(start,0);
        }
        flag == true ? printf("YES\n") : printf("NO\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1461
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/

 

转载于:https://www.cnblogs.com/zpfbuaa/p/6756101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值