-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnectedHorses.cpp
More file actions
165 lines (122 loc) · 3.4 KB
/
connectedHorses.cpp
File metadata and controls
165 lines (122 loc) · 3.4 KB
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
// Connected horses
// You all must be familiar with the chess-board having
// 8*8 squares of alternate black and white cells. Well, here
// we have for you a similar
// N*M size board with similar arrangement of black and white
// cells.
// A few of these cells have Horses placed over them.
// Each horse is unique. Now these horses are not the
// usual horses which could jump to any of the
// 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.
// 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
// 10^9+7
// Input:
// First line contains
// T which is the number of test cases.
// T test cases follow first line of each containing three integers
// N, M and Q where
// N,M is the size of the board and
// Q is the number of horses on it.
// Q lines follow each containing the 2 integers
// X and Y which are the coordinates of the Horses.
// Output:
// For each test case, output the number of photographs taken by photographer.
// Constraints:
// 1<=T<=10
// 1<=N,M<=1000
// 1<=Q<=N*M
// SAMPLE INPUT
// 2
// 4 4 4
// 1 1
// 1 2
// 3 1
// 3 2
// 4 4 4
// 1 1
// 1 2
// 3 1
// 4 4
// SAMPLE OUTPUT
// 4
// 2
#include<bits/stdc++.h>
using namespace std;
long M = 1e9+7;
long long fact[100000];
struct coordinates {
int x;
int y;
};
void calculateFact() {
fact[0] = 1;
for(int i = 1; i <= 100000; i++) {
fact[i] = (fact[i-1] * i) % M;
}
}
int dfs(vector<int> *edges, int currVertex, bool *visited) {
visited[currVertex] = true;
int count = 1;
for(int i = 0; i < edges[currVertex].size(); i++) {
int adjacent = edges[currVertex][i];
if(!visited[adjacent]) {
count += dfs(edges, adjacent, visited);
}
}
return count;
}
long getPermutations(vector<int> *edges, int q) {
bool *visited = new bool[q]();
long permutations = 1;
for(int i = 0; i < q; i++) {
int componentCount = dfs(edges, i, visited);
permutations = (permutations * fact[componentCount]) % M;
}
delete [] visited;
return permutations;
}
bool swapPossible(coordinates c1, coordinates c2) {
if((c2.x == c1.x-2 && c2.y == c1.y-1) || (c2.x == c1.x-2 && c2.y == c1.y+1) || (c2.x == c1.x-1 && c2.y == c1.y-2) || (c2.x == c1.x-1 && c2.y == c1.y+2)) {
return true;
}
else if((c2.x == c1.x+1 && c2.y == c1.y-2) || (c2.x == c1.x+1 && c2.y == c1.y+2) || (c2.x == c1.x+2 && c2.y == c1.y-1) || (c2.x == c1.x+2 && c2.y == c1.y+1)) {
return true;
}
return false;
}
int main() {
int t;
cin >> t;
calculateFact();
while(t--) {
int n, m, q;
cin >> n >> m >> q;
unordered_map<int, coordinates> horses;
unordered_set<coordinates> h;
for(int i = 0; i < q; i++) {
int x, y;
coordinates pos;
cin >> x >> y;
pos.x = x;
pos.y = y;
horses[i] = pos;
h.insert(pos);
}
vector<int> *edges = new vector<int>[q];
for(int i = 0; i < horses.size(); i++) {
for(int j = i+1; j < horses.size(); j++) {
if(i == j) {
continue;
}
if(swapPossible(horses[i], horses[j])) {
edges[i].push_back(j);
edges[j].push_back(i);
}
}
}
long permutations = getPermutations(edges, q);
cout << permutations << endl;
delete [] edges;
}
return 0;
}