您的位置:首页 > 其它

hdu 1010 非递归实现

2014-07-05 09:39 274 查看
不知道怎么了,前几天有人问我会不会dfs的非递归实现啊,我想想,好像不会啊,然后回头就找了道水dfs练练手,然后我就出各种小问题,最后过了,然后发现时间还比用递归写还多,不用想,肯定是写渣了。好歹过了,晒个代码吧。
http://acm.hdu.edu.cn/showproblem.php?pid=1010
递归实现:

#include  <iostream>
#include  <cstdio>
#include  <string>
#include  <stdlib.h>
#include  <memory.h>
using  namespace std ;
int N , M , T , sx , sy , ex , ey , j , wall ;
int  abs ( int x ) { return x > 0 ? x :- x ; }
int step [ 4 ][ 2 ]= {{ - 1 , 0 } , { 1 , 0 } , { 0 , 1 } , { 0 ,- 1 }} ;
bool escape ;
char map [ 9 ][ 9 ];
void  dfs ( int x , int y , int cnt )
{
int i ;
if ( x < 1 || x > N || y < 1 || y > M ) return ;
if ( x == ex && y == ey && cnt == T ) { escape = 1 ; }
if ( escape ) return ;
int temp =( T - cnt )- abs ( ex - x )- abs ( ey - y );
if ( temp < 0 || temp & 1 ) return ;
for ( i = 0 ; i < 4 ; i ++)
{
if ( x + step [ i ][ 0 ]>= 1 && x + step [ i ][ 0 ]<= N && y + step [ i ][ 1 ]>= 1 && y + step [ i ][ 1 ]<= M )
{
if ( map [ x + step [ i ][ 0 ]][ y + step [ i ][ 1 ]]!= 'X' )
{
map [ x + step [ i ][ 0 ]][ y + step [ i ][ 1 ]]= 'X' ;
dfs ( x + step [ i ][ 0 ], y + step [ i ][ 1 ], cnt +1 );
map [ x + step [ i ][ 0 ]][ y + step [ i ][ 1 ]]= '.' ;
}
}
}
return ;
}
int  main ()
{
int i ;
// freopen("in.txt","r",stdin);
while ( scanf ( "%d%d%d" ,& N ,& M ,& T )!= EOF && N && M && T )
{
wall = 0 ; escape = 0 ;
memset ( map , 0 , sizeof ( map ));
for ( i = 1 ; i <= N ; i ++)
{
for ( j = 1 ; j <= M ; j ++)
{
cin >> map [ i ][ j ];
if ( map [ i ][ j ]== 'S' ) { sx = i ; sy = j ; }
else  if ( map [ i ][ j ]== 'D' ) { ex = i ; ey = j ; }
else  if ( map [ i ][ j ]== 'X' ) { wall ++; }
}
}
if ( N * M - wall <= T ) { cout << "NO" << endl ; continue ; }
map [ sx ][ sy ]= 'X' ;
dfs ( sx , sy , 0 );
if ( escape ) cout << "YES" << endl ;
else cout << "NO" << endl ;
}
return  0 ;
}


非递归实现:

#include  <iostream>
#include  <cstdio>
#include  <string>
#include  <stack>
#include  <stdlib.h>
#include  <memory.h>
using  namespace std ;
int N , M , T , sx , sy , ex , ey , wall ;
int  abs ( int x ) { return x > 0 ? x :- x ; }
int step [ 4 ][ 2 ]= {{ - 1 , 0 } , { 1 , 0 } , { 0 , 1 } , { 0 ,- 1 }} ;
bool escape ;
char map [ 9 ][ 9 ];
struct  status {
int posx , posy , step , i ;
} now , put ;
bool  check ( status  now )
{
if ( now . posx >=  1 && now . posx <= N && now . posy >=  1  && now . posy <=  M  && map [ now . posx ][ now . posy ]!= 'X' ) return  true ;
return  false ;
}
int  dfs ( status  now )
{
stack < status > mystatus ;
mystatus . push ( now );
while (! mystatus . empty ())
{
now = mystatus . top ();
mystatus . pop ();
int temp =( T - now . step )- abs ( ex - now . posx )- abs ( ey - now . posy );
if ( now . i ==  4 )
{
map [ now . posx ][ now . posy ]= '.' ;
continue ;
}
if ( temp < 0 || temp & 1 )
{
map [ now . posx ][ now . posy ]= '.' ;
continue ;
}
if ( now . posx == ex && now . posy == ey && now . step == T )
{
escape =  1 ;
break ;
}
while ( now . i < 4 )
{
put . posx = now . posx + step [ now . i ][ 0 ];
put . posy = now . posy + step [ now . i ][ 1 ];
if ( check ( put ))
{
put . step = now . step + 1 ;
put . i =  0 ;
++ now . i ;
map [ put . posx ][ put . posy ]= 'X' ;
mystatus . push ( now );
mystatus . push ( put );
break ;
}
else
{
if (++ now . i ==  4 )
{
map [ now . posx ][ now . posy ]= '.' ;
break ;
}
}
}
}
}
int  main ()
{
// freopen("in.txt","r",stdin);
while ( scanf ( "%d%d%d" ,& N ,& M ,& T )!= EOF && N && M && T )
{
wall = 0 ; escape = 0 ;
memset ( map , 0 , sizeof ( map ));
for ( int i = 1 ; i <= N ; i ++)
{
for ( int j = 1 ; j <= M ; j ++)
{
cin >> map [ i ][ j ];
if ( map [ i ][ j ]== 'S' ) { sx = i ; sy = j ; }
else  if ( map [ i ][ j ]== 'D' ) { ex = i ; ey = j ; }
else  if ( map [ i ][ j ]== 'X' ) { wall ++; }
}
}
if ( N * M - wall <= T ) { cout << "NO" << endl ; continue ; }
map [ sx ][ sy ]= 'X' ;
now =  ( status ) { sx , sy , 0 } ;
dfs ( now );
if ( escape ) cout << "YES" << endl ;
else cout << "NO" << endl ;
}
return  0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息