Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19320 Accepted Submission(s):
6234
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.
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
#include <iostream> #include <queue> using namespace std; char a[205][205]; int y[205][205],m[205][205]; int d[4][2]={-1,0,1,0,0,-1,0,1}; int n,k; int yx,yy,mx,my; queue<int>q; queue<int>qy; queue<int>qm; bool safe(int x,int y) { if(x>n||y>k||x<=0||y<=0) return 0; return 1; } int main() { while(cin>>n>>k) { int i,j; long long min=9999999; for(i=1;i<=n;i++) { for(j=1;j<=k;j++) { cin>>a[i][j]; y[i][j]=-1; m[i][j]=-1; if(a[i][j]=='Y') { qy.push(i); qy.push(j); y[i][j]=0; } if(a[i][j]=='M') { qm.push(i); qm.push(j); m[i][j]=0; } if(a[i][j]=='@') { q.push(i); q.push(j); } } } while(!qy.empty()||!qm.empty()) { if(!qy.empty()) { yx=qy.front();qy.pop(); yy=qy.front();qy.pop(); int vy=y[yx][yy]; for(i=0;i<4;i++) { if(safe(yx+d[i][0],yy+d[i][1])&&a[yx+d[i][0]][yy+d[i][1]]!='#'&&y[yx+d[i][0]][yy+d[i][1]]==-1) { y[yx+d[i][0]][yy+d[i][1]]=vy+1; qy.push(yx+d[i][0]); qy.push(yy+d[i][1]); } } } if(!qm.empty()) { mx=qm.front();qm.pop(); my=qm.front();qm.pop(); int vm=m[mx][my]; for(i=0;i<4;i++) { if(safe(mx+d[i][0],my+d[i][1])&&a[mx+d[i][0]][my+d[i][1]]!='#'&&m[mx+d[i][0]][my+d[i][1]]==-1) { m[mx+d[i][0]][my+d[i][1]]=vm+1; qm.push(mx+d[i][0]); qm.push(my+d[i][1]); } } } } int kx,ky; while(!q.empty()) { kx=q.front();q.pop(); ky=q.front();q.pop(); if(y[kx][ky]==-1||m[kx][ky]==-1) continue; long long s=y[kx][ky]+m[kx][ky]; if(s<min) min=s; } cout<<min*11<<endl; } return 0; }
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19320 Accepted Submission(s):
6234
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.
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
#include <iostream> #include <queue> using namespace std; char a[205][205]; int y[205][205],m[205][205]; int d[4][2]={-1,0,1,0,0,-1,0,1}; int n,k; int yx,yy,mx,my; queue<int>q; queue<int>qy; queue<int>qm; bool safe(int x,int y) { if(x>n||y>k||x<=0||y<=0) return 0; return 1; } int main() { while(cin>>n>>k) { int i,j; long long min=9999999; for(i=1;i<=n;i++) { for(j=1;j<=k;j++) { cin>>a[i][j]; y[i][j]=-1; m[i][j]=-1; if(a[i][j]=='Y') { qy.push(i); qy.push(j); y[i][j]=0; } if(a[i][j]=='M') { qm.push(i); qm.push(j); m[i][j]=0; } if(a[i][j]=='@') { q.push(i); q.push(j); } } } while(!qy.empty()||!qm.empty()) { if(!qy.empty()) { yx=qy.front();qy.pop(); yy=qy.front();qy.pop(); int vy=y[yx][yy]; for(i=0;i<4;i++) { if(safe(yx+d[i][0],yy+d[i][1])&&a[yx+d[i][0]][yy+d[i][1]]!='#'&&y[yx+d[i][0]][yy+d[i][1]]==-1) { y[yx+d[i][0]][yy+d[i][1]]=vy+1; qy.push(yx+d[i][0]); qy.push(yy+d[i][1]); } } } if(!qm.empty()) { mx=qm.front();qm.pop(); my=qm.front();qm.pop(); int vm=m[mx][my]; for(i=0;i<4;i++) { if(safe(mx+d[i][0],my+d[i][1])&&a[mx+d[i][0]][my+d[i][1]]!='#'&&m[mx+d[i][0]][my+d[i][1]]==-1) { m[mx+d[i][0]][my+d[i][1]]=vm+1; qm.push(mx+d[i][0]); qm.push(my+d[i][1]); } } } } int kx,ky; while(!q.empty()) { kx=q.front();q.pop(); ky=q.front();q.pop(); if(y[kx][ky]==-1||m[kx][ky]==-1) continue; long long s=y[kx][ky]+m[kx][ky]; if(s<min) min=s; } cout<<min*11<<endl; } return 0; }