#include <iostream> #include<vector> #include<string> #include<queue> #include<utility> using namespace std; vector<string>tab; vector<vector<bool>>visited; vector<vector<int>>parent; void bfs(int n, int m){ queue<pair<int,int>>q; q.push({0,0}); visited[0][0]=true; while(!q.empty()){ int y =q.front().first; int x = q.front().second; q.pop(); if(y>0 && !visited[y-1][x] && tab[y-1][x]=='.'){ visited[y-1][x]=true; parent[y-1][x]=3; q.push({y-1,x}); } if(y<n-1 && !visited[y+1][x] && tab[y+1][x]=='.'){ visited[y+1][x]=true; parent[y+1][x]=1; q.push({y+1,x}); } if(x>0 && !visited[y][x-1] && tab[y][x-1]=='.'){ visited[y][x-1]=true; parent[y][x-1]=2; q.push({y,x-1}); } if(x<m-1 && !visited[y][x+1] && tab[y][x+1]=='.'){ visited[y][x+1]=true; parent[y][x+1]=4; q.push({y,x+1}); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m,k; cin>>n>>m>>k; tab.resize(n);visited.resize(n);parent.resize(n); for(int i = 0;i<n;++i){visited[i].resize(m);parent[i].resize(m);} for (int i = 0;i<n;++i){ cin>>tab[i]; } bfs(n,m); int up = 0, down=0; int x=m-1,y=n-1; while(x!=0 || y!=0){ if(parent[y][x]==1){ ++up; --y; } else if(parent[y][x]==3){ ++down; ++y; } else if(parent[y][x]==2){ ++down; ++x; } else if(parent[y][x]==4){ ++up; --x; } } int kup,kdown,ile=1; long long best=900000000000000000,temp; for(int i = 0;i<k;++i){ cin>>kup>>kdown; temp=kup*up+kdown*down; if(temp<best || i==0){ best=temp; ile=1; } else if(temp==best)++ile; } cout<<best<<" "<<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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <iostream> #include<vector> #include<string> #include<queue> #include<utility> using namespace std; vector<string>tab; vector<vector<bool>>visited; vector<vector<int>>parent; void bfs(int n, int m){ queue<pair<int,int>>q; q.push({0,0}); visited[0][0]=true; while(!q.empty()){ int y =q.front().first; int x = q.front().second; q.pop(); if(y>0 && !visited[y-1][x] && tab[y-1][x]=='.'){ visited[y-1][x]=true; parent[y-1][x]=3; q.push({y-1,x}); } if(y<n-1 && !visited[y+1][x] && tab[y+1][x]=='.'){ visited[y+1][x]=true; parent[y+1][x]=1; q.push({y+1,x}); } if(x>0 && !visited[y][x-1] && tab[y][x-1]=='.'){ visited[y][x-1]=true; parent[y][x-1]=2; q.push({y,x-1}); } if(x<m-1 && !visited[y][x+1] && tab[y][x+1]=='.'){ visited[y][x+1]=true; parent[y][x+1]=4; q.push({y,x+1}); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m,k; cin>>n>>m>>k; tab.resize(n);visited.resize(n);parent.resize(n); for(int i = 0;i<n;++i){visited[i].resize(m);parent[i].resize(m);} for (int i = 0;i<n;++i){ cin>>tab[i]; } bfs(n,m); int up = 0, down=0; int x=m-1,y=n-1; while(x!=0 || y!=0){ if(parent[y][x]==1){ ++up; --y; } else if(parent[y][x]==3){ ++down; ++y; } else if(parent[y][x]==2){ ++down; ++x; } else if(parent[y][x]==4){ ++up; --x; } } int kup,kdown,ile=1; long long best=900000000000000000,temp; for(int i = 0;i<k;++i){ cin>>kup>>kdown; temp=kup*up+kdown*down; if(temp<best || i==0){ best=temp; ile=1; } else if(temp==best)++ile; } cout<<best<<" "<<ile; return 0; } |