HDU 2612 (2次BFS,有点小细节)
Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
Sample Output
66
88
66
题解:这题很水,读完题目就有思路了,但是......在一个小细节上WA了3次...
将人到每个可以到达位置的最小时间用bfs打表存d[][],然后后扫地图,如果扫到@的话,维护ans;
but,有个小细节,问题在于最后维护ans,ans=min(d1[i][j]+d2[i][j], ans);这个会出现d[][]使用了初始化的值(0),即并不能到达这个位置,但由于min()的维护给我算进来了...
反思:查WA的时候,确定思路没错的话,查初始化(有否和维护的数据有冲突),查边界范围,查维护的数据
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <queue> 6 #define min(x,y) ((x)<(y)? (x):(y)) 7 using namespace std; 8 9 const int INF=0x3f3f3f3f; 10 const int maxn=500; 11 char Map[maxn][maxn]; 12 int d1[maxn][maxn], d2[maxn][maxn]; 13 int dx[4]={0,0,1,-1}, dy[4]={1,-1,0,0}; 14 15 int main () 16 { 17 //freopen("in.txt", "r", stdin); 18 int n,m; 19 while(cin>>n>>m) 20 { 21 int x1,y1,x2,y2; 22 for(int i=0; i<maxn; i++) 23 for(int j=0; j<maxn; j++) 24 { 25 d1[i][j]=d2[i][j]=0; 26 } 27 28 for(int i=0; i<n; i++) 29 for(int j=0; j<m; j++) 30 { 31 cin>>Map[i][j]; 32 if(Map[i][j]=='Y') 33 x1=i, y1=j; 34 if(Map[i][j]=='M') 35 x2=i, y2=j; 36 } 37 38 queue<pair<int, int> > que; 39 pair<int, int> p; 40 p.first=x1, p.second=y1; 41 que.push(p); 42 while(que.size()) 43 { 44 p=que.front(); que.pop(); 45 for(int i=0; i<4; i++) 46 { 47 int nx=p.first+dx[i], ny=p.second+dy[i]; 48 if( nx>=0&&nx<n&&ny>=0&&ny<m && (Map[nx][ny]=='.'||Map[nx][ny]=='@') && d1[nx][ny]==0) 49 { 50 que.push(make_pair(nx,ny)); 51 d1[nx][ny]=d1[p.first][p.second]+1; 52 } 53 } 54 } 55 56 57 p.first=x2, p.second=y2; 58 que.push(p); 59 while(que.size()) 60 { 61 p=que.front(), que.pop(); 62 for(int i=0; i<4; i++) 63 { 64 int nx=p.first+dx[i], ny=p.second+dy[i]; 65 if( nx>=0&&nx<n&&ny>=0&&ny<m && (Map[nx][ny]=='.'||Map[nx][ny]=='@') && d2[nx][ny]==0) 66 { 67 que.push(make_pair(nx,ny)); 68 d2[nx][ny]=d2[p.first][p.second]+1; 69 } 70 } 71 } 72 73 int ans=INF; 74 for(int i=0; i<n; i++) 75 for(int j=0; j<m; j++) 76 if(Map[i][j]=='@') 77 { 78 if(d1[i][j]!=0&&d2[i][j]!=0) 79 //这个判断之前没有想到,一直wa,后来看了下discuss,才发现这个问题, 80 //发现这个问题后,我最先d初始化为0改成初始化d为INF=0x3f3f3f3f,会溢出输出负数, 81 //发现自己很sb,算dis,d肯定初始化为0呀...,我看到这个min(d1[i][j]+d2[i][j], ans)有问题,就想到初始化为INF...... 82 ans=min(d1[i][j]+d2[i][j], ans); 83 } 84 cout<<ans*11<<endl; 85 } 86 return 0; 87 }