#include <iostream>
#include <vector>
#include <queue>
#include <limits.h>
using namespace std;
vector <vector <bool> > mountain;
vector <vector <bool> > added;
vector <vector <int> > _distance;
long long n, m;
bool is_valid(int y, int x){
if(y<n && y>=0){
if(x<m && x>=0){
//cout << mountain[y][x] << " " << (!added[y][x]) << endl;
return (mountain[y][x]&&(!added[y][x]));
}
}
return false;
}
void BFS(int y, int x)
{
queue<pair<int, int> > Q;
Q.push(make_pair(y, x));
while(!Q.empty())
{
pair<int , int> node;
node = Q.front();
y = node.first;
x = node.second;
added[y][x] = true;
Q.pop();
if(is_valid(y+1, x)){
Q.push(make_pair(y+1, x));
added[y+1][x] = true;
_distance[y+1][x] = _distance[y][x]+1;
}
if(is_valid(y-1, x)){
Q.push(make_pair(y-1, x));
added[y-1][x] = true;
_distance[y-1][x] = _distance[y][x]+1;
}
if(is_valid(y, x+1)){
Q.push(make_pair(y, x+1));
added[y][x+1] = true;
_distance[y][x+1] = _distance[y][x]+1;
}
if(is_valid(y, x-1)){
Q.push(make_pair(y, x-1));
added[y][x-1] = true;
_distance[y][x-1] = _distance[y][x]+1;
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k;
cin >> n>> m>> k;
for(int i=0; i<n; i++){
mountain.push_back(vector<bool>());
added.push_back(vector<bool>());
_distance.push_back(vector<int>());
for(int j=0; j<m; j++){
char t;
cin >> t;
mountain[i].push_back(t=='.'?true:false);
added[i].push_back(false);
_distance[i].push_back(0);
}
}
BFS(n-1, m-1);
long long min_value = LLONG_MAX;
int count = 0;
long long x = (_distance[0][0]-(n-1 + m-1))/2;
for(int i=0; i<k; i++){
long long a, b;
cin >> a >> b;
long long cur_value = a*(n-1 + m-1) + (a+b)*x;
if(cur_value<min_value){
min_value = cur_value;
count = 1;
}else if(cur_value==min_value){
count++;
}
}
cout << min_value << " " << count;
}
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 | #include <iostream> #include <vector> #include <queue> #include <limits.h> using namespace std; vector <vector <bool> > mountain; vector <vector <bool> > added; vector <vector <int> > _distance; long long n, m; bool is_valid(int y, int x){ if(y<n && y>=0){ if(x<m && x>=0){ //cout << mountain[y][x] << " " << (!added[y][x]) << endl; return (mountain[y][x]&&(!added[y][x])); } } return false; } void BFS(int y, int x) { queue<pair<int, int> > Q; Q.push(make_pair(y, x)); while(!Q.empty()) { pair<int , int> node; node = Q.front(); y = node.first; x = node.second; added[y][x] = true; Q.pop(); if(is_valid(y+1, x)){ Q.push(make_pair(y+1, x)); added[y+1][x] = true; _distance[y+1][x] = _distance[y][x]+1; } if(is_valid(y-1, x)){ Q.push(make_pair(y-1, x)); added[y-1][x] = true; _distance[y-1][x] = _distance[y][x]+1; } if(is_valid(y, x+1)){ Q.push(make_pair(y, x+1)); added[y][x+1] = true; _distance[y][x+1] = _distance[y][x]+1; } if(is_valid(y, x-1)){ Q.push(make_pair(y, x-1)); added[y][x-1] = true; _distance[y][x-1] = _distance[y][x]+1; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k; cin >> n>> m>> k; for(int i=0; i<n; i++){ mountain.push_back(vector<bool>()); added.push_back(vector<bool>()); _distance.push_back(vector<int>()); for(int j=0; j<m; j++){ char t; cin >> t; mountain[i].push_back(t=='.'?true:false); added[i].push_back(false); _distance[i].push_back(0); } } BFS(n-1, m-1); long long min_value = LLONG_MAX; int count = 0; long long x = (_distance[0][0]-(n-1 + m-1))/2; for(int i=0; i<k; i++){ long long a, b; cin >> a >> b; long long cur_value = a*(n-1 + m-1) + (a+b)*x; if(cur_value<min_value){ min_value = cur_value; count = 1; }else if(cur_value==min_value){ count++; } } cout << min_value << " " << count; } |
English