#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 2020 typedef long long ll; typedef struct Point { int r, c; } TPoint; TPoint bfs[MAX*MAX]; char M[MAX][MAX]; char V[MAX][MAX]; int R, C, k; int dxy[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}}; int main() { memset(M, 0, sizeof(M)); memset(V, -1, sizeof(V)); scanf("%d %d %d", &R, &C, &k); for (int r=1; r<=R; r++) { scanf("%s", &M[r][1]); for (int c=1; c<=C; c++) { M[r][c] = (M[r][c]=='.'); } } int qb = 1, qe = 2; bfs[1].r = bfs[1].c = 1; V[1][1] = 0; while (qb < qe && V[R][C] == -1) { int r = bfs[qb].r; int c = bfs[qb].c; qb++; for (int i=0; i<4; i++) { int nr = r+dxy[i][0]; int nc = c+dxy[i][1]; if (!M[nr][nc]) continue; if (V[nr][nc] != -1) continue; bfs[qe].r = nr; bfs[qe].c = nc; qe ++; V[nr][nc] = i; } } ll A=0, B=0; int r=R, c=C; while (r!=1 || c!=1) { int i = V[r][c]; if (i < 2) A++; else B++; r = r-dxy[i][0]; c = c-dxy[i][1]; } ll ile=0, min=1e18; for (int i=0; i<k; i++) { ll a, b; scanf("%lld %lld", &a, &b); if (A*a + B*b < min) { min = A*a + B*b; ile = 1; } else if (A*a + B*b == min) { ile ++; } } printf("%lld %lld\n", min, 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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 2020 typedef long long ll; typedef struct Point { int r, c; } TPoint; TPoint bfs[MAX*MAX]; char M[MAX][MAX]; char V[MAX][MAX]; int R, C, k; int dxy[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}}; int main() { memset(M, 0, sizeof(M)); memset(V, -1, sizeof(V)); scanf("%d %d %d", &R, &C, &k); for (int r=1; r<=R; r++) { scanf("%s", &M[r][1]); for (int c=1; c<=C; c++) { M[r][c] = (M[r][c]=='.'); } } int qb = 1, qe = 2; bfs[1].r = bfs[1].c = 1; V[1][1] = 0; while (qb < qe && V[R][C] == -1) { int r = bfs[qb].r; int c = bfs[qb].c; qb++; for (int i=0; i<4; i++) { int nr = r+dxy[i][0]; int nc = c+dxy[i][1]; if (!M[nr][nc]) continue; if (V[nr][nc] != -1) continue; bfs[qe].r = nr; bfs[qe].c = nc; qe ++; V[nr][nc] = i; } } ll A=0, B=0; int r=R, c=C; while (r!=1 || c!=1) { int i = V[r][c]; if (i < 2) A++; else B++; r = r-dxy[i][0]; c = c-dxy[i][1]; } ll ile=0, min=1e18; for (int i=0; i<k; i++) { ll a, b; scanf("%lld %lld", &a, &b); if (A*a + B*b < min) { min = A*a + B*b; ile = 1; } else if (A*a + B*b == min) { ile ++; } } printf("%lld %lld\n", min, ile); return 0; } |