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
//
//  main.cpp
//  PA_4_B
//
//  Created by Michal Kowalski on 10/12/2021.
//

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <set>
#include <tuple>

int N,M,Q;
int V[15005][15005];
std::vector<std::vector<std::string>> L;

int current_time;
int current_time_mod;
inline bool light_status(int a, int b) {
    std::string str = L[a][b];
    int r = current_time % str.size();
    return str.at(r) - '0';
}

inline bool can_move_N(int a, int b) {
    int i1 = a - 1;
    int j1 = b - 1;
    if (i1 >=0 && i1 < N && j1 >= 0 && j1 < M && !light_status(i1, j1)) return true;
    int i2 = a - 1;
    int j2 = b;
    if (i2 >=0 && i2 < N && j2 >= 0 && j2 < M && !light_status(i2,j2)) return true;
    return false;
}

inline bool can_move_S(int a, int b) {
    int i1 = a;
    int j1 = b - 1;
    if (i1 >=0 && i1 < N && j1 >= 0 && j1 < M && !light_status(i1, j1)) return true;
    int i2 = a;
    int j2 = b;
    if (i2 >=0 && i2 < N && j2 >= 0 && j2 < M && !light_status(i2,j2)) return true;
    return false;
}

inline bool can_move_W(int a, int b) {
    int i1 = a;
    int j1 = b - 1;
    if (i1 >=0 && i1 < N && j1 >= 0 && j1 < M && light_status(i1, j1)) return true;
    int i2 = a - 1;
    int j2 = b - 1;
    if (i2 >=0 && i2 < N && j2 >= 0 && j2 < M && light_status(i2,j2)) return true;
    return false;
}

inline bool can_move_E(int a, int b) {
    int i1 = a;
    int j1 = b;
    if (i1 >=0 && i1 < N && j1 >= 0 && j1 < M && light_status(i1, j1)) return true;
    int i2 = a - 1;
    int j2 = b;
    if (i2 >=0 && i2 < N && j2 >= 0 && j2 < M && light_status(i2,j2)) return true;
    return false;
}

inline bool not_visited(int a, int b, int q) {
    return V[a][b] < q;
}

int main(int argc, const char * argv[]) {
    scanf("%d %d %d",&N,&M,&Q);
    for (int i=0;i<N;++i) {
        std::vector<std::string> lj;
        lj.reserve(M);
        for (int j=0;j<M;++j) {
            char s[10];
            scanf("%s",&s[0]);
            std::string str = s;
            lj.push_back(str);
        }
        L.push_back(lj);
    }
    int q = 0;
    for (q=1;q<=Q;q++) {
        int t,a,b,c,d;
        scanf("%d %d %d %d %d",&t,&a,&b,&c,&d);
        std::pair<int, int> start = {a,b};
        std::pair<int, int> stop = {c,d};
        current_time = t;
        current_time_mod = t % 10;
        if (a == c && b == d) {
            printf("%d\n",current_time);
            continue;
        }
        std::vector<std::tuple<int, int, char, int>> Q;
        if (a>0) Q.push_back(std::make_tuple(a,b,'N',0));
        if (a<N) Q.push_back(std::make_tuple(a,b,'S',0));
        if (b>0) Q.push_back(std::make_tuple(a,b,'W',0));
        if (b<M) Q.push_back(std::make_tuple(a,b,'E',0));
        V[a][b] = q;
        bool found = false;
        while(true) {
            std::vector<std::tuple<int, int, char, int>> tQ;
            tQ.reserve(32);
            tQ.insert(tQ.begin(), Q.begin(), Q.end());
            Q.clear();
            while(!tQ.empty()) {
                auto t = tQ.back();tQ.pop_back();
                int a = std::get<0>(t);
                int b = std::get<1>(t);
                char d = std::get<2>(t);
                if (d == 'N' && not_visited(a - 1, b, q) && can_move_N(a, b)) {
                    int i = a - 1;
                    int j = b;
                    V[i][j] = q;
                    if (i == stop.first && j == stop.second) {
                        found = true;break;
                    } else {
                        if (i>0) tQ.push_back(std::make_tuple(i,j,'N',0));
                        //if (i<N) tQ.push_back(std::make_tuple(i,j,'S',0));
                        if (j>0) tQ.push_back(std::make_tuple(i,j,'W',0));
                        if (j<M) tQ.push_back(std::make_tuple(i,j,'E',0));
                    }
                } else if ( d == 'S' && not_visited(a + 1, b, q) && can_move_S(a, b)) {
                    int i = a + 1;
                    int j = b;
                    V[i][j] = q;
                    if (i == stop.first && j == stop.second) {
                        found = true;break;
                    } else {
                        //if (i>0) tQ.push_back(std::make_tuple(i,j,'N',0));
                        if (i<N) tQ.push_back(std::make_tuple(i,j,'S',0));
                        if (j>0) tQ.push_back(std::make_tuple(i,j,'W',0));
                        if (j<M) tQ.push_back(std::make_tuple(i,j,'E',0));
                    }
                } else if ( d == 'W' && not_visited(a, b - 1, q) && can_move_W(a, b)) {
                    int i = a;
                    int j = b - 1;
                    V[i][j] = q;
                    if (i == stop.first && j == stop.second) {
                        found = true;break;
                    } else {
                        if (i>0) tQ.push_back(std::make_tuple(i,j,'N',0));
                        if (i<N) tQ.push_back(std::make_tuple(i,j,'S',0));
                        if (j>0) tQ.push_back(std::make_tuple(i,j,'W',0));
                        //if (j<M) tQ.push_back(std::make_tuple(i,j,'E',0));
                    }
                } else if ( d == 'E' && not_visited(a, b + 1, q) && can_move_E(a, b)) {
                    int i = a;
                    int j = b + 1;
                    V[i][j] = q;
                    if (i == stop.first && j == stop.second) {
                        found = true;break;
                    } else {
                        if (i>0) tQ.push_back(std::make_tuple(i,j,'N',0));
                        if (i<N) tQ.push_back(std::make_tuple(i,j,'S',0));
                        //if (j>0) tQ.push_back(std::make_tuple(i,j,'W',0));
                        if (j<M) tQ.push_back(std::make_tuple(i,j,'E',0));
                    }
                } else {
                    Q.push_back(t);
                }
            }
            if (found) {
                printf("%d\n",current_time);
                break;
            }
            ++current_time;
            current_time_mod = current_time % 10;
        }
    }
    return 0;
}