#include <iostream> #include <vector> #include<bits/stdc++.h> #include <stdio.h> using namespace std; bool solveMazeUtil( int **maze, int x, int y, int R, int C, vector < vector<int> > &result); bool isSafe(int **maze, int x, int y, int R, int C){ if(x>=0 && x<R && y>=0 && y<C && maze[x][y]==1) { return true; } return false; } vector < vector<int> > solveMaze(int **maze, int R, int C) { vector < vector<int> > result; if(solveMazeUtil(maze, 0, 0, R, C, result) == false){ result.clear(); } return result; } bool solveMazeUtil(int **maze, int x, int y, int R, int C, vector < vector<int> > &result) { if(x==R-1 && y==C-1 && maze[x][y]==1){ vector <int> v1; v1.push_back(x); v1.push_back(y); result.push_back(v1); return true; } if(isSafe(maze, x, y, R, C) == true) { vector <int> v1; v1.push_back(x); v1.push_back(y); result.push_back(v1); maze[x][y] = 2; if(solveMazeUtil(maze, x + 1, y, R, C, result) == true) return true; if (solveMazeUtil(maze, x, y + 1, R, C, result) == true) return true; if(y-1>=0&&x>=0){ if (solveMazeUtil(maze, x, y - 1, R, C, result) == true) return true; } if(x-1>=0&&y>=0){ if (solveMazeUtil(maze, x - 1, y, R, C, result) == true) return true; } result.pop_back(); return false; } return false; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin>>n; cin>>m; cin>>k; int** tab = new int*[n]; for(int i=0; i<n; i++){ tab[i] = new int[m]; } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ tab[i][j] = 1; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ char a; cin>>a; if(a=='X') tab[i][j] = 0; } } int znajomi[k][2]; for(int i=0;i<k;i++){ for(int j=0;j<2;j++){ cin>>znajomi[i][j]; } } vector < vector<int> > wynik = solveMaze(tab, n, m); int gora[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ gora[i][j] = 0; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(j==m-1){ gora[i+1][j] = gora[i][j]+1; continue; } if(i==n-1){ gora[i][j+1] = gora[i][j]+1; continue; } gora[i+1][j] = gora[i][j]+1; gora[i][j+1] = gora[i][j]+1; } } int wgore = 0; int wdol = 0; for(int i=1;i<wynik.size();i++){ if(gora[wynik[i][0]][wynik[i][1]]>gora[wynik[i-1][0]][wynik[i-1][1]]) wgore++; else if(gora[wynik[i][0]][wynik[i][1]]<gora[wynik[i-1][0]][wynik[i-1][1]]) wdol++; } int czasy[k]; int max = 0; for(int i=0;i<k;i++){ czasy[i] = znajomi[i][0]*wgore + znajomi[i][1]*wdol; if(czasy[i]>max) max = czasy[i]; } int min = max; int liczbaosob = 0; for(int i=0;i<k;i++){ if(czasy[i]<min){ min = czasy[i]; liczbaosob = 0; } if(czasy[i]==min) liczbaosob++; } cout<<min<<" "<<liczbaosob; 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #include <iostream> #include <vector> #include<bits/stdc++.h> #include <stdio.h> using namespace std; bool solveMazeUtil( int **maze, int x, int y, int R, int C, vector < vector<int> > &result); bool isSafe(int **maze, int x, int y, int R, int C){ if(x>=0 && x<R && y>=0 && y<C && maze[x][y]==1) { return true; } return false; } vector < vector<int> > solveMaze(int **maze, int R, int C) { vector < vector<int> > result; if(solveMazeUtil(maze, 0, 0, R, C, result) == false){ result.clear(); } return result; } bool solveMazeUtil(int **maze, int x, int y, int R, int C, vector < vector<int> > &result) { if(x==R-1 && y==C-1 && maze[x][y]==1){ vector <int> v1; v1.push_back(x); v1.push_back(y); result.push_back(v1); return true; } if(isSafe(maze, x, y, R, C) == true) { vector <int> v1; v1.push_back(x); v1.push_back(y); result.push_back(v1); maze[x][y] = 2; if(solveMazeUtil(maze, x + 1, y, R, C, result) == true) return true; if (solveMazeUtil(maze, x, y + 1, R, C, result) == true) return true; if(y-1>=0&&x>=0){ if (solveMazeUtil(maze, x, y - 1, R, C, result) == true) return true; } if(x-1>=0&&y>=0){ if (solveMazeUtil(maze, x - 1, y, R, C, result) == true) return true; } result.pop_back(); return false; } return false; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin>>n; cin>>m; cin>>k; int** tab = new int*[n]; for(int i=0; i<n; i++){ tab[i] = new int[m]; } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ tab[i][j] = 1; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ char a; cin>>a; if(a=='X') tab[i][j] = 0; } } int znajomi[k][2]; for(int i=0;i<k;i++){ for(int j=0;j<2;j++){ cin>>znajomi[i][j]; } } vector < vector<int> > wynik = solveMaze(tab, n, m); int gora[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ gora[i][j] = 0; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(j==m-1){ gora[i+1][j] = gora[i][j]+1; continue; } if(i==n-1){ gora[i][j+1] = gora[i][j]+1; continue; } gora[i+1][j] = gora[i][j]+1; gora[i][j+1] = gora[i][j]+1; } } int wgore = 0; int wdol = 0; for(int i=1;i<wynik.size();i++){ if(gora[wynik[i][0]][wynik[i][1]]>gora[wynik[i-1][0]][wynik[i-1][1]]) wgore++; else if(gora[wynik[i][0]][wynik[i][1]]<gora[wynik[i-1][0]][wynik[i-1][1]]) wdol++; } int czasy[k]; int max = 0; for(int i=0;i<k;i++){ czasy[i] = znajomi[i][0]*wgore + znajomi[i][1]*wdol; if(czasy[i]>max) max = czasy[i]; } int min = max; int liczbaosob = 0; for(int i=0;i<k;i++){ if(czasy[i]<min){ min = czasy[i]; liczbaosob = 0; } if(czasy[i]==min) liczbaosob++; } cout<<min<<" "<<liczbaosob; return 0; } |