#include <bits/stdc++.h>
using namespace std;
char tab[2007][2007];
bool vis[2007][2007];
pair<int,int> dp[2007][2007];
int n,m,k;
int mv[4][2]={{0,1}, {1,0}, {-1,0}, {0,-1}};
void bfs()
{
vis[1][1] = true;
dp[1][1] = {0,0};
queue<pair<int,int> > kol;
kol.push({1,1});
while(!kol.empty())
{
int x=kol.front().first, y=kol.front().second;
kol.pop();
for(int i=0; i < 4; i++)
{
if(vis[x+mv[i][0]][y+mv[i][1]] == false && tab[x+mv[i][0]][y+mv[i][1]] == '.')
{
vis[x+mv[i][0]][y+mv[i][1]] = true;
if(i < 2)
{
dp[x+mv[i][0]][y+mv[i][1]].first = dp[x][y].first + 1;
dp[x+mv[i][0]][y+mv[i][1]].second = dp[x][y].second;
}
else
{
dp[x+mv[i][0]][y+mv[i][1]].first = dp[x][y].first;
dp[x+mv[i][0]][y+mv[i][1]].second = dp[x][y].second + 1;
}
kol.push({x+mv[i][0], y+mv[i][1]});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for(int i=0; i<=m; i++)
{
vis[0][i] = true;
vis[n+1][i] = true;
}
for(int i=0; i<=n; i++)
{
vis[i][0] = true;
vis[i][m+1] = true;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin >> tab[i][j];
bfs();
int x = dp[n][m].first, y = dp[n][m].second;
multiset<long long>S;
for(int i=0; i<k; i++)
{
long long a,b;
cin >> a >> b;
S.insert(a*x+b*y);
}
long long h = *S.lower_bound(0);
cout << h << " " << S.count(h);
}
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 | #include <bits/stdc++.h> using namespace std; char tab[2007][2007]; bool vis[2007][2007]; pair<int,int> dp[2007][2007]; int n,m,k; int mv[4][2]={{0,1}, {1,0}, {-1,0}, {0,-1}}; void bfs() { vis[1][1] = true; dp[1][1] = {0,0}; queue<pair<int,int> > kol; kol.push({1,1}); while(!kol.empty()) { int x=kol.front().first, y=kol.front().second; kol.pop(); for(int i=0; i < 4; i++) { if(vis[x+mv[i][0]][y+mv[i][1]] == false && tab[x+mv[i][0]][y+mv[i][1]] == '.') { vis[x+mv[i][0]][y+mv[i][1]] = true; if(i < 2) { dp[x+mv[i][0]][y+mv[i][1]].first = dp[x][y].first + 1; dp[x+mv[i][0]][y+mv[i][1]].second = dp[x][y].second; } else { dp[x+mv[i][0]][y+mv[i][1]].first = dp[x][y].first; dp[x+mv[i][0]][y+mv[i][1]].second = dp[x][y].second + 1; } kol.push({x+mv[i][0], y+mv[i][1]}); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for(int i=0; i<=m; i++) { vis[0][i] = true; vis[n+1][i] = true; } for(int i=0; i<=n; i++) { vis[i][0] = true; vis[i][m+1] = true; } for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) cin >> tab[i][j]; bfs(); int x = dp[n][m].first, y = dp[n][m].second; multiset<long long>S; for(int i=0; i<k; i++) { long long a,b; cin >> a >> b; S.insert(a*x+b*y); } long long h = *S.lower_bound(0); cout << h << " " << S.count(h); } |
English