#include <iostream> #include <vector> #include <bitset> using namespace std; int n, m; vector< vector<int> > graph[840]; bitset<1010000> visited; bitset<1010000> visitedNow; void DFS(int pos, int time) { for(int i = 0; i < graph[time][pos].size(); i++) if(!visitedNow[graph[time][pos][i]]) { visitedNow[graph[time][pos][i]] = 1; visited[graph[time][pos][i]] = 1; DFS(graph[time][pos][i], time); } } int calculate_time(int start, int ending, int time) { visited.reset(); visited[start] = 1; while (true) { visitedNow.reset(); for(int i = 1; i <= n * m; i++) if(visited[i] && !visitedNow[i]) { DFS(i, time % 840); } if(visited[ending]) return time; time++; } } void solve() { int q, plac, tmp, time, x1, y1, x2, y2, start, ending, ending_time; 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]; tmp = (n+1)*(m+1)+5; for(int i = 0; i < 840; i++) graph[i].resize(tmp); for(int t = 0; t < 840; t++) { for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { plac = (m + 1) * i + j + 1; if(a[i][j][t%a[i][j].size()] == '0') { // pion graph[t][plac].push_back(plac + m+1); graph[t][plac+m+1].push_back(plac); graph[t][plac+1].push_back(plac + m+2); graph[t][plac+m+2].push_back(plac+1); } else { // poziom graph[t][plac].push_back(plac + 1); graph[t][plac+1].push_back(plac); graph[t][plac+m+1].push_back(plac + 2+m); graph[t][plac+2+m].push_back(plac+m+1); // m and n are minus 1 } } } n++; m++; for(int i = 0; i < q; i++) { cin >> time >> y1 >> x1 >> y2 >> x2; start = y1 * n + x1 + 1; ending = y2 * n + x2 + 1; ending_time = calculate_time(start, ending, time); cout << ending_time << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); 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 86 87 88 89 90 91 92 93 94 | #include <iostream> #include <vector> #include <bitset> using namespace std; int n, m; vector< vector<int> > graph[840]; bitset<1010000> visited; bitset<1010000> visitedNow; void DFS(int pos, int time) { for(int i = 0; i < graph[time][pos].size(); i++) if(!visitedNow[graph[time][pos][i]]) { visitedNow[graph[time][pos][i]] = 1; visited[graph[time][pos][i]] = 1; DFS(graph[time][pos][i], time); } } int calculate_time(int start, int ending, int time) { visited.reset(); visited[start] = 1; while (true) { visitedNow.reset(); for(int i = 1; i <= n * m; i++) if(visited[i] && !visitedNow[i]) { DFS(i, time % 840); } if(visited[ending]) return time; time++; } } void solve() { int q, plac, tmp, time, x1, y1, x2, y2, start, ending, ending_time; 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]; tmp = (n+1)*(m+1)+5; for(int i = 0; i < 840; i++) graph[i].resize(tmp); for(int t = 0; t < 840; t++) { for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { plac = (m + 1) * i + j + 1; if(a[i][j][t%a[i][j].size()] == '0') { // pion graph[t][plac].push_back(plac + m+1); graph[t][plac+m+1].push_back(plac); graph[t][plac+1].push_back(plac + m+2); graph[t][plac+m+2].push_back(plac+1); } else { // poziom graph[t][plac].push_back(plac + 1); graph[t][plac+1].push_back(plac); graph[t][plac+m+1].push_back(plac + 2+m); graph[t][plac+2+m].push_back(plac+m+1); // m and n are minus 1 } } } n++; m++; for(int i = 0; i < q; i++) { cin >> time >> y1 >> x1 >> y2 >> x2; start = y1 * n + x1 + 1; ending = y2 * n + x2 + 1; ending_time = calculate_time(start, ending, time); cout << ending_time << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } |