hdu--1240--很水的bfs
今天 没什么事 有空在家 就再做了一题~
撸2盘 刷一题 的节奏....
这题 好水的 ..
#include <iostream> #include <cstring> #include <queue> using namespace std; int n; char maze[110][15]; bool vis[110][15]; int dir[4][2]={1,0,-1,0,0,-1,0,1}; struct data { int x , y; int step; }st,end; int bfs( ) { data now , next; int xx , yy; queue<data>q; memset( vis , false , sizeof(vis) ); while( !q.empty() ) q.pop(); q.push(st); vis[st.x][st.y] = true; while( !q.empty() ) { now = q.front(); q.pop(); if( now.x == end.x && now.y == end.y ) { return now.step; } for( int i = 0 ; i<4 ; i++ ) { xx = now.x + dir[i][0]; yy = now.y + dir[i][1]; if( xx>=0 && xx<n*n && yy>=0 && yy<n && maze[xx][yy]!='X' && !vis[xx][yy] ) { next.x = xx; next.y = yy; next.step = now.step + 1; vis[xx][yy] = true; q.push(next); } } } return -1; } int main() { int ans , x , y , z; char str[10]; while( cin>>str ) { cin >> n; for( int i = 0 ; i<n*n ; i++ ) { for( int j = 0 ; j<n ; j++ ) { cin >> maze[i][j]; } } cin >> x >> y >> z; x += n*z; st.x = x; st.y = y; st.step = 0; cin >> x >> y >> z; x += n*z; end.x = x; end.y = y; cin >> str; ans = bfs( ); if( -1 == ans ) cout << "NO ROUTE" << endl; else cout << n << " " << ans << endl; } return 0; }
有个 蛮麻烦的bfs 我出现了个bug 太奇怪了 =-=
just follow your heart