|
| 1 | +/* |
| 2 | + |
| 3 | + Name: Mehul Chaturvedi |
| 4 | + IIT-Guwahati |
| 5 | + |
| 6 | +*/ |
| 7 | +/* |
| 8 | +You all must be familiar with the chess-board having |
| 9 | +8*8 squares of alternate black and white cells. Well, here |
| 10 | +we have for you a similar |
| 11 | +N*M size board with similar arrangement of black and white |
| 12 | +cells. |
| 13 | +A few of these cells have Horses placed over them. |
| 14 | +Each horse is unique. Now these horses are not the |
| 15 | +usual horses which could jump to any of the |
| 16 | +8 positions they usually jump in. They can move only if there is another horse on one of the 8-positions that it can go to usually and then both the horses will swap their positions. This swapping can happen infinitely times. |
| 17 | +A photographer was assigned to take a picture of all the different ways that the horses occupy the board! Given the state of the board, calculate answer. Sincethis answer may be quite large, calculate in modulo |
| 18 | +10^9+7 |
| 19 | +Input: |
| 20 | +First line contains |
| 21 | +T which is the number of test cases. |
| 22 | +T test cases follow first line of each containing three integers |
| 23 | +N, M and Q where |
| 24 | +N,M is the size of the board and |
| 25 | +Q is the number of horses on it. |
| 26 | + |
| 27 | +Q lines follow each containing the 2 integers |
| 28 | +X and Y which are the coordinates of the Horses. |
| 29 | +Output: |
| 30 | +For each test case, output the number of photographs taken by photographer. |
| 31 | +Constraints: |
| 32 | + 1<=T<=10 |
| 33 | + 1<=N,M<=1000 |
| 34 | + 1<=Q<=N*M |
| 35 | +SAMPLE INPUT |
| 36 | +2 |
| 37 | +4 4 4 |
| 38 | +1 1 |
| 39 | +1 2 |
| 40 | +3 1 |
| 41 | +3 2 |
| 42 | +4 4 4 |
| 43 | +1 1 |
| 44 | +1 2 |
| 45 | +3 1 |
| 46 | +4 4 |
| 47 | +SAMPLE OUTPUT |
| 48 | +4 |
| 49 | +2 |
| 50 | +*/ |
| 51 | + |
| 52 | + |
| 53 | +#include <bits/stdc++.h> |
| 54 | + |
| 55 | +using namespace std; |
| 56 | +#define pii pair<ll,ll> |
| 57 | +#define MOD 1000000007 |
| 58 | +#define ll long long |
| 59 | + |
| 60 | +ll dfs(ll i, ll j, vector<pii>** graph, ll** visited){ |
| 61 | + visited[i][j] = 1; |
| 62 | + ll answer = 1; |
| 63 | + |
| 64 | + for(ll k = 0; k < graph[i][j].size(); k++){ |
| 65 | + ll x = graph[i][j][k].first; |
| 66 | + ll y = graph[i][j][k].second; |
| 67 | + if(!visited[x][y]){ |
| 68 | + answer = (answer+dfs(x, y, graph, visited))%MOD; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return answer; |
| 73 | + |
| 74 | + // ll count = 0; |
| 75 | + // for (ll i = 0; i < graph[sx][sy].size(); ++i) |
| 76 | + // { |
| 77 | + // ll left = (graph[sx][sy].at(i)).first; |
| 78 | + // ll right = (graph[sx][sy].at(i)).second; |
| 79 | + // if (visited[left][right] == 0) |
| 80 | + // { |
| 81 | + // visited[left][right] = 1; |
| 82 | + // count = (count+1)%MOD; |
| 83 | + // } |
| 84 | + |
| 85 | + // } |
| 86 | + |
| 87 | + // return count%MOD; |
| 88 | +} |
| 89 | + |
| 90 | +void fill_vector(vector<pii>** graph, ll** board, ll m, ll n){ |
| 91 | + for (ll i = 0; i < n; ++i) |
| 92 | + { |
| 93 | + for (ll j = 0; j < m; ++j) |
| 94 | + { |
| 95 | + if (board[i][j] == 1) |
| 96 | + { |
| 97 | + graph[i][j].push_back(make_pair(i, j)); |
| 98 | + |
| 99 | + if (i+2<n && j+1<m && board[i+2][j+1] == 1) |
| 100 | + { |
| 101 | + //return ; |
| 102 | + graph[i][j].push_back(make_pair(i+2, j+1)); |
| 103 | + } |
| 104 | + //return ; |
| 105 | + |
| 106 | + |
| 107 | + if (i+2<n && j-1>=0 && board[i+2][j-1] == 1) |
| 108 | + { |
| 109 | + graph[i][j].push_back(make_pair(i+2, j-1)); |
| 110 | + } |
| 111 | + |
| 112 | + if (i-2>=0 && j+1<m && board[i-2][j+1] == 1) |
| 113 | + { |
| 114 | + graph[i][j].push_back(make_pair(i-2, j+1)); |
| 115 | + } |
| 116 | + |
| 117 | + if (i-2>=0 && j-1>=0 && board[i-2][j-1] == 1) |
| 118 | + { |
| 119 | + graph[i][j].push_back(make_pair(i-2, j-1)); |
| 120 | + } |
| 121 | + |
| 122 | + if (i+1<n && j+2<m && board[i+1][j+2] == 1) |
| 123 | + { |
| 124 | + graph[i][j].push_back(make_pair(i+1, j+2)); |
| 125 | + } |
| 126 | + |
| 127 | + if (i+1<n && j-2>=0 && board[i+1][j-2] == 1) |
| 128 | + { |
| 129 | + graph[i][j].push_back(make_pair(i+1, j-2)); |
| 130 | + } |
| 131 | + |
| 132 | + if (i-1>=0 && j+2<m && board[i-1][j+2] == 1) |
| 133 | + { |
| 134 | + graph[i][j].push_back(make_pair(i-1, j+2)); |
| 135 | + } |
| 136 | + |
| 137 | + if (i-1>=0 && j-2>=0 && board[i-1][j-2] == 1) |
| 138 | + { |
| 139 | + graph[i][j].push_back(make_pair(i-1, j-2)); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return; |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +int main( int argc , char ** argv ) |
| 150 | +{ |
| 151 | + |
| 152 | + ios_base::sync_with_stdio(false) ; |
| 153 | + cin.tie(NULL) ; |
| 154 | + |
| 155 | + //To get the factorial |
| 156 | + ll *factorial = new ll[1000000]; |
| 157 | + factorial[1] = 1; |
| 158 | + for(ll i = 2; i < 1000000; i++){ |
| 159 | + factorial[i] = (factorial[i-1]*i)%MOD; |
| 160 | + } |
| 161 | + |
| 162 | + //Main part of the input |
| 163 | + ll t; |
| 164 | + cin>>t; |
| 165 | + while(t--){ |
| 166 | + ll n, m; |
| 167 | + ll q; |
| 168 | + cin>>n>>m>>q; |
| 169 | + |
| 170 | + //Create a 2d array for board |
| 171 | + ll** board = new ll*[n]; |
| 172 | + for (ll i = 0; i < n; ++i) |
| 173 | + { |
| 174 | + board[i] = new ll[m]; |
| 175 | + for (ll j = 0; j < m; ++j) |
| 176 | + { |
| 177 | + board[i][j] = 0; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + //Fill the board |
| 182 | + while(q--){ |
| 183 | + ll x, y; |
| 184 | + cin>>x>>y; |
| 185 | + board[x-1][y-1] = 1; |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + //Creating a graph 2d array |
| 190 | + vector<pii>** graph = new vector<pii>*[(n)]; |
| 191 | + for (ll i = 0; i < n; ++i) |
| 192 | + { |
| 193 | + graph[i] = new vector<pii>[m]; |
| 194 | + } |
| 195 | + |
| 196 | + //Fill the graph |
| 197 | + fill_vector(graph, board, m, n); |
| 198 | + |
| 199 | + // for (ll i = 0; i < n; ++i) |
| 200 | + // { |
| 201 | + // for (ll j = 0; j < m; ++j) |
| 202 | + // { |
| 203 | + // if (graph[i][j].size()!=0) |
| 204 | + // { |
| 205 | + // for (ll k = 0; k < graph[i][j].size(); ++k) |
| 206 | + // { |
| 207 | + // cout<<graph[i][j][k].first<<" "<<graph[i][j][k].second<<" "; |
| 208 | + // } |
| 209 | + // cout <<'\n'; |
| 210 | + // } |
| 211 | + // } |
| 212 | + // } |
| 213 | + |
| 214 | + //Count and multiply the number of elements |
| 215 | + ll** visited = new ll*[n]; |
| 216 | + for (ll i = 0; i < n; ++i) |
| 217 | + { |
| 218 | + visited[i] = new ll[m];; |
| 219 | + for (ll j = 0; j < m; ++j) |
| 220 | + { |
| 221 | + visited[i][j] = 0; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + ll ans = 1; |
| 226 | + for (ll i = 0; i < n; ++i) |
| 227 | + { |
| 228 | + for (ll j = 0; j < m; ++j) |
| 229 | + { |
| 230 | + if (visited[i][j] == 0 && board[i][j] == 1) |
| 231 | + { |
| 232 | + // cout << i<<" "<<j<<" "<<" "<<dfs(i, j, graph, visited, m, n) << '\n'; |
| 233 | + // cout << "Here "<<factorial[2] << '\n'; |
| 234 | + // cout << i<<" "<<j<<" "<<" "<<dfs(i, j, graph, visited, m, n) << '\n'; |
| 235 | + |
| 236 | + ans = (ans*factorial[dfs(i, j, graph, visited)])%MOD; |
| 237 | + } |
| 238 | + |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + cout << ans << '\n'; |
| 243 | + } |
| 244 | + |
| 245 | + |
| 246 | + return 0 ; |
| 247 | + |
| 248 | +} |
0 commit comments