#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define ST first
#define ND second
#define MP make_pair
#define PB push_back
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef set<int> SI;
typedef long long LL;
#ifdef DEBUG
const bool debug = true;
#else
const bool debug = false;
#endif
// #define MULTI_TASK true
const int INF = 1000 * 1000 * 1000;
const LL INFLL = 1e18;
const int MAKSN = 2 * 1000 + 13;
const LL MOD = 123456789LL;
LL N, M, k, l, a, b, c, d;
vector<string> mapa;
string s;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int pr[] = {1, 0, 0, 0};
int pu[] = {0, 0, 1, 0};
int pd[] = {0, 0, 0, 1};
int pl[] = {0, 1, 0, 0};
int moves[MAKSN][MAKSN];
bool vis[MAKSN][MAKSN];
LL rig[MAKSN][MAKSN];
LL down[MAKSN][MAKSN];
LL up[MAKSN][MAKSN];
LL lef[MAKSN][MAKSN];
void zeruj() {
mapa.clear();
}
void readIn() {
cin >> N >> M >> k;
REP(i, N) {
cin >> s;
mapa.PB(s);
}
}
bool inBoard(int x, int y) {
return x >= 0 && x < N && y >= 0 && y < M;
}
void BFS(int x, int y) {
queue<PII> q;
q.push({x, y});
moves[x][y] = 0;
rig[x][y] = 0;
down[x][y] = 0;
up[x][y] = 0;
lef[x][y] = 0;
vis[x][y] = true;
while (!q.empty()) {
auto p = q.front();
q.pop();
x = p.ST;
y = p.ND;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (inBoard(nx, ny) && mapa[nx][ny] != 'X' && !vis[nx][ny]) {
q.push({nx, ny});
vis[nx][ny] = true;
down[nx][ny] = down[x][y] + pd[i];
rig[nx][ny] = rig[x][y] + pr[i];
up[nx][ny] = up[x][y] + pu[i];
lef[nx][ny] = lef[x][y] + pl[i];
moves[nx][ny] = moves[x][y] + 1;
}
}
}
}
void solve() {
BFS(0, 0);
LL mini = INFLL, cnt = 0;
for (int i = 0; i < k; i++) {
cin >> a >> b;
LL act = a * (rig[N - 1][M - 1] + up[N - 1][M - 1]) + b * (lef[N - 1][M - 1] + down[N - 1][M - 1]);
if (act == mini)
cnt++;
if (act < mini) {
mini = act;
cnt = 1;
}
}
cout << mini << " " << cnt << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
#ifdef MULTI_TASK
int test;
cin >> test;
while (test--) {
#endif
zeruj();
readIn();
solve();
#ifdef MULTI_TASK
}
#endif
return 0;
}
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 122 123 124 125 126 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif // #define MULTI_TASK true const int INF = 1000 * 1000 * 1000; const LL INFLL = 1e18; const int MAKSN = 2 * 1000 + 13; const LL MOD = 123456789LL; LL N, M, k, l, a, b, c, d; vector<string> mapa; string s; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int pr[] = {1, 0, 0, 0}; int pu[] = {0, 0, 1, 0}; int pd[] = {0, 0, 0, 1}; int pl[] = {0, 1, 0, 0}; int moves[MAKSN][MAKSN]; bool vis[MAKSN][MAKSN]; LL rig[MAKSN][MAKSN]; LL down[MAKSN][MAKSN]; LL up[MAKSN][MAKSN]; LL lef[MAKSN][MAKSN]; void zeruj() { mapa.clear(); } void readIn() { cin >> N >> M >> k; REP(i, N) { cin >> s; mapa.PB(s); } } bool inBoard(int x, int y) { return x >= 0 && x < N && y >= 0 && y < M; } void BFS(int x, int y) { queue<PII> q; q.push({x, y}); moves[x][y] = 0; rig[x][y] = 0; down[x][y] = 0; up[x][y] = 0; lef[x][y] = 0; vis[x][y] = true; while (!q.empty()) { auto p = q.front(); q.pop(); x = p.ST; y = p.ND; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (inBoard(nx, ny) && mapa[nx][ny] != 'X' && !vis[nx][ny]) { q.push({nx, ny}); vis[nx][ny] = true; down[nx][ny] = down[x][y] + pd[i]; rig[nx][ny] = rig[x][y] + pr[i]; up[nx][ny] = up[x][y] + pu[i]; lef[nx][ny] = lef[x][y] + pl[i]; moves[nx][ny] = moves[x][y] + 1; } } } } void solve() { BFS(0, 0); LL mini = INFLL, cnt = 0; for (int i = 0; i < k; i++) { cin >> a >> b; LL act = a * (rig[N - 1][M - 1] + up[N - 1][M - 1]) + b * (lef[N - 1][M - 1] + down[N - 1][M - 1]); if (act == mini) cnt++; if (act < mini) { mini = act; cnt = 1; } } cout << mini << " " << cnt << "\n"; } int main() { ios_base::sync_with_stdio(0); #ifdef MULTI_TASK int test; cin >> test; while (test--) { #endif zeruj(); readIn(); solve(); #ifdef MULTI_TASK } #endif return 0; } |
English