hdu1010——Tempter of the Bone
2010-08-22 01:08
246 查看
#include<stdio.h>
#include<string.h>
#include<math.h>
#define maxv 10
int
dir[
4
][
2
]={{-
1
,
0
},{
1
,
0
},{
0
,-
1
},{
0
,
1
}};
char
g[
maxv][
maxv];
int
m,
n,
t;
int
escape;
int
scx,
scy;
int
outx,
outy;
int
sum;
void
dfs(
int
x,
int
y,
int
step)
{
int
i,
temp,
sx,
sy;
if
(
x==
outx&&
y==
outy&&
step==
t)
escape=
1
;
if
(
escape)//可以出去,返回!
return
;
if
(
step>
t)/走的步数大于时间限制,返回!
return
;
if
(
sum+
1
<
t-
step)//能走的地方小于必要步数,返回!
return
;
for
(
i=
0
;
i<
4
;
i++)
{
sx=
x+
dir[
i][
0
];
sy=
y+
dir[
i][
1
];
if
(
g[
sx][
sy]!=
'X'
&&
sx>=
0
&&
sy>=
0
&&
sx<
n&&
sy<
m)
{
g[
sx][
sy]=
'X'
;
sum--;
dfs(
sx,
sy,
step+
1
);
g[
sx][
sy]=
'.'
;
sum++;
}
}
}
main
()
{
int
i,
j,
k;
while
(
scanf(
"%d%d%d"
,&
n,&
m,&
t)!=
EOF)
{
if
(
n==
0
&&
m==
0
&&
t==
0
)
break
;
else
{
sum=
0
;
for
(
i=
0
;
i<
n;
i++)
scanf(
"%s"
,&
g[
i]);
for
(
i=
0
;
i<
n;
i++)
for
(
j=
0
;
j<
m;
j++)
{
if
(
g[
i][
j]==
'D'
)
{
outx=
i;
outy=
j;
}
else if
(
g[
i][
j]==
'S'
)
{
scx=
i;
scy=
j;
}
else if
(
g[
i][
j]==
'.'
)
sum++;
}
if
(
sum+
1
<
t)
{
printf(
"NO/n"
);
continue
;
}
if
((
abs(
outx-
scx)+
abs(
outy-
scy))%
2
!=
t%
2
)//奇偶数判断!不可少!
{
printf(
"NO/n"
);
continue
;
}
escape=
0
;
g[
scx][
scy]=
'X'
;
dfs(
scx,
scy,
0
);
if
(
escape)
printf(
"YES/n"
);
else
printf(
"NO/n"
);
}
}
}
#include<string.h>
#include<math.h>
#define maxv 10
int
dir[
4
][
2
]={{-
1
,
0
},{
1
,
0
},{
0
,-
1
},{
0
,
1
}};
char
g[
maxv][
maxv];
int
m,
n,
t;
int
escape;
int
scx,
scy;
int
outx,
outy;
int
sum;
void
dfs(
int
x,
int
y,
int
step)
{
int
i,
temp,
sx,
sy;
if
(
x==
outx&&
y==
outy&&
step==
t)
escape=
1
;
if
(
escape)//可以出去,返回!
return
;
if
(
step>
t)/走的步数大于时间限制,返回!
return
;
if
(
sum+
1
<
t-
step)//能走的地方小于必要步数,返回!
return
;
for
(
i=
0
;
i<
4
;
i++)
{
sx=
x+
dir[
i][
0
];
sy=
y+
dir[
i][
1
];
if
(
g[
sx][
sy]!=
'X'
&&
sx>=
0
&&
sy>=
0
&&
sx<
n&&
sy<
m)
{
g[
sx][
sy]=
'X'
;
sum--;
dfs(
sx,
sy,
step+
1
);
g[
sx][
sy]=
'.'
;
sum++;
}
}
}
main
()
{
int
i,
j,
k;
while
(
scanf(
"%d%d%d"
,&
n,&
m,&
t)!=
EOF)
{
if
(
n==
0
&&
m==
0
&&
t==
0
)
break
;
else
{
sum=
0
;
for
(
i=
0
;
i<
n;
i++)
scanf(
"%s"
,&
g[
i]);
for
(
i=
0
;
i<
n;
i++)
for
(
j=
0
;
j<
m;
j++)
{
if
(
g[
i][
j]==
'D'
)
{
outx=
i;
outy=
j;
}
else if
(
g[
i][
j]==
'S'
)
{
scx=
i;
scy=
j;
}
else if
(
g[
i][
j]==
'.'
)
sum++;
}
if
(
sum+
1
<
t)
{
printf(
"NO/n"
);
continue
;
}
if
((
abs(
outx-
scx)+
abs(
outy-
scy))%
2
!=
t%
2
)//奇偶数判断!不可少!
{
printf(
"NO/n"
);
continue
;
}
escape=
0
;
g[
scx][
scy]=
'X'
;
dfs(
scx,
scy,
0
);
if
(
escape)
printf(
"YES/n"
);
else
printf(
"NO/n"
);
}
}
}
相关文章推荐
- HDU1010 Tempter of the Bone(深度优先搜索DFS+奇偶性剪枝)
- hdu1010 Tempter of the Bone dfs搜索 剪枝
- hdu1010:Tempter of the Bone
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
- HDU1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone(dfs)
- Tempter of the Bone —HDU1010(DFS+剪枝)
- HDU1010(Tempter of the Bone)深搜
- hdu1010 Tempter of the Bone(DFS+剪枝)
- hdu1010(Tempter of the Bone)深搜+剪枝
- HDU1010 Tempter of the Bone
- hdu1010(Tempter of the Bone)
- Tempter of the Bone(HDU1010)
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
- Tempter of the Bone 回溯法 hdu1010 zoj2110
- hdu1010 Tempter of the Bone(DFS)
- HDU1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone(回溯 + 剪枝)