Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10715 Accepted Submission(s): 3522
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.
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
Author
yifenfei
—————————————————————————————————————————————
注意:
1.本题用BFS .
2.注意起点的选取 .
下帖两个代码:
—————————————————————————————————————————————
错误代码:
以@为起点,由于@可以有多个,每一个@都需要运行一次bfs,所以毫无悬念的 Time Limit Exceeded.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
char map[205][205];
int vis[205][205];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m,cnt,ex1,ex2,ey1,ey2;
int sx[40000],sy[40000];
struct st{
int x;
int y;
int time;
friend bool operator<( st a,st b )
{
return a.time > b.time;
}
};
int bfs(int x, int y)
{
memset(vis,0,sizeof(vis));
st ang,next;
int flag = 0;
int T1 = 0;
int T2 = 0;
ang.x = x;
ang.y = y;
ang.time = 0;
vis[x][y] = 1;
priority_queue<st>que;
que.push(ang);
while( que.size() )
{
ang = que.top();
que.pop();
if( ang.x == ex1 && ang.y == ey1 ){
T1 = ang.time;
flag++;
}
if( ang.x == ex2 && ang.y == ex2 ){
T2 = ang.time;
flag++;
}
if( flag == 2 ){
return (T1+T2)*11;
}
for( int k=0; k<4; k++ )
{
next.x = ang.x + dx[k];
next.y = ang.y + dy[k];
if( next.x>=0 && next.y>=0 && next.x<n && next.y<m && !vis[next.x][next.y] && map[next.x][next.y]!='#')
{
next.time = ang.time+1;
vis[next.x][next.y] = 1;
que.push(next);
}
}
}
return 0;
}
int main()
{
while( ~scanf("%d%d",&n,&m) )
{
for( int i=0; i<n; i++ )
{
scanf("%s",map[i]);
}
cnt = 0;
for( int i=0; i<n; i++ )
for( int j=0; j<m; j++ )
{
if( map[i][j] == 'Y'){
ex1 = i;
ey1 = j;
}
if( map[i][j] == 'M'){
ex2 = i;
ey2 = j;
}
if( map[i][j] == '@'){
sx[cnt] = i;
sy[cnt] = j;
cnt++;
}
}
int ans = INF;
for( int i=0; i<cnt; i++ )
{
int mid = bfs(sx[i],sy[i]);
if( mid < ans)
{
ans = mid;
}
}
printf("%d\n",ans);
}
return 0;
}
——————————————————————————————————————————————
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
char map[205][205];
int vis[205][205];
int dis1[205][205];
int dis2[205][205];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m;
struct st{
int x;
int y;
int time;
friend bool operator<( st a,st b )
{
return a.time > b.time;
}
};
void bfs1( int x,int y )
{
st ang,next;
memset(vis,0,sizeof(vis));
for( int i=0; i<n; i++ )
for( int j=0; j<m; j++ )
{
dis1[i][j] = INF;
}
ang.x = x;
ang.y = y;
ang.time = 0;
vis[x][y] = 1;
priority_queue<st>que1;
que1.push(ang);
while( que1.size() )
{
ang = que1.top();
que1.pop();
if( map[ang.x][ang.y] == '@' ){
dis1[ang.x][ang.y] = ang.time*11;
}
for( int k=0; k<4; k++ )
{
next.x = ang.x + dx[k];
next.y = ang.y + dy[k];
if( next.x>=0 && next.y>=0 && next.x<n && next.y<m && !vis[next.x][next.y] && map[next.x][next.y]!='#')
{
next.time = ang.time+1;
vis[next.x][next.y] = 1;
que1.push(next);
}
}
}
}
void bfs2( int x,int y )
{
st ang,next;
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++ )
for(int j=0; j<m; j++ )
{
dis2[i][j] = INF;
}
ang.x = x;
ang.y = y;
ang.time = 0;
vis[x][y] = 1;
priority_queue<st>que2;
que2.push(ang);
while( que2.size() )
{
ang = que2.top();
que2.pop();
if( map[ang.x][ang.y] == '@' ){
dis2[ang.x][ang.y] = ang.time*11;
}
for( int k=0; k<4; k++ )
{
next.x = ang.x + dx[k];
next.y = ang.y + dy[k];
if( next.x>=0 && next.y>=0 && next.x<n && next.y<m && !vis[next.x][next.y] && map[next.x][next.y]!='#')
{
next.time = ang.time+1;
vis[next.x][next.y] = 1;
que2.push(next);
}
}
}
}
int main()
{
int sx1,sx2,sy1,sy2;
while( ~scanf("%d%d",&n,&m) )
{
for( int i=0; i<n; i++ )
{
scanf("%s",map[i]);
}
for( int i=0; i<n; i++ )
for( int j=0; j<m; j++ )
{
if( map[i][j] == 'Y'){
sx1 = i;
sy1 = j;
}
if( map[i][j] == 'M'){
sx2 = i;
sy2 = j;
}
}
bfs1(sx1,sy1);
bfs2(sx2,sy2);
int ans = INF;
for( int i=0; i<n; i++ )
{
for( int j=0; j<m; j++ )
{
int mid = dis1[i][j]+dis2[i][j];
if( mid < ans )
{
ans = mid;
}
}
}
printf("%d\n",ans);
}
return 0;
}