Skip to content

Commit e4e93b6

Browse files
committed
srm 172 div 1 med
1 parent 1b3b20c commit e4e93b6

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

Fifteen.cpp

+33-44
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,56 @@
66
#include <map>
77
#include <cstring>
88
#include <climits>
9+
#define cmpl(A, X) for(int i = 1; i <= 9; i++) if(find((A).begin(), (A).end(), i) == (A).end()) (X).push_back(i);
910
using namespace std;
1011
typedef vector<int> vi;
1112
typedef vector<vi> vvi;
1213
typedef vector<string> vs;
1314
typedef vector<vs> vvs;
1415
typedef set<int> si;
1516
int maxv = 1000000;
17+
int pd[4] = {0, 2, 4, 8};
18+
int pp[4] = {1, 3, 5, 7};
19+
vi v = {1,2,3,4,5,6,7,8,9};
1620

1721
class Fifteen {
1822
public:
1923
//P plays maxv means P lose and win otherwise
20-
int fp(si D, si P, si X){
21-
int minv = maxv;
22-
for(int c : X)
23-
for(int a : P)
24-
for(int b : P)
25-
if(a != b && a+b+c == 15) minv = min(minv, c);
26-
if(X.size() == 1) return maxv;
27-
si XX = X;
28-
for(int c : X){
29-
P.insert(c);
30-
XX.erase(c);
31-
if(fd(D, P, XX) == 0) minv = min(minv, c);
32-
P.erase(c);
33-
XX.insert(c);
34-
}
35-
if(minv != maxv) return minv;
36-
return maxv;
37-
}
38-
//D plays maxv means D wins and 0 means D lose
39-
int fd(si D, si P, si X){
40-
// if(X.size() == 1) return maxv;
41-
for(int c : X)
42-
for(int a : D)
43-
for(int b : D)
44-
if(a != b && a+b+c == 15) return maxv;
45-
si XX = X;
46-
for(int c : X){
47-
D.insert(c);
48-
XX.erase(c);
49-
if(fp(D, P, XX) == maxv) return maxv;
50-
D.erase(c);
51-
XX.insert(c);
24+
int f(vi A, int p){
25+
vi M;
26+
for(int i = p; i < A.size(); i+=2)
27+
M.push_back(A[i]);
28+
sort(M.begin(), M.end());
29+
for(int a : M)
30+
for(int b : M)
31+
for(int c : M)
32+
if(a != b && b != c && c != a && a+b+c == 15) return 1;
33+
int q = (p == 0 ? 1 : 0);
34+
if(A.size() == 9) return q;
35+
36+
vi X, XX;
37+
cmpl(A, X);
38+
XX = A;
39+
for(int r : X){
40+
XX.push_back(r);
41+
if(f(XX, q)) return 0;
42+
XX.pop_back();
5243
}
53-
return 0;
44+
45+
return 1;
5446
}
5547

5648
string outcome(vector <int> moves)
5749
{
58-
si D, P;
59-
vi v = {1,2,3,4,5,6,7,8,9};
60-
si X(v.begin(), v.end());
61-
for(int i = 0; i < moves.size(); i++){
62-
int k = moves[i];
63-
X.erase(k);
64-
if(i%2) P.insert(k);
65-
else D.insert(k);
50+
vi X;
51+
cmpl(moves, X);
52+
vi XX = moves;
53+
for(int r : X){
54+
XX.push_back(r);
55+
if(f(XX, 1)) return "WIN "+to_string(r);
56+
XX.pop_back();
6657
}
67-
int r = fp(D, P, X);
68-
if(r == maxv) return "LOSE";
69-
return "WIN "+to_string(r);
58+
return "LOSE";
7059
}
7160

7261
// BEGIN CUT HERE

0 commit comments

Comments
 (0)