완전 탐색 문제
https://www.acmicpc.net/problem/2503
입력
첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트라이크 개수를 나타내는 정수와 볼의 개수를 나타내는 정수, 이렇게 총 세 개의 정수가 빈칸을 사이에 두고 주어진다.
출력
예상 가능한 숫자
예시
예제 입력
4
123 1 1
356 1 0
327 2 0
489 0 1
예제 출력
2
-------------------------------------------------------------------------------------------------------------------------------------------------
만약에
123 1 1 만 줬다면 어떤 수가 가능 할까? 라고 생각하게 되었다.
>>알고리즘은 천천히 간단한 예시를 생각하면서 푸는게 중요하다고 생각한다.
123 1 1
1 x 2
1 3 x
3 2 x
x 2 1
x 1 3
2 x 3
이렇게 6가지가 가능 하다.
123 ~ 999 숫자 중에서 ( 중복되는 숫자 제외하고 )
123이라는 숫자와 1 스트라이크, 1 볼인 모든 숫자를 찾으면 된다.
-------------------------------------------------------------------------------------------------------------------------------------------------
풀이
#include <iostream>
#include <string>
using namespace std;
bool check[1000];
int main()
{
int num;
cin >> num;
int number,strike,ball;
string compareString,myNumber;
int strike_cnt,ball_cnt;
int count=0;
// 123 to 999 find number that has same position.
for(int i =0;i<num;i++) //rotate in num
{ // ex 4
cin >>number >>strike >>ball;
myNumber = to_string(number);
for(int i =123;i<=998;i++)
{
compareString = to_string(i); // 123 124 to string
if(compareString[0] == compareString[1] ||
compareString[0] == compareString[2] ||
compareString[1] == compareString[2])
{
check[i]=true;
continue;
}
if (compareString[0] - '0' == 0 ||
compareString[1] - '0' == 0 ||
compareString[2] - '0' == 0)
{
check[i] = true;
continue;
}
strike_cnt =0;
ball_cnt =0;
for(int x =0;x<3;x++)
for(int y=0;y<3;y++)
{
if(x == y && compareString[y]== myNumber[x])
strike_cnt++;
if(x != y && compareString[y] == myNumber[x])
ball_cnt++;
}
if(strike != strike_cnt || ball != ball_cnt)
check[i]=true;
}
}
for(int i =123;i<999;i++)
{
if(!check[i])
count++;
}
cout<<count;
return 0;
}
같은수와 0 이 나오는 경우를 잘 걸러야 한다.