[BFS] FZU 2150
来源:互联网 发布:淘宝购物车怎么扩容 编辑:程序博客网 时间:2024/06/12 23:09
双入口的bfs
数据量比较小,可以暴力枚举
烧完之后找到时间最长的点
#include <cstdlib>#include <cstring>#include <iostream>#include <queue>#define N 20using namespace std;int mintime[ N ][ N ];int visit[ N ][ N ];char map[ N ][ N ];int dirx[ 4 ] = {1, -1, 0, 0};int diry[ 4 ] = {0, 0, 1, -1};int n, m;bool path ( int x, int y ) { if ( x >= 0 && x < n && y >= 0 && y < m && map[ x ][ y ] == '#' ) return true; return false;}struct Node { int x, y; int cnt;} cur, nex;int bfs ( int x1, int y1, int x2, int y2 ) { Node n1, n2; n1.x = x1, n1.y = y1, n1.cnt = 0; n2.x = x2, n2.y = y2, n2.cnt = 0; // mintime初始化为inf memset ( mintime, 0x7f, sizeof ( mintime ) ); memset ( visit, false, sizeof ( visit ) ); mintime[ x1 ][ y1 ] = 0; mintime[ x2 ][ y2 ] = 0; queue<Node> q; q.push ( n1 ); q.push ( n2 ); int ans = -1; while ( !q.empty () ) { cur = q.front (); q.pop (); for ( int i = 0; i < 4; ++i ) { nex.x = cur.x + dirx[ i ]; nex.y = cur.y + diry[ i ]; nex.cnt = cur.cnt + 1; if ( path ( nex.x, nex.y ) && nex.cnt < mintime[ nex.x ][ nex.y ] ) { mintime[ nex.x ][ nex.y ] = nex.cnt; q.push ( nex ); } } } //找到所有#里最大的,如果有剩余刚好是inf for ( int i = 0; i < n; ++i ) for ( int j = 0; j < m; ++j ) if ( map[ i ][ j ] == '#' ) ans = max ( ans, mintime[ i ][ j ] ); return ans;}int main () { int num; cin >> num; for ( int cs = 1; cs <= num; cs++ ) { cin >> n >> m; for ( int i = 0; i < n; ++i ) cin >> map[ i ]; int ans = 0x3f3f3f; //暴力搜索 for ( int i = 0; i < n; ++i ) for ( int j = 0; j < m; ++j ) if ( map[ i ][ j ] == '#' ) //找到第一个# for ( int ii = 0; ii < n; ++ii ) for ( int jj = 0; jj < m; ++jj ) if ( map[ ii ][ jj ] == '#' ) { //找到第二个# int tmp = bfs ( i, j, ii, jj ); ans = min ( ans, tmp ); } if ( ans == 0x3f3f3f ) ans = -1; cout << "Case " << cs << ": " << ans << endl; } return 0;}
阅读全文
0 0
- FZU 2150 BFS
- [BFS] FZU 2150
- fzu 2150 Fire Game(BFS)
- fzu 2150 Fire Game(BFS)
- FZU 2150 Fire Game(BFS)
- fzu 2150 Fire Game_(bfs)
- FZU--2150--Fire Game【BFS】
- FZU 2150Fire game-BFS
- FZU 2150 Fire Game(BFS)
- FZU 2150 Fire Game(bfs)
- FZU - 2150 Fire Game(BFS)
- FZU 2150 Fire Game(bfs)
- FZU-2150-Fire Game【BFS】
- FZU 2150 Fire Game (BFS)
- FZU 2150(DFS+BFS)
- FZU 2150 Fire Game BFS
- 【BFS】FZU 2150 Fire Game
- [FZU](2150)Fire Game ---bfs
- ES6语法学习(一):class(1)
- 疯狂写作中
- Python eval 函数的滥用
- 一个开发中常见到的问题
- 023day(指针与字符串)
- [BFS] FZU 2150
- 关于java重载函数,参数为null时,调用的处理。(精确性原则)
- 安卓学习笔记——Android点击事件的四种写法
- 一个简单的子类继承父类私有方法代码
- KMP算法
- 兼容ie6,7,8 的 ssm+redis的简单聊天室搭建
- 预编译头的作用
- Tensorflow学习与应用二
- HTTPClient和HttpURLConnection实例对比