hdoj1010

Tempter of the Bone

奶奶的,这道题用了我一下午的时间,只是调用dfs时用错了一个参数,怎么也没想到会是这样出错。错误的代码竟然能过测试数据和我自己的数据,无语啊。

/***************************************************************\
*Author: 小呼
*Created Time: Sun 11 Jul 2010 05:33:54 PM CST
*File Name: a.cpp
*Description:回溯
\***************************************************************/

//*========================*Head File*========================*\\

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*----------------------*Global Variable*----------------------*/
int
N,M,T,di,dj;
char
maze[
9][9];
bool
escape;
int
dir[
4][2]={{0,-1},{0,1},{1,0},{-1,0}};
//*=======================*Main Program*=======================*//
using namespace std;
void
dfs(int sr,int sc,int t){
if
(sr<=
0||sr>N||sc<=0||sc>M)return;

if
(escape||(sr==di&&sc==dj&&t==
0)){
escape=
1;
return
;
}

int
tem=t-abs(sr-di)-abs(sc-dj);
if
(tem<
0||tem&1)
return
;
maze[sr][sc]=
'X';
for
(int i=
0;i<4;++i){
if
(maze[sr+dir[i][
0]][sc+dir[i][1]]!='X'){
dfs(sr+dir[i][
0],sc+dir[i][1],t-1);
if
(escape)return;
}
}

maze[sr][sc]=
'.';
}


int
main(){
int
sr,sc;
while
(cin>>N>>M>>T){
if
(N==
0&&M==0&&T==0)
break
;
int
wall=
0;
for
(int i=
1;i<=N;++i)
for
(int j=
1;j<=M;++j){
cin>>maze[i][j];
if
(maze[i][j]==
'X')
wall++;
else if
(maze[i][j]==
'S'){
sr=i;sc=j;
}

else if
(maze[i][j]==
'D'){
di=i;dj=j;
}
}

if
(N*M-wall<=T){
cout<<
"NO\n";
continue
;
}

escape=
0;
dfs(sr,sc,T);
if
(escape)
cout<<
"YES\n";
else

cout<<
"NO\n";
}
}



转载于:https://www.cnblogs.com/Open_Source/archive/2010/07/11/1904934.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值