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
#include <bits/stdc++.h>

using namespace std;

struct Point
{
    int x, y;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int h, w, queries;
    cin >> h >> w >> queries;
    
    vector<vector<string>> s(h, vector<string>(w));
    for (int y = 0; y < h; ++y)
    {
        for (int x = 0; x < w; ++x)
            cin >> s[y][x];
    }
    
    while (queries--)
    {
        int t;
        Point start, end;
        cin >> t >> start.y >> start.x >> end.y >> end.x;
        
        vector<vector<int>> board(h + 1, vector<int>(w + 1, numeric_limits<int>::max()));
        queue<pair<Point, int>> q;
        q.push({start, t});
        
        while (!q.empty())
        {
            auto curr = q.front();
            q.pop();
            
            if (board[curr.first.y][curr.first.x] <= curr.second)
                continue;
            board[curr.first.y][curr.first.x] = curr.second;
            
            if (curr.first.y > 0)
            {
                int nextX = curr.first.x;
                int nextY = curr.first.y - 1;
                
                int rightValidT = numeric_limits<int>::max();
                if (nextX < w)
                {
                    rightValidT = curr.second;
                    while (s[nextY][nextX][rightValidT % s[nextY][nextX].length()] != '0')
                        ++rightValidT;
                }
                int leftValidT = numeric_limits<int>::max();
                if (nextX - 1 >= 0)
                {
                    leftValidT = curr.second;
                    while (s[nextY][nextX - 1][leftValidT % s[nextY][nextX - 1].length()] != '0')
                        ++leftValidT;
                }
                
                int nextT = min(leftValidT, rightValidT);
                q.push({{nextX, nextY}, nextT});
            }
            
            if (curr.first.y < h)
            {
                int nextX = curr.first.x;
                int nextY = curr.first.y + 1;
                
                int rightValidT = numeric_limits<int>::max();
                if (nextX < w)
                {
                    rightValidT = curr.second;
                    while (s[nextY - 1][nextX][rightValidT % s[nextY - 1][nextX].length()] != '0')
                        ++rightValidT;
                }
                int leftValidT = numeric_limits<int>::max();
                if (nextX - 1 >= 0)
                {
                    leftValidT = curr.second;
                    while (s[nextY - 1][nextX - 1][leftValidT % s[nextY - 1][nextX - 1].length()] != '0')
                        ++leftValidT;
                }
                
                int nextT = min(leftValidT, rightValidT);
                q.push({{nextX, nextY}, nextT});
            }
            
            if (curr.first.x > 0)
            {
                int nextX = curr.first.x - 1;
                int nextY = curr.first.y;
                
                int upValidT = numeric_limits<int>::max();
                if (nextY - 1 >= 0)
                {
                    upValidT = curr.second;
                    while (s[nextY - 1][nextX][upValidT % s[nextY - 1][nextX].length()] != '1')
                        ++upValidT;
                }
                int downValidT = numeric_limits<int>::max();
                if (nextY < h)
                {
                    downValidT = curr.second;
                    while (s[nextY][nextX][downValidT % s[nextY][nextX].length()] != '1')
                        ++downValidT;
                }
                int nextT = min(upValidT, downValidT);
                q.push({{nextX, nextY}, nextT});
            }
            
            if (curr.first.x < w)
            {
                int nextX = curr.first.x + 1;
                int nextY = curr.first.y;
                
                int upValidT = numeric_limits<int>::max();
                if (nextY - 1 >= 0)
                {
                    upValidT = curr.second;
                    while (s[nextY - 1][nextX - 1][upValidT % s[nextY - 1][nextX - 1].length()] != '1')
                        ++upValidT;
                }
                int downValidT = numeric_limits<int>::max();
                if (nextY < h)
                {
                    downValidT = curr.second;
                    while (s[nextY][nextX - 1][downValidT % s[nextY][nextX - 1].length()] != '1')
                        ++downValidT;
                }
                int nextT = min(upValidT, downValidT);
                q.push({{nextX, nextY}, nextT});
            }
        }
        
        cout << board[end.y][end.x] << "\n";
    }

    return 0;
}