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
172
173
174
//11.12.20 21:39 start //11.12.20 23:38 koniec
#include <iostream>
using namespace std;

bool **tab;
bool **visited;
int n,m,k;
unsigned int max_steps = 0 - 1;
unsigned int max_steps_global = 0 - 1;
unsigned int people_in_end;

class peoples
{
public:
int L;
int P;	
};//poples;



void remove_blind(int npos, int mpos)
{
bool gone_nowhere = true;
visited[npos][mpos] = true;
if(npos == n-1 && mpos == m-1){
	return;
	}//if
//go_right
if(mpos + 1 < m && !visited[npos][mpos+1] && tab[npos][mpos+1]){
	remove_blind(npos,mpos+1);
	gone_nowhere = false;
	}//if
//go_left
if(mpos - 1 >= 0 && !visited[npos][mpos-1] && tab[npos][mpos-1]){
	remove_blind(npos,mpos-1);
	gone_nowhere = false;
	}//if
//go_down
if(npos + 1 < n && !visited[npos+1][mpos] && tab[npos+1][mpos]){
	remove_blind(npos+1,mpos);
	gone_nowhere = false;
	}//if
//go_up
if(npos - 1 >= 0 && !visited[npos-1][mpos] && tab[npos-1][mpos]){
	remove_blind(npos-1,mpos);
	gone_nowhere = false;
	}//if
if(gone_nowhere){
	tab[npos][mpos] = false;
	}//if
}//remove_blind



void do_step(peoples &people, int npos, int mpos, int step, int step2)
{
visited[npos][mpos] = true;
if(step2 > (n + m) * 100) return;  
if(step > max_steps || step > max_steps_global) return;
if(npos == n-1 && mpos == m-1){
	if(step < max_steps){
		max_steps = step;
		}//if2
	}//if
//go_right
if(mpos + 1 < m && !visited[npos][mpos+1] && tab[npos][mpos+1]){
	do_step(people,npos,mpos+1, step + people.L,step2+1);
	}//if
//go_left
if(mpos - 1 >= 0 && !visited[npos][mpos-1] && tab[npos][mpos-1]){
	do_step(people,npos,mpos-1, step + people.P,step2+1);
	}//if
//go_down
if(npos + 1 < n && !visited[npos+1][mpos] && tab[npos+1][mpos]){
	do_step(people,npos+1,mpos, step + people.L,step2+1);
	}//if
//go_up
if(npos - 1 >= 0 && !visited[npos-1][mpos] && tab[npos-1][mpos]){
	do_step(people,npos-1,mpos, step + people.P,step2+1);
	}//if
}//do_step

void set_track(peoples &people)
{
visited[0][0] = true;	

if(m>=1 && tab[0][1]){
	do_step(people,0,1,people.L,1);
	}//if
if(n>=1 && tab[1][0]){
	do_step(people,1,0,people.L,1);
	}//if

return;
}//set track


int main()
{
peoples *people;
char sig;
string stri;
int i,j,l;
cin>>n>>m>>k;
people = new peoples[k];
tab = new bool*[n];
visited = new bool*[n];	
for(i=0;i<n;++i){
	tab[i] = new bool[m];
	visited[i] = new bool[m];
	}//for

for(i=0;i<n;++i){
	cin>>stri;
	for(j=0;j<m;++j){
		if(stri[j] == '.') tab[i][j] = true;
		else tab[i][j] = false; 
		visited[i][j] = false;
		}//for
	}//for
/*
//print out the track begin
cout<<endl<<endl;
for(i=0;i<n;++i){
	for(j=0;j<m;++j){
		if(tab[i][j]) cout<<".";
		else cout<<"X";
		}//for
	cout<<endl;
	}//for
cout<<endl<<endl;
//print out the track end
*/

for(i=0;i<k;++i){
	cin>>people[i].L;
	cin>>people[i].P;
	}//for
	
remove_blind(0,0);
	for(j=0;j<n;++j){
		for(l=0;l<m;++l){
			visited[j][l] = false;
			}//for2
		
		}//for
		
for(i=0;i<k;++i){
	set_track(people[i]);
	if(max_steps < max_steps_global){
		people_in_end = 1;
		max_steps_global = max_steps;
		}//if
	else if(max_steps == max_steps_global){
		++people_in_end;
		}//if
	//cout<<"Dla k = "<<i<<" max_steps wynosi "<<max_steps<<" a global "<<max_steps_global<<endl;
	max_steps = 0-1;
	for(j=0;j<n;++j){
		for(l=0;l<m;++l){
			visited[j][l] = false;
			}//for2
		
		}//for
	}//for
cout<<max_steps_global<<" "<<people_in_end<<endl;

for(i=0;i<n;++i){
	delete tab[i];
	}//for
delete [] tab;
delete [] people;	
return 0;	
}