题目链接: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; }
时间: 2024-10-11 23:06:24