-
Notifications
You must be signed in to change notification settings - Fork 0
/
ting_hu.cpp
154 lines (141 loc) · 3.63 KB
/
ting_hu.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
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
# include <iostream>
# include <algorithm>
# include <vector>
# include <fstream>
# include <typeinfo>
using namespace std;
bool canhu ( vector<int>& , vector<int>& );
int main(int argc, char** argv){
#define SIZE 100
char line[SIZE];
// input into in_vec
vector<int> in_vec;
fstream file;
file.open("input.txt",ios::in);
while(file.getline(line,sizeof(line),' ')){
int iline = atoi(line);
in_vec.push_back(iline);
}
// ting case
if( in_vec.size() == 13){
vector<int> ting;
for ( int p = 1; p <= 9; p++ ){
// add one tile per time
in_vec.push_back( p );
// sort
sort( in_vec.begin(), in_vec.end() );
// count numbers of each type of tiles
vector<int> count ( 10 );
for ( int i = 0; i < in_vec.size(); i++ ){
for ( int j = 1; j <= 9; j++ ){
if ( in_vec[i] == j ) {
count[j]++;
break;
}
}
}
if ( canhu( in_vec, count ) == 1 ){
ting.push_back( p );
}
// delete the tile added before
for ( int i = 0; i < in_vec.size(); i++ ){
if ( in_vec[i] == p ){
in_vec.erase( in_vec.begin() + i );
break;
}
}
}
ofstream fout;
fout.open("output.txt");
for ( int i = 0; i < ting.size(); i++ ){
fout << ting[i] << " ";
}
fout.close();
}
// hu case
else if ( in_vec.size() == 14 ){
if ( canhu( in_vec, count))
}
else{
ofstream fout;
fout.open("output.txt");
fout << "Wrong input!\n";
fout.close();
}
return 0;
}
bool canhu ( vector<int>& ptr, vector<int>& cnt_ptr ){
bool win = 0;
for ( int j = 1; j <= 9; j++ ){
if ( win == 1 ) break;
// find a pair of tiles
if ( cnt_ptr[j] >= 2 ){
//copy vector
vector<int> a_new( ptr.size() );
for ( int i = 0; i < ptr.size(); i++ )
a_new[i] = ptr[i];
//define which index to remove from vector
int remove1 = 0;
int remove2 = 0;
for ( int i = 0; i < a_new.size(); i++ ){
if ( a_new[i] == j ) {
remove1 = i;
remove2 = i + 1;
break;
}
}
// remove a pair of tiles
a_new.erase( a_new.begin() + remove2 );
a_new.erase( a_new.begin() + remove1 );
while ( a_new.size() != 0 ){
// if it is a flush, such as x,x,x or y,y,y
if ( ( a_new[0] == a_new[1]) && ( a_new[1] == a_new[2] ) ){
a_new.erase( a_new.begin() );
a_new.erase( a_new.begin() );
a_new.erase( a_new.begin() );
// if no tiles left, win
if ( a_new.size() == 0 ){
win = 1;
}
}
// if it's a straight, such as x, x+1, x+2
// else case, try to find x, x+1, x+2
else{
int rv1 = 0, rv2 = 0; // index of x+1, x+2
int flag = 0; // to confirm there exists x, x+1, x+2
for ( int i = 1; i < a_new.size(); i++ ){
flag = 0;
//find that x+1 exist
if ( a_new[i] == a_new[0] + 1 ){
rv1 = i;
for ( int k = rv1 + 1; k < a_new.size(); k++ ){
//find that x+2 exist
if ( a_new[k] == a_new[0] + 2 ){
flag = 1;
rv2 = k;
//delete x, x+1, x+2
a_new.erase( a_new.begin() + rv2 );
a_new.erase( a_new.begin() + rv1 );
a_new.erase( a_new.begin() );
//if there is no remaining tile, hu!
if ( a_new.size() == 0 ){
win = 1;
}
break;
}
}
break;
}
}
// no x, x+1, x+2 exists
if ( flag == 0 ) break;
}
}
// renew copy vector
a_new.clear();
}
}
// show result
if ( win == 1 ) return 1;
else return 0;
}