I'm going... this question is speechless.
I forgot to clear the queue... Of course, if you declare the queue in the function, it will be okay. Every call is equivalent to clearing the queue.
And I declare it as a global variable...
The slot is stuck.
Leave this question blank.
Girls are still very thoughtful...
The third digit of the three-dimensional vis array is used to move the time remaining in the step % K. Because of the special nature of the question conditions, the stone will disappear when time % K = 0.,
In addition to this, nothing else. It's a very common BFS that searches for the status of the next layer from one layer.
1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 const int size = 110; 7 int stx , sty; 8 int n , m , k; 9 int dir[4][2] = {1,0,-1,0,0,1,0,-1};10 char mp[size][size];11 bool vis[size][size][15];12 struct node13 {14 int x;15 int y;16 int step;17 node( int a , int b , int c ):x(a),y(b),step(c){};18 node(){};19 };20 queue<node>q;21 22 int bfs( )23 {24 while(!q.empty())25 q.pop();26 int ans = -1;27 int xx , yy , step;28 node now;29 q.push( node(stx,sty,0) );30 vis[stx][sty][0] = true;31 while( !q.empty() )32 {33 now = q.front();34 q.pop();35 if( mp[now.x][now.y] == ‘G‘ )36 return now.step;37 for( int i = 0 ; i<4 ; i++ )38 {39 xx = now.x + dir[i][0];40 yy = now.y + dir[i][1];41 step = (now.step+1)%k;42 if( xx>=1 && xx<=n && yy>=1 && yy<=m && !vis[xx][yy][step] )43 {44 if( mp[xx][yy] == ‘#‘ && !step )45 {46 q.push( node(xx,yy,now.step+1) );47 vis[xx][yy][step] = true;48 }49 if( mp[xx][yy]!=‘#‘ )50 {51 q.push( node(xx,yy,now.step+1) );52 vis[xx][yy][step] = true;53 }54 }55 }56 }57 return ans;58 }59 60 int main()61 {62 cin.sync_with_stdio(false);63 int t , ans;64 cin >> t;65 while(t--)66 {67 cin >> n >> m >> k;68 memset( vis , false , sizeof(vis) );69 for( int i = 1 ; i<=n ; i++ )70 {71 for( int j = 1 ; j<=m ; j++ )72 {73 cin >> mp[i][j];74 if( mp[i][j] == ‘Y‘ )75 {76 stx = i;77 sty = j;78 }79 }80 }81 ans = bfs( );82 if( ans == -1 )83 cout << "Please give me another chance!" << endl;84 else85 cout << ans << endl;86 }87 return 0;88 }
View code
Today:
One day
You think of someone for no reason
She made you look forward to tomorrow
But it does not appear in your tomorrow.
HDU -- 2579 -- the second appointment with a girl