hdu 1010 非递归实现
2014-07-05 09:39
274 查看
不知道怎么了,前几天有人问我会不会dfs的非递归实现啊,我想想,好像不会啊,然后回头就找了道水dfs练练手,然后我就出各种小问题,最后过了,然后发现时间还比用递归写还多,不用想,肯定是写渣了。好歹过了,晒个代码吧。
http://acm.hdu.edu.cn/showproblem.php?pid=1010
递归实现:
非递归实现:
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 ; }
相关文章推荐
- HDU 4359-Easy Tree DP?-动态规划-递归方法实现
- hdu 1856 More is better(find函数的递归实现比while循环快7.8倍)
- hdu 1532 dinic 递归 与 非递归实现
- HDU-1010-java实现 奇偶剪枝
- hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现
- hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现
- 用递归实现排列组合取数(组合显示 例5选3等)
- 用VB函数Dir实现递归搜索目录
- 树形目录的递归实现(一)数据库+jsp+javabean
- Xslt中递归的实现
- 递归实现十进制转X进制
- 使用"函数递归"实现的树型菜单 (转)
- fibonacci 递归实现
- SQL Server 2005中的递归实现
- 用递归和委托实现通用的树遍历
- 树形目录的递归实现(一)数据库+jsp+javabean
- 非递归实现不重复序列的全排列(三)
- 使用"函数递归"实现的树型菜单
- 生成树的递归实现
- 一个C语言实现不含递归的高效快速排序算法