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.
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
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 8866
找一个kfc的位置,让两个人花费的总时间最少(PS:他们不处于同一时间下吗,为什么要找时间和,题目说啥就是啥吧)
从两个人的位置开始做两次广搜,记录下到kfc需要的步数,最后把步数相加,找一下在可达的kfc里面最小的时间花费就行了。忘记把动态数组初始化,看着大佬们一遍就过,而我一直WA到比赛结束。。。
#include<cstdio> #include<queue> #include<vector> #include<cstring> using namespace std; const int M = 205; const int INF = 99999999; struct Node { int x, y, s; }; char mapp[M][M]; int mv[4][2] = {{0,-1},{0,1},{1,0},{-1,0}}; vector<Node> kfc; int n, m, yx, yy, mx, my; int tim[M][M]; bool bo[M][M]; void bfs(int x, int y) { memset(bo, false, sizeof(bo)); Node tmp, nex; queue<Node> q; tmp.x = x; tmp.y = y; tmp.s = 0; tim[x][y] = 0; bo[x][y] = true; q.push(tmp); while(!q.empty()) { tmp = q.front(); q.pop(); for(int i=0;i<4;i++) { nex.x = tmp.x + mv[i][0]; nex.y = tmp.y + mv[i][1]; nex.s = tmp.s + 1; if(nex.x<1||nex.x>n||nex.y<1||nex.y>m) continue; if(mapp[nex.x][nex.y]!='#'&&!bo[nex.x][nex.y]) { bo[nex.x][nex.y] = true; tim[nex.x][nex.y] += nex.s; q.push(nex); } } } } int main() { Node tmm; int ans; while(~scanf("%d%d", &n, &m)) { kfc.clear();//这句老是忘记,坑的是不加这一句我试的每组样例都还能过!!?? for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf(" %c", &mapp[i][j]); tim[i][j] = 0; if(mapp[i][j]=='Y') { yx = i; yy = j; } else if(mapp[i][j]=='M') { mx = i; my = j; } else if(mapp[i][j]=='@') { tmm.x = i; tmm.y = j; kfc.push_back(tmm); } } bfs(yx, yy); bfs(mx, my); ans = INF; vector<Node>::iterator it=kfc.begin(); for(;it!=kfc.end();it++) if(tim[it->x][it->y]!=0) ans = min(ans, tim[it->x][it->y]); printf("%d\n", 11*ans); } return 0; }