[BFS] HDU 2102
来源:互联网 发布:淘宝店铺升级在哪里 编辑:程序博客网 时间:2024/05/17 07:15
两层迷宫,三维坐标表示
遇到上下两层都是# 的,就把上下两层的这个位置都弄成 墙就行
还有遇到 一层是#一层是墙的。。也直接把俩都弄城墙就行。。。
#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" ); }}
阅读全文
0 0
- HDU 2102 双层bfs
- hdu 2102 BFS
- hdu 2102 bfs
- HDU 2102 BFS
- HDU 2102 BFS
- HDU 2102 BFS
- [BFS] HDU 2102
- 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
- hdu 2102 A计划(bfs)
- hdu 2102 A计划 BFS
- hdu 2102 A计划(bfs)
- 各大公司Java后端开发面试题总结
- SSH密钥远程登陆
- 转:Python中的sorted函数以及operator.itemgetter函数
- [IMX6Q]uboot_v2015.04编译流程分析
- JAVA中ListIterator和Iterator详解与辨析
- [BFS] HDU 2102
- 最大访客数(c/python)
- stackoverflow那些有趣的badges
- 国内云市场的发展前景
- mysql 单表更新和多表更新
- Exception sending context initialized event to listener instance of class org.springframework.web.co
- 平安城市,路不拾遗、夜不闭户的“大同”世界
- 心得
- 初识android HAL