#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m, q;
vector<vector<string> > s;
int t, a, b, c, d;
const int duzo=INT_MAX;
vector<int> ans[15009];
int f(int t, int a, int b, bool zera){
int res = t;
if(a >= 0 && a <n && b >= 0 && b <m){
for(unsigned i=t; i<t+s[a][b].size(); i++){
char c = s[a][b][i%s[a][b].size()];
if((!zera && c!='0') || (zera && c=='0')){
res++;
}else{
break;
}
}
}else{
return duzo;
}
return res;
}
//gora, dol, lewo, prawo
const int tab[8][5] = {{-1, -1, 0, -1, 0},{-1, 0, 0, -1, 0},{0, -1, 0, 1, 0},{0, 0, 0, 1, 0},{-1, -1, 1, 0, -1},{0, -1, 1, 0, -1},{-1, 0, 1, 0, 1},{0, 0, 1, 0, 1}};
int solve(){
for(int i=0; i<=n; i++){
for(int j=0; j<=m; j++){
ans[i].push_back(duzo);
}
}
ans[a][b]=t;
priority_queue<pair<int,pair<int, int> > > Q;
Q.push(make_pair(-t, make_pair(a, b)));
int r[2];
while(!Q.empty()){
auto q = Q.top();
Q.pop();
int A = q.second.first;
int B = q.second.second;
for(int i=0; i<8; i++){
r[i%2] = f(-q.first, A+tab[i][0], B+tab[i][1], tab[i][2]);
if(i%2){
if(r[0] > r[1]){
swap(r[0], r[1]);
}
if(r[0] != duzo && ans[A+tab[i][3]][B+tab[i][4]] > r[0]){
ans[A+tab[i][3]][B+tab[i][4]] = r[0];
Q.push(make_pair(-ans[A+tab[i][3]][B+tab[i][4]], make_pair(A+tab[i][3], B+tab[i][4])));
}
}
}
}
int result = ans[c][d];
for(int i=0; i<=n; i++){
ans[i].clear();
}
return result;
}
int main(){
ios_base::sync_with_stdio(false);
cin>>n>>m>>q;
for(int i=0; i<n; i++){
vector<string> tmp;
for(int j=0; j<m; j++){
string str;
cin>>str;
tmp.push_back(str);
}
s.push_back(tmp);
}
for(int i=0; i<q; i++){
cin>>t>>a>>b>>c>>d;
cout<<solve()<<"\n";
}
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 <bits/stdc++.h> using namespace std; typedef long long LL; int n, m, q; vector<vector<string> > s; int t, a, b, c, d; const int duzo=INT_MAX; vector<int> ans[15009]; int f(int t, int a, int b, bool zera){ int res = t; if(a >= 0 && a <n && b >= 0 && b <m){ for(unsigned i=t; i<t+s[a][b].size(); i++){ char c = s[a][b][i%s[a][b].size()]; if((!zera && c!='0') || (zera && c=='0')){ res++; }else{ break; } } }else{ return duzo; } return res; } //gora, dol, lewo, prawo const int tab[8][5] = {{-1, -1, 0, -1, 0},{-1, 0, 0, -1, 0},{0, -1, 0, 1, 0},{0, 0, 0, 1, 0},{-1, -1, 1, 0, -1},{0, -1, 1, 0, -1},{-1, 0, 1, 0, 1},{0, 0, 1, 0, 1}}; int solve(){ for(int i=0; i<=n; i++){ for(int j=0; j<=m; j++){ ans[i].push_back(duzo); } } ans[a][b]=t; priority_queue<pair<int,pair<int, int> > > Q; Q.push(make_pair(-t, make_pair(a, b))); int r[2]; while(!Q.empty()){ auto q = Q.top(); Q.pop(); int A = q.second.first; int B = q.second.second; for(int i=0; i<8; i++){ r[i%2] = f(-q.first, A+tab[i][0], B+tab[i][1], tab[i][2]); if(i%2){ if(r[0] > r[1]){ swap(r[0], r[1]); } if(r[0] != duzo && ans[A+tab[i][3]][B+tab[i][4]] > r[0]){ ans[A+tab[i][3]][B+tab[i][4]] = r[0]; Q.push(make_pair(-ans[A+tab[i][3]][B+tab[i][4]], make_pair(A+tab[i][3], B+tab[i][4]))); } } } } int result = ans[c][d]; for(int i=0; i<=n; i++){ ans[i].clear(); } return result; } int main(){ ios_base::sync_with_stdio(false); cin>>n>>m>>q; for(int i=0; i<n; i++){ vector<string> tmp; for(int j=0; j<m; j++){ string str; cin>>str; tmp.push_back(str); } s.push_back(tmp); } for(int i=0; i<q; i++){ cin>>t>>a>>b>>c>>d; cout<<solve()<<"\n"; } return 0; } |
English