非常可乐
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 阅读全文 »
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 阅读全文 »
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
Source
题目类型:BFS+贪心
算法分析:首先贪心可以得到到’@’最短的距离是Yifenfei和Merceki分别到该位置时的距离和的最小值,此时可以调用两次BFS来将两人到所有’@’的距离求出来,然后更新最小值即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; char g[266][266]; int row, col, x2, y2, x3, y3, aa[266*266], bb[266*266], flag[266][266], tot; bool vis[266][266]; struct node { int a, b, cnt; node (int ax, int bx, int ccnt) : a (ax), b (bx), cnt (ccnt) {} node () {} }; void bfs (int x, int y, int f) { memset (vis, false, sizeof (vis)); vis[x][y] = true; queue <node> qu; qu.push (node (x, y, 0)); while (!qu.empty ()) { node tt = qu.front (); qu.pop (); if (g[tt.a][tt.b] == '@') { if (f == 1) aa[flag[tt.a][tt.b]] = tt.cnt; if (f == 2) bb[flag[tt.a][tt.b]] = tt.cnt; } for (int i = 0; i < 4; i++) { int tx = tt.a + dx[i], ty = tt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && !vis[tx][ty] && g[tx][ty] != '#') { vis[tx][ty] = true; qu.push (node (tx, ty, tt.cnt + 1)); } } } } int main() { // CFF; while (scanf ("%d %d", &row, &col) != EOF) { for (int i = 0; i < row; i++) scanf (" %s", g[i]); tot = 0; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) { if (g[i][j] == 'Y') x2 = i, y2 = j; if (g[i][j] == 'M') x3 = i, y3 = j; if (g[i][j] == '@') flag[i][j] = tot++; } fill (aa, aa + (266 * 266), INF); fill (bb, bb + (266 * 266), INF); bfs (x2, y2, 1); bfs (x3, y3, 2); int minval = INF; for (int i = 0; i < tot; i++) if (aa[i] < INF && bb[i] < INF) minval = min (minval, aa[i] + bb[i]); printf ("%d\n", minval * 11); } return 0; } |