[Programmers] 숫자 야구

Time Lapse :20min 0sec

solution.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
#include <vector>
#include <algorithm>
using namespace std;
//20:00
int b[3], d[3];
int strike = 0, ball = 0;
vector<int> possible;
bool cmp(vector<int> v1, vector<int> v2){
if(v1[1]==v2[1]) return v1[2]>v2[2];
return v1[1]>v2[1];
}
inline bool is_Possible(vector<int> &baseball){
strike = 0, ball = 0;
for(int f=0;f<3;f++){
for(int l=0;l<3;l++) {
if (b[f] == d[l]) {
if (f != l)
++ball;
else
++strike;
}
//if(ball>baseball[2]||strike>baseball[1]) return false;
}
}
if(strike == baseball[1] && ball == baseball[2]) return true;
return false;
}
int solution(vector<vector<int>> baseball) {
possible.reserve(1000);
//sort(baseball.begin(),baseball.end(),cmp);
b[0] = baseball[0][0]/100;
b[1] = (baseball[0][0]/10)%10;
b[2] = baseball[0][0]%10;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
if(i==j)
continue;
for(int k=1;k<=9;k++){
if(j==k||i==k)
continue;
d[0] = i, d[1] = j, d[2] = k;
if(is_Possible(baseball[0])){
possible.push_back(i*100+j*10+k);
}
}
}
}
for(int i=1;i<baseball.size();i++){
for(int j=0;j<possible.size();j++){
strike = 0, ball = 0;
b[0] = baseball[i][0]/100;
b[1] = (baseball[i][0]/10)%10;
b[2] = baseball[i][0]%10;
d[0] = possible[j]/100;
d[1] = (possible[j]/10)%10;
d[2] = possible[j]%10;

if(!is_Possible(baseball[i])){
possible.erase(possible.begin()+j);
--j;
}
}
}
return possible.size();
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/numbericBaseball/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.