[BFS] HDU 2102
2017-11-15 17:02
274 查看
两层迷宫,三维坐标表示
遇到上下两层都是# 的,就把上下两层的这个位置都弄成 墙就行
还有遇到 一层是#一层是墙的。。也直接把俩都弄城墙就行。。。
遇到上下两层都是# 的,就把上下两层的这个位置都弄成 墙就行
还有遇到 一层是#一层是墙的。。也直接把俩都弄城墙就行。。。
#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; struct Node { int x, y, z; int cnt; }; int n, m, t; //两层 char maze[ 2 ][ 35 ][ 35 ]; bool vis[ 2 ][ 35 ][ 35 ]; int dir[ 4 ][ 2 ] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool path ( int x, int y, int z ) { if ( x < 0 || x >= n || y < 0 || y >= m || maze[ z ][ x ][ y ] == '*' ) return false; //进不了的入口直接屏蔽掉 if ( maze[ z ][ x ][ y ] == '#' && ( maze[ !z ][ x ][ y ] == '#' || maze[ !z ][ x ][ y ] == '*' ) ) return false; return true; } bool bfs () { Node cur, nex; cur.x = cur.y = cur.z = 0; cur.cnt = 0; vis[ cur.z ][ cur.x ][ cur.y ] = true; queue<Node> Q; Q.push ( cur ); while ( !Q.empty () ) { cur = Q.front (); Q.pop (); //超过时限 if ( cur.cnt > t ) return false; //找到结果 if ( maze[ cur.z ][ cur.x ][ cur.y ] == 'P' ) { return true; } for ( int i = 0; i < 4; ++i ) { nex.x = cur.x + dir[ i ][ 0 ]; nex.y = cur.y + dir[ i ][ 1 ]; nex.z = cur.z; if ( path ( nex.x, nex.y, nex.z ) && !vis[ nex.z ][ nex.x ][ nex.y ] ) { if ( maze[ nex.z ][ nex.x ][ nex.y ] == '#' ) //找到传送站直接换到另一层,不算时间 nex.z = !cur.z; nex.cnt = cur.cnt + 1; vis[ nex.z ][ nex.x ][ nex.y ] = true; Q.push ( nex ); } } } return false; } int main () { int c; scanf ( "%d", &c ); while ( c-- ) { scanf ( "%d%d%d", &n, &m, &t ); memset ( vis, false, sizeof ( vis ) ); for ( int i = 0; i < n; ++i ) scanf ( "%s", maze[ 0 ][ i ] ); for ( int i = 0; i < n; ++i ) scanf ( "%s", maze[ 1 ][ i ] ); bool flag = bfs (); if ( flag ) printf ( "YES\n" ); else printf ( "NO\n" ); } }
相关文章推荐
- HDU 2102 A计划(BFS)
- HDU 2102 A计划(BFS)
- hdu 2102 A计划(bfs)
- HDU 2102 A计划(三维BFS)
- HDU-2102 A计划(BFS)
- hdu 2102 A计划bfs
- HDU-2102 A计划 BFS
- hdu 2102 A计划(BFS)(H)
- HDU 2102 A计划 (三维的迷宫BFS)
- 【HDU 2102】A计划(BFS)
- HDU 2102 A计划【BFS广搜】
- HDU 2102 BFS A计划
- HDU 2102 A计划 bfs
- hdu 2102 A计划 BFS
- HDU 2102 A计划 (BFS或DFS)
- HDU - 2102(bfs)
- A计划 hdu 2102 终于AC,why?? bfs
- HDU 2102 A计划(BFS)
- HDU 2102 A计划 (BFS)
- HDU 2102:A计划【bfs】