Find a way
问题描述
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.
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
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
思路
两次bfs,记录Y和M到达每个位置所需的最短时间。然后再遍历一次寻找‘@’所在位置,进而得到到达‘@’所需的最短时间。先打表,再按图索骥。
也许会有这样的顾虑,有某个‘@’是Y和M不能都到达的。其实完全不需要这样担心。题意中指出,给出的测试样例中必然会有至少一个‘@’是Y和M都能到达的。这意味着,Y和M是可以相互到达对方的起始位置的,也就是说不存在有一个‘@’是她们不能都到达的。
代码样例
//N-Find a way
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
char map[205][205];
int n, m;
int vist[205][205], t[205][205], dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node{
int x, y, step;
};
int check(int r, int c){
if(r<0||r>=n||c<0||c>=m||vist[r][c]||map[r][c]=='#') return 1;
return 0;
}
int bfs(int sx, int sy){
//init()
memset(vist, 0, sizeof(vist));
node p, q;
p.x=sx, p.y=sy, p.step=0;
vist[sx][sy]=1;
queue<node> Q;
Q.push(p);
while(!Q.empty()){
p=Q.front(), Q.pop();
for(int i=0; i<4; i++){
q=p;
q.x+=dir[i][0];
q.y+=dir[i][1];
if(check(q.x, q.y)) continue;
vist[q.x][q.y]=1;
q.step++;
t[q.x][q.y]+=q.step;
Q.push(q);
}
}
}
int main(void){
while(scanf("%d %d", &n, &m)!=EOF){
memset(t, 0, sizeof(t));
for(int i=0; i<n; i++){
scanf("%s", map[i]);
}//待map输入完成,才能bfs
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(map[i][j]=='Y'||map[i][j]=='M'){
bfs(i, j);
}
}
}
int ans=0x3f3f3f3f;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(map[i][j]=='@'&&t[i][j]&&ans>t[i][j]){
ans=t[i][j];
}
}
}
printf("%d\n", 11*ans);
}
return 0;
}
犯过的错误。。。
也许会有这样的疑惑,为什么不在bfs的while循环中设置一个出口——遍历到‘@’时就终止。
题意中呢,‘@’是有多个的。如果要有这样的出口,你就得对二维的地图做一次遍历,找到一个‘@’就把一个‘@’的地址分别协同Y和M放到bfs里做一次广搜。这样的开销是很大的。一个‘@’就意味着两次bfs,而且有的‘@’可能是Y和M都不能到达的。
也就是说,以下的程序结构并不推荐,submit时它超时了。这样看来,程序结构也是十分重要的,值得多花时间考虑。
//N-Find a way
#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
char map[205][205];
int n, m;
int vist[205][205], dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node{
int x, y, step;
};
int check(int r, int c){
if(r<0||r>=n||c<0||c>=m||vist[r][c]||map[r][c]=='#') return 1;
return 0;
}
int bfs(int sx, int sy, int ex, int ey){
//init()
memset(vist, 0, sizeof(vist));
node p, q;
p.x=sx, p.y=sy, p.step=0;
vist[sx][sy]=1;
queue<node> Q;
Q.push(p);
while(!Q.empty()){
p=Q.front(), Q.pop();
if(p.x==ex&&p.y==ey){
return p.step;
}
for(int i=0; i<4; i++){
q=p;
q.x+=dir[i][0];
q.y+=dir[i][1];
if(check(q.x, q.y)) continue;
vist[q.x][q.y]=1;
q.step++;
Q.push(q);
}
}
return -1;
}
int main(void){
int Yx, Yy, Mx, My;
while(scanf("%d %d", &n, &m)!=EOF){
for(int i=0; i<n; i++){
scanf("%s", map[i]);
for(int j=0; j<m; j++){
if(map[i][j]=='Y'){
Yx=i, Yy=j;
}
if(map[i][j]=='M'){
Mx=i, My=j;
}
}
}
int t1, t2, t, ans=0x3f3f3f3f;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(map[i][j]=='@'){
bfs(Yx, Yy, i, j);
bfs(Mx, My, i, j);
if(t1==-1||t2==-1) continue;
t=t1+t2;
if(t<ans) ans=t;
}
}
}
printf("%d\n", 11*ans);
}
return 0;
}
- 1605集训计划第十四天任务
- 提前一天完成