-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMATGAME.cpp
74 lines (56 loc) · 1.07 KB
/
MATGAME.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string.h>
using namespace std;
int n,m;
vector<vector<int> > matrix;
int mem[51][51];
int solve(int row, int index) {
if(index == m-1) {
return matrix[row][index];
}
if(mem[row][index]!=-1) return mem[row][index];
int temp = solve(row, index+1);
if(matrix[row][index] <= temp) {
return mem[row][index] = matrix[row][index] - 1;
} else {
return mem[row][index] = matrix[row][index];
}
}
int compute() {
int answer =0;
memset(mem,-1,sizeof(mem));
for(int i=0;i<n;i++) {
int temp = solve(i, 0);
answer ^= temp;
}
return answer;
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
int test;
cin>>test;
while(test--) {
cin>>n>>m;
matrix.clear();
for(int i=0;i<n;i++) {
vector<int> t;
for(int j=0;j<m;j++) {
int temp;
cin>>temp;
t.push_back(temp);
}
matrix.push_back(t);
}
int val = compute();
if(val == 0) {
cout<<"SECOND"<<endl;
} else {
cout<<"FIRST"<<endl;
}
}
return 0;
}