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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* 2021
 * Maciej Szeptuch
 */
#include <algorithm>
#include <cstdio>
#include <string>
#include <unordered_set>
#include <vector>

int rows;
int cols;
int queries;
std::vector<std::vector<std::string>> crossing;
bool seen[8];
int time_loop = 1;
int results;

struct Query
{
    struct
    {
        int start;
        int end;
    } time;

    struct
    {
        int r;
        int c;
    } start;

    struct
    {
        int r;
        int c;
    } end;
};

std::vector<Query> query;
std::vector<int> waiting;
std::vector<std::unordered_set<int>> pedestrian[2];
std::vector<std::vector<int>> square_to_group[2];
int groups[2];
std::vector<std::unordered_set<int>> next_groups;
std::vector<int> rank[2];

void calculate_grouping(int time);

int main(void)
{
    scanf("%d %d %d\n", &rows, &cols, &queries);
    ++rows;
    ++cols;
    query.resize(queries);
    square_to_group[0].resize(rows);
    square_to_group[1].resize(rows);
    rank[0].resize(rows * cols);
    rank[1].resize(rows * cols);
    for(int r = 0; r < rows; ++r)
    {
        square_to_group[0][r].resize(cols);
        square_to_group[1][r].resize(cols);
    }

    waiting.resize(queries);
    crossing.resize(rows - 1);
    for(int r = 0; r < rows - 1; ++r)
    {
        crossing[r].resize(cols - 1);
        for(int c = 0; c < cols - 1; ++c)
        {
            char temp[9];
            scanf("%s", temp);
            crossing[r][c] = temp;
            int len = crossing[r][c].size();
            if(!seen[len])
            {
                time_loop *= len / std::__gcd(time_loop, len);
                seen[len] = true;
            }
        }
    }

    for(int q = 0; q < queries; ++q)
    {
        scanf("%d %d %d %d %d", &query[q].time.start, &query[q].start.r, &query[q].start.c, &query[q].end.r, &query[q].end.c);
        query[q].time.end = -1;
        waiting[q] = q;
    }

    std::sort(std::begin(waiting), std::end(waiting), [&](auto a, auto b)
        { return query[a].time.start % time_loop == query[b].time.start % time_loop ? a < b : query[a].time.start % time_loop < query[b].time.start % time_loop; });

    calculate_grouping(0);
    for(int w = 0, time = 0; results < queries; ++time)
    {
        int t = time % 2;
        while(w < queries && query[waiting[w]].time.start % time_loop == time)
        {
            int q = waiting[w];
            pedestrian[t][square_to_group[t][query[q].start.r][query[q].start.c]].insert(q);
            ++w;
        }

        for(int g = 0; g < groups[t]; ++g)
            for(auto &q: pedestrian[t][g])
                if(query[q].time.end == -1 && square_to_group[t][query[q].end.r][query[q].end.c] == g)
                {
                    query[q].time.end = time;
                    ++results;
                }

        calculate_grouping(time + 1);

        next_groups.resize(groups[t]);
        for(int g = 0; g < groups[t]; ++g)
            next_groups[g] = {};

        for(int r = 0; r < rows; ++r)
            for(int c = 0; c < cols; ++c)
                next_groups[square_to_group[t][r][c]].insert(square_to_group[!t][r][c]);

        for(int g = 0; g < groups[t]; ++g)
            for(auto &next_group: next_groups[g])
                for(auto &q: pedestrian[t][g])
                {
                    if(query[q].time.end != -1)
                        continue;

                    pedestrian[!t][next_group].insert(q);
                }
    }

    for(int q = 0; q < queries; ++q)
        printf("%d\n", query[q].time.start + (query[q].time.end - query[q].time.start % time_loop));

    return 0;
}

int find(int t, int r, int c)
{
    int id = square_to_group[t][r][c];
    if(id == r * cols + c)
        return id;

    return square_to_group[t][r][c] = find(t, id / cols, id % cols);
}

void union_(int t, int r1, int c1, int r2, int c2)
{
    int id1 = find(t, r1, c1);
    int id2 = find(t, r2, c2);

    if(rank[t][id1] > rank[t][id2])
        square_to_group[t][id2 / cols][id2 % cols] = id1;
    else if(rank[t][id1] < rank[t][id2])
        square_to_group[t][id1 / cols][id1 % cols] = id2;
    else
    {
        square_to_group[t][id2 / cols][id2 % cols] = id1;
        rank[t][id1] += 1;
    }
}

void calculate_grouping(int time)
{
    int t = time % 2;
    for(int r = 0; r < rows; ++r)
        for(int c = 0; c < cols; ++c)
        {
            square_to_group[t][r][c] = r * cols + c;
            rank[t][r * cols + c] = 0;
        }

    for(int r = 0; r < rows; ++r)
        for(int c = 0; c < cols; ++c)
        {
            if(r > 0)
            {
                if(c > 0)
                {
                    // Nie patrze wstecz
                }

                if(c < cols - 1)
                {
                    if(crossing[r - 1][c][time % crossing[r - 1][c].size()] == '1')
                        union_(t, r, c, r, c + 1);
                }
            }

            if(r < rows - 1)
            {
                if(c > 0)
                {
                    if(crossing[r][c - 1][time % crossing[r][c - 1].size()] == '0')
                        union_(t, r, c, r + 1, c);
                }

                if(c < cols - 1)
                {
                    if(crossing[r][c][time % crossing[r][c].size()] == '0')
                        union_(t, r, c, r + 1, c);
                    else
                        union_(t, r, c, r, c + 1);
                }
            }
        }

    groups[t] = 0;
    std::unordered_map<int, int> used_group;

    for(int r = 0; r < rows; ++r)
        for(int c = 0; c < cols; ++c)
        {
            int group_id = find(t, r, c);
            if(!used_group.count(group_id))
                used_group[group_id] = groups[t]++;
        }

    for(int r = 0; r < rows; ++r)
        for(int c = 0; c < cols; ++c)
            square_to_group[t][r][c] = used_group[square_to_group[t][r][c]];

    pedestrian[t].resize(groups[t]);
    for(int g = 0; g < groups[t]; ++g)
        pedestrian[t][g] = {};
}