[hdu 2102]bfs+註意INF
阿新 • • 發佈:2017-09-04
push class get return sca main c++ const std
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2102
感覺這個題非常水,結果一直WA,最後發現居然是0x3f3f3f3f不夠大導致的……把INF改成INF+INF就過了。
#include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1}; const int fy[]={1,-1,0,0}; int bfs() { memset(vis,false,sizeof(vis)); queue< pair<int,int> > q; while (!q.empty()) q.pop(); q.push(make_pair(0*10000+1*100+1,0)); vis[0][1][1]=true; while (!q.empty()) { int layer=q.front().first/10000; int x=q.front().first%10000/100; int y=q.front().first%100; int st=q.front().second; q.pop();for (int i=0;i<4;i++) { int xx=x+fx[i]; int yy=y+fy[i]; if (s[layer][xx][yy]!=‘*‘ && !vis[layer][xx][yy]) { if (s[layer][xx][yy]==‘P‘ || (s[layer][xx][yy]==‘#‘ && s[layer^1][xx][yy]==‘P‘)) return st+1;if (s[layer][xx][yy]==‘#‘ && !vis[layer^1][xx][yy]) { vis[layer][xx][yy]=vis[layer^1][xx][yy]=true; q.push(make_pair((layer^1)*10000+xx*100+yy,st+1)); } if (s[layer][xx][yy]==‘.‘ || s[layer][xx][yy]==‘S‘) { vis[layer][xx][yy]=true; q.push(make_pair(layer*10000+xx*100+yy,st+1)); } } } } return INF+INF; } int main() { int T; scanf("%d",&T); while (T--) { memset(s,‘*‘,sizeof(s)); int n,m,t; cin >>n >>m >>t; for (int _=0;_<2;_++) for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin >>s[_][i][j]; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (s[0][i][j]==‘#‘&&s[1][i][j]==‘#‘ || s[0][i][j]==‘#‘&&s[1][i][j]==‘*‘ || s[0][i][j]==‘*‘&&s[1][i][j]==‘#‘) s[0][i][j]=s[1][i][j]=‘*‘; if (bfs()<=t) cout <<"YES" <<endl; else cout <<"NO" <<endl; } return 0; }
[hdu 2102]bfs+註意INF