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

时间:2023-03-08 19:19:07

题目链接: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[][]={{,,-,},{-,,,}};//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 = ; i < ; i ++){
nextP.x = pos.x + change[][i];
nextP.y = pos.y + change[][i];
if(nextP.x< || nextP.x>=n || nextP.y< || 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+==t){
flag = true;
return;
}
else{
continue;
}
}
visited[nextP.x][nextP.y]=true;
DFS(nextP,time+);
visited[nextP.x][nextP.y]=false;
if(flag==true) return;
} }
int main(){
while(scanf("%d%d%d",&n,&m,&t)!=EOF){ if(==n && ==m && ==t) break;
for(int i = ; i < n ; i++){
scanf("%s",plot[i]);
}
flag = false;
for(int i = ; i < n ; i++){
for(int j = ; 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)% == ((over.x+over.y)%+t%) %){
visited[start.x][start.y]=true;
DFS(start,);
}
flag == true ? printf("YES\n") : printf("NO\n");
}
return ;
}
/**************************************************************
Problem: 1461
User: zpfbuaa
Language: C++
Result: Accepted
Time:10 ms
Memory:1520 kb
****************************************************************/