#include <bits/stdc++.h>
#define st first
#define nd second
#define ll long long
const int N=2005;
using namespace std;
char g[N][N];
int dp[N][N], n, m, q;
bool vis[N][N];
void bfs(){
queue<pair<int, int> > kolejka;
vis[1][1]=1;
kolejka.push({1, 1});
while(!kolejka.empty()){
auto x=kolejka.front();
kolejka.pop();
vis[x.st][x.nd]=1;
if(x.st>1&&vis[x.st-1][x.nd]==0){
kolejka.push({x.st-1, x.nd});
vis[x.st-1][x.nd]=1;
dp[x.st-1][x.nd]=dp[x.st][x.nd]+1;
}
if(x.st<n&&vis[x.st+1][x.nd]==0){
kolejka.push({x.st+1, x.nd});
vis[x.st+1][x.nd]=1;
dp[x.st+1][x.nd]=dp[x.st][x.nd]+1;
}
if(x.nd>1&&!vis[x.st][x.nd-1]){
kolejka.push({x.st, x.nd-1});
vis[x.st][x.nd-1]=1;
dp[x.st][x.nd-1]=dp[x.st][x.nd]+1;
}
if(x.nd<m&&!vis[x.st][x.nd+1]){
kolejka.push({x.st, x.nd+1});
vis[x.st][x.nd+1]=1;
dp[x.st][x.nd+1]=dp[x.st][x.nd]+1;
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin>>n>>m>>q;
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
cin>>g[i][j];
if(g[i][j]!='.') vis[i][j]=1;
}
}
bfs();
int p=dp[n][m];
int b=(p-n-m+2)/2;
int a=p-b;
ll ans=1e18, ile=0;
while(q--){
ll x, y;
cin>>x>>y;
ll w=x*a+y*b;
if(w==ans){
ile++;
}
if(w<ans){
ile=1;
ans=w;
}
}
cout<<ans<<' '<<ile;
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 | #include <bits/stdc++.h> #define st first #define nd second #define ll long long const int N=2005; using namespace std; char g[N][N]; int dp[N][N], n, m, q; bool vis[N][N]; void bfs(){ queue<pair<int, int> > kolejka; vis[1][1]=1; kolejka.push({1, 1}); while(!kolejka.empty()){ auto x=kolejka.front(); kolejka.pop(); vis[x.st][x.nd]=1; if(x.st>1&&vis[x.st-1][x.nd]==0){ kolejka.push({x.st-1, x.nd}); vis[x.st-1][x.nd]=1; dp[x.st-1][x.nd]=dp[x.st][x.nd]+1; } if(x.st<n&&vis[x.st+1][x.nd]==0){ kolejka.push({x.st+1, x.nd}); vis[x.st+1][x.nd]=1; dp[x.st+1][x.nd]=dp[x.st][x.nd]+1; } if(x.nd>1&&!vis[x.st][x.nd-1]){ kolejka.push({x.st, x.nd-1}); vis[x.st][x.nd-1]=1; dp[x.st][x.nd-1]=dp[x.st][x.nd]+1; } if(x.nd<m&&!vis[x.st][x.nd+1]){ kolejka.push({x.st, x.nd+1}); vis[x.st][x.nd+1]=1; dp[x.st][x.nd+1]=dp[x.st][x.nd]+1; } } } int main() { ios_base::sync_with_stdio(0); cin>>n>>m>>q; for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ cin>>g[i][j]; if(g[i][j]!='.') vis[i][j]=1; } } bfs(); int p=dp[n][m]; int b=(p-n-m+2)/2; int a=p-b; ll ans=1e18, ile=0; while(q--){ ll x, y; cin>>x>>y; ll w=x*a+y*b; if(w==ans){ ile++; } if(w<ans){ ile=1; ans=w; } } cout<<ans<<' '<<ile; return 0; } |
English