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

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int n, m, q;
	cin >> n >> m >> q;
	string a[n][m];
	
	for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) cin >> a[i][j];
	
	for(int o = 0; o < q; ++o)
	{
		int t, i1, j1, i2, j2;
		cin >> t >> i1 >> j1 >> i2 >> j2;
		
		int od[n+1][m+1];
		for(int i = 0; i <= n; ++i) for(int j = 0; j <= m; ++j) od[i][j] = INT_MAX;
		priority_queue <pair <int, pair <int, int>>> q;
		od[i1][j1] = t;
		q.push({-t, {i1, j1}});
		
		while(q.size())
		{
			int ti = q.top().second.first, tj = q.top().second.second;
			int tt = -q.top().first;
			q.pop();
			
			//cout << ti << " " << tj << " " << tt << "\n";
			
			if(ti == i2 && tj == j2)
			{
				cout << tt << "\n";
				break;
			}
			
			if(ti-1 >= 0)
			{
				int mint1 = INT_MAX, mint2 = INT_MAX;
				if(tj-1 >= 0 && ti-1 < n && tj-1 < m) for(mint1 = tt; a[ti-1][tj-1][mint1%a[ti-1][tj-1].length()] == '1'; ++mint1) {}
				if(ti-1 < n && tj < m) for(mint2 = tt; a[ti-1][tj][mint2%a[ti-1][tj].length()] == '1'; ++mint2) {}
				
				if(min(mint1, mint2) < od[ti-1][tj])
				{
					od[ti-1][tj] = min(mint1, mint2);
					q.push({-min(mint1, mint2), {ti-1, tj}});
					//cout << "1";
				}
			}
			//cout << "a";
			
			if(ti+1 <= n)
			{
				int mint1 = INT_MAX, mint2 = INT_MAX;
				if(ti < n && tj-1 >= 0 && tj-1 < m) for(mint1 = tt; a[ti][tj-1][mint1%a[ti][tj-1].length()] == '1'; ++mint1) {}
				if(ti < n && tj < m) for(mint2 = tt; a[ti][tj][mint2%a[ti][tj].length()] == '1'; ++mint2) {}
				
				if(min(mint1, mint2) < od[ti+1][tj])
				{
					od[ti+1][tj] = min(mint1, mint2);
					q.push({-min(mint1, mint2), {ti+1, tj}});
					//cout << "2";
				}
			}
			//cout << "b";
			
			if(tj-1 >= 0)
			{
				int mint1 = INT_MAX, mint2 = INT_MAX;
				if(ti-1 >= 0 && ti-1 < n && tj-1 < m) for(mint1 = tt; a[ti-1][tj-1][mint1%a[ti-1][tj-1].length()] == '0'; ++mint1) {}
				if(ti < n && tj-1 < m) for(mint2 = tt; a[ti][tj-1][mint2%a[ti][tj-1].length()] == '0'; ++mint2) {}
				
				if(min(mint1, mint2) < od[ti][tj-1])
				{
					od[ti][tj-1] = min(mint1, mint2);
					q.push({-min(mint1, mint2), {ti, tj-1}});
					//cout << "3";
				}
			}
			//cout << "c";
			
			if(tj+1 <= m)
			{
				int mint1 = INT_MAX, mint2 = INT_MAX;
				if(ti-1 >= 0 && tj < m) for(mint1 = tt; a[ti-1][tj][mint1%a[ti-1][tj].length()] == '0'; ++mint1) {}
				if(ti < n && tj < m) for(mint2 = tt; a[ti][tj][mint2%a[ti][tj].length()] == '0'; ++mint2) {}
				
				if(min(mint1, mint2) < od[ti][tj+1])
				{
					od[ti][tj+1] = min(mint1, mint2);
					q.push({-min(mint1, mint2), {ti, tj+1}});
					//cout << "4";
				}
			}
			//cout << "d";
			
			//cout << "\n";
		}
	}
	
	return 0;
}