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 88 66
题意很明确,就不再多嘴了,需要注意的是KFC不止一个,有多个,但是又不能每个都调用bfs,否则会超时,所以只需将Y的起点跑一边,M的起点跑一边,当经过KFC点的时候记录下来,找最小值就行了,所以本题只调用了两边bfs;
用了两种做法一个是用数组记录步数,一个是用map记录步数,两种相差不大
P数组记录步数
#include <map> #include <set> #include <cmath> #include <queue> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define inf 0x3f3f3f3f #define eps 1e-10 #define maxn 300005 #define zero(a) fabs(a)<eps #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define pb(a) push_back(a) #define mem(a,b) memset(a,b,sizeof(a)) #define LL long long #define lson step<<1 #define rson step<<1|1 #define MOD 1000000009 #define sqr(a) ((a)*(a)) using namespace std; int n , m; //行和列 map<pair<int,int>,int> mat; //将KFC的点当做键 pair<int,int> pr; //存KFC的点 int Yx, Yy, Mx, My; //分别代表Y的起点和M的起点 int d[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};//四个方向 int v[205][205]; //用于标记 int P[205][205]; //记录步数 char Map[205][205]; //存地点 struct node { int x, y; int p ; }cur,nex; queue<node> q; void bfs(int x, int y) { cur.x = x; cur.y = y; cur.p = 0; v[x][y] = 1; q.push(cur); while(!q.empty()) { cur = q.front(); q.pop(); pr.first = cur.x; pr.second = cur.y; if(mat[pr] == -1) { // 当经过KFC P[cur.x][cur.y] += cur.p; //把路径加起来即可 这点需要仔细想一下,很精妙 , 不需要加其他的 } for(int i = 0; i < 4; i++) { nex.x = cur.x + d[i][0]; nex.y = cur.y + d[i][1]; if(nex.x < 1 || nex.y < 1 || nex.x > n || nex.y > m || v[nex.x][nex.y]) continue; if(Map[nex.x][nex.y] != '#') { nex.p = cur.p + 1; v[nex.x][nex.y] = 1; q.push(nex); } } } } int main() { while(~scanf("%d%d",&n,&m)) { int mn = inf; mem(v, 0); mat.clear(); for(int i = 1; i <= n; i++) { scanf("%s",Map[i]+1); for(int j = 1; j <= m; j++) { if(Map[i][j] == 'Y') { Yx = i; Yy = j; } if(Map[i][j] == 'M') { Mx = i; My = j; } if(Map[i][j] == '@') { mat[make_pair(i,j)] = -1; } } } mem(P , 0); bfs(Yx , Yy); mem(v , 0); bfs(Mx , My); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(P[i][j]) { mn = Min(mn , P[i][j]); } } } printf("%d\n",mn*11); } return 0; }
map记录步数
#include <map> #include <set> #include <cmath> #include <queue> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define inf 0x3f3f3f3f #define eps 1e-10 #define maxn 300005 #define zero(a) fabs(a)<eps #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define pb(a) push_back(a) #define mem(a,b) memset(a,b,sizeof(a)) #define LL long long #define lson step<<1 #define rson step<<1|1 #define MOD 1000000009 #define sqr(a) ((a)*(a)) using namespace std; int n , m; int Yx,Yy,Mx,My; int v[205][205]; pair<int,int> pr; char Map[205][205]; map<pair<int,int>,int> mat; int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; struct node { int x,y; int p; }cur,nex; queue<node> q; bool MAP(int x, int y) { return x>0&&y>0&&x<=n&&y<=m; } void bfs(int x, int y) { cur.x = x; cur.y = y; cur.p = 0; v[x][y] = 1; q.push(cur); while(!q.empty()) { cur = q.front(); q.pop(); pr.first = cur.x; pr.second = cur.y; if(mat[pr]) { if(mat[pr] == inf) mat[pr] = 0; mat[pr] += cur.p; } for(int i = 0; i < 4; i++) { int dx = cur.x + d[i][0]; int dy = cur.y + d[i][1]; if(MAP(dx,dy)) { if(!v[dx][dy] && Map[dx][dy] != '#') { nex.x = dx; nex.y = dy; nex.p = cur.p+1; v[dx][dy] = 1; q.push(nex); } } } } return ; } int main() { while(~scanf("%d%d",&n,&m)) { int mn = inf; mat.clear(); for(int i = 1; i <= n; i++) { scanf("%s",Map[i]+1); for(int j = 1; j <= m; j++) { if(Map[i][j] == 'Y') { Yx = i; Yy = j; } else if(Map[i][j] == 'M') { Mx = i; My = j; } else if(Map[i][j] == '@') { mat[make_pair(i,j)] = inf; } } } mem(v, 0); bfs(Yx,Yy); mem(v, 0); bfs(Mx,My); map<pair<int,int>,int>::iterator it; for(it = mat.begin(); it != mat.end(); it++) { if((*it).second) mn = Min(mn , (*it).second); } printf("%d\n",mn*11); } return 0; }