标签:style blog http color io os ar for strong
题目链接:http://poj.org/problem?id=3984
思路:
经典型的DFS题目。搜索时注意剪枝:越界处理,不能访问处理。
代码:
#include <iostream> using namespace std; const int MAX_N = 15; int map[MAX_N][MAX_N]; typedef struct Node { int x; int y; }Road; Road vis[MAX_N]; int SearchRoad( int i, int j, int count ) { if ( i == 4 && j == 4 ) { vis[count].x = i; vis[count].y = j; for ( int i = 0; i <= count; ++i ) printf( "(%d, %d)\n", vis[i].x, vis[i].y ); } else if ( i >= 5 || j >= 5 || map[i][j] == 1 ) return 0; else { vis[count].x = i; vis[count].y = j; count++; SearchRoad( i+1, j, count ); SearchRoad( i, j+1, count ); } return 0; } int main() { for ( int i = 0; i < 5; ++i ) for ( int j = 0; j < 5; ++j ) scanf( "%d", &map[i][j] ); SearchRoad( 0, 0, 0 ); return 0; }
标签:style blog http color io os ar for strong
原文地址:http://www.cnblogs.com/tallisHe/p/4020866.html