#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;
#define BIGMOD 1000012177LL
#define MAX_N 2048
char buf[MAX_N][MAX_N];
int dist[MAX_N][MAX_N];
struct Vertex {
int r,c;
int dist;
Vertex(int r, int c, int dist) : r(r), c(c), dist(dist) {};
};
pair<LL, int> solve(char buf[MAX_N][MAX_N], int rows, int cols, vector<pair<LL, LL> > people)
{
pair<int, int> dir[]={{-1,0}, {1,0}, {0,-1}, {0,1}};
Vertex start(0,0, 0);
REP(i,rows) REP(j,cols) dist[i][j] = -1;
dist[0][0] = 0;
queue<Vertex> Q;
Q.push(start);
while (!Q.empty())
{
Vertex v = Q.front();
Q.pop();
if (dist[v.r][v.c] > 0)
{
continue;
}
dist[v.r][v.c] = v.dist;
for (int i=0; i < 4; i++)
{
int nr = v.r + dir[i].first;
int nc = v.c + dir[i].second;
if (nr < 0 || nr >= rows) continue;
if (nc < 0 || nc >= cols) continue;
if (buf[nr][nc] != '.') continue;
Q.push(Vertex(nr, nc, v.dist+1));
}
}
//printf("Shortest path length=%d\n", dist[rows-1][cols-1]);
LL X = (dist[rows-1][cols-1] - (rows + cols - 2)) / 2;
//printf("X=%lld\n", X);
LL fastestTime = -1;
int numberOfAchievers = 0;
for (int i=0; i < people.size(); i++)
{
LL ai = people[i].first;
LL bi = people[i].second;
LL t = (rows + cols - 2) * ai + X * (ai + bi);
//printf("osoba %d time=%lld\n", i, t);
if (fastestTime == -1 || t < fastestTime)
{
fastestTime = t;
}
}
for (int i=0; i < people.size(); i++)
{
LL ai = people[i].first;
LL bi = people[i].second;
LL t = (rows + cols - 2) * ai + X * (ai + bi);
numberOfAchievers += (t == fastestTime) ? 1 : 0;
}
return make_pair(fastestTime, numberOfAchievers);
}
int main() {
int rows, cols, peopleCount;
scanf("%d%d%d",&rows, &cols, &peopleCount);
for (int i=0; i < rows; i++)
{
scanf("%s", buf[i]);
}
vector<pair<LL, LL> > people;
for (int i=0; i < peopleCount; i++)
{
LL ai, bi;
scanf("%lld%lld", &ai, &bi);
people.emplace_back(make_pair(ai, bi));
}
pair<LL, int> res = solve(buf, rows, cols, people);
printf("%lld %d\n", res.first, res.second);
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 | #include <bits/stdc++.h> #include <unistd.h> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; #define BIGMOD 1000012177LL #define MAX_N 2048 char buf[MAX_N][MAX_N]; int dist[MAX_N][MAX_N]; struct Vertex { int r,c; int dist; Vertex(int r, int c, int dist) : r(r), c(c), dist(dist) {}; }; pair<LL, int> solve(char buf[MAX_N][MAX_N], int rows, int cols, vector<pair<LL, LL> > people) { pair<int, int> dir[]={{-1,0}, {1,0}, {0,-1}, {0,1}}; Vertex start(0,0, 0); REP(i,rows) REP(j,cols) dist[i][j] = -1; dist[0][0] = 0; queue<Vertex> Q; Q.push(start); while (!Q.empty()) { Vertex v = Q.front(); Q.pop(); if (dist[v.r][v.c] > 0) { continue; } dist[v.r][v.c] = v.dist; for (int i=0; i < 4; i++) { int nr = v.r + dir[i].first; int nc = v.c + dir[i].second; if (nr < 0 || nr >= rows) continue; if (nc < 0 || nc >= cols) continue; if (buf[nr][nc] != '.') continue; Q.push(Vertex(nr, nc, v.dist+1)); } } //printf("Shortest path length=%d\n", dist[rows-1][cols-1]); LL X = (dist[rows-1][cols-1] - (rows + cols - 2)) / 2; //printf("X=%lld\n", X); LL fastestTime = -1; int numberOfAchievers = 0; for (int i=0; i < people.size(); i++) { LL ai = people[i].first; LL bi = people[i].second; LL t = (rows + cols - 2) * ai + X * (ai + bi); //printf("osoba %d time=%lld\n", i, t); if (fastestTime == -1 || t < fastestTime) { fastestTime = t; } } for (int i=0; i < people.size(); i++) { LL ai = people[i].first; LL bi = people[i].second; LL t = (rows + cols - 2) * ai + X * (ai + bi); numberOfAchievers += (t == fastestTime) ? 1 : 0; } return make_pair(fastestTime, numberOfAchievers); } int main() { int rows, cols, peopleCount; scanf("%d%d%d",&rows, &cols, &peopleCount); for (int i=0; i < rows; i++) { scanf("%s", buf[i]); } vector<pair<LL, LL> > people; for (int i=0; i < peopleCount; i++) { LL ai, bi; scanf("%lld%lld", &ai, &bi); people.emplace_back(make_pair(ai, bi)); } pair<LL, int> res = solve(buf, rows, cols, people); printf("%lld %d\n", res.first, res.second); return 0; } |
English