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
#include <iostream>

int MAX=2002;
using namespace std;



int main()
{
    int n,m,k;
    int s=0;
    int **tab;
    int f,i,j,x;
    char c;


    tab = new int*[MAX];
    for(i = 0; i < MAX; ++i)
    tab[i] = new int[MAX];

    scanf("%d%d%d",&n,&m,&k);
    for(i=0;i<MAX;i++)
        for(j=0;j<=MAX;j++)
            tab[i][j]=1000000;

    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
            scanf(" %c",&c);
            if(c=='.') tab[i][j]=10000;
        }
    tab[1][1]=0;
    do{
        f=0;
        for(i=1;i<=n+1;i++)
            for(j=1;j<=m+1;j++)
            {
                if(tab[i][j]==1000000) continue;
                x=tab[i-1][j]+1;
                if(x<tab[i][j])
                {
                    tab[i][j]=x;
                    f=1;
                }
                x=tab[i+1][j]+1;
                if(x<tab[i][j])
                {
                    tab[i][j]=x;
                    f=1;
                }
                x=tab[i][j-1]+1;
                if(x<tab[i][j])
                {
                    tab[i][j]=x;
                    f=1;
                }
                x=tab[i][j+1]+1;
                if(x<tab[i][j])
                {
                    tab[i][j]=x;
                    f=1;
                }
            }
    }while(f==1);
    s=tab[n][m];

    x=1000000;
    f=0;
    int a,b;
    for(i=0;i<k;i++)
    {
        scanf("%d%d",&a,&b);
        j=a*s+b*(s+2-n-m);
        if (j==x)f++;
        if(j<x) {x=j;f=1;}
    }
    printf("%d %d\n",x,f);
    return 0;
}