https://softeer.ai/practice/info.do?idx=1&eid=624
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
void countPlace(int &fivePlace,int &fourPlace,int &threePlace,int &twoPlace,int &onePlace, string sA){
if(sA.length()==5){
fivePlace=(int)sA[0]-48 ;
fourPlace=(int)sA[1]-48 ;
threePlace=(int)sA[2]-48 ;
twoPlace=(int)sA[3]-48 ;
onePlace=(int)sA[4]-48 ;
}
if(sA.length()==4){
fourPlace=(int)sA[0]-48 ;
threePlace=(int)sA[1]-48 ;
twoPlace=(int)sA[2]-48 ;
onePlace=(int)sA[3]-48 ;
}
if(sA.length()==3){
threePlace=(int)sA[0]-48 ;
twoPlace=(int)sA[1]-48 ;
onePlace=(int)sA[2]-48 ;
}
if(sA.length()==2){
twoPlace=(int)sA[0]-48 ;
onePlace=(int)sA[1]-48 ;
}
if(sA.length()==1){
onePlace=(int)sA[0]-48;
}
}
void bulbSwitch(int num, map<int,bool> &tmpMap,int &count){
//전공판 스위치 번호
/*
_
| |
_
| |
_
0
1 2
3
4 5
6
0// 0,1,2,4,5,6
1// 2,5
2// 0,2,3,4,6
3// 0,2,3,5,6
4// 1,2,3,5
5// 0,1,3,5,6
6// 1,3,4,5,6
7// 0,1,2,5
8// 0,1,2,3,4,5,6
9// 0,1,2,3,5,6
*/
if(num==0){
//0: 0,1,2,4,5,6
for(int i=0; i<7; i++){
if(i==0||i==1||i==2||i==4||i==5||i==6){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}else if(num==1){
// 1: 2,5
for(int i=0;i<7;i++){
if(i==2||i==5){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==2){
//2: 0,2,3,4,6
for(int i=0;i<7;i++){
if(i==0||i==2||i==3||i==4||i==6){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==3){
// 3: 0,2,3,5,6
for(int i=0;i<7;i++){
if(i==0||i==2||i==3||i==5||i==6){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==4){
//4: 1,2,3,5
for(int i=0;i<7;i++){
if(i==1||i==2||i==3||i==5){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==5){
//5: 0,1,3,5,6
for(int i=0;i<7;i++){
if(i==0||i==1||i==3||i==5||i==6){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==6){
//6 :0, 1,3,4,5,6
for(int i=0;i<7;i++){
if(i==0||i==1||i==3||i==4||i==5||i==6){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==7){
//7: 0,1,2,5
for(int i=0;i<7;i++){
if(i==0||i==1||i==2||i==5){
if(tmpMap[i]==false){
count++;
//cout<<i<<",";
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
//cout<<i<<",";
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==8){
//8: 0,1,2,3,4,5,6
// cout<<num<<endl;
for(int i=0;i<7;i++){
if(i==0||i==1||i==2||i==3||i==4||i==5||i==6){
if(tmpMap[i]==false){
// cout<<i<<",";
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
//cout<<i<<",";
count++;
}
tmpMap[i]=false;
}
}
}
else if(num==9){
// 9 :0,1,2,3,5,6
for(int i=0;i<7;i++){
if(i==0||i==1||i==2||i==3||i==5||i==6){
if(tmpMap[i]==false){
count++;
}
tmpMap[i]=true;
}else{
if(tmpMap[i]==true){
count++;
}
tmpMap[i]=false;
}
}
}
}
int main(int argc, char** argv)
{
// 현대차그룹
// 전세계 유가 변동
// 다섯자리 자연수
// 7(자릿수당 전국 갯수)*5(다섯자리 자연수) = 35
// 8자모양 0~9까지 표현
//1000자리 숫자 표현시 만의자리는 숫자는 0이아닌 ->전부 전구 off
//각전구에는 스위치가 달려있다.
//전구에 달려잇는 스위치를 누를때,
//전구가 켜져있었다면 꺼지고 꺼져잇으면 켜진다
//스위치는 트리거 on/off
//지금 전광판에는 자연수 A가 표시되어 있는데,
// 유가 변동됨에 따라 자연수 B로. 바꿔야함
// 1000->1234 (A->B)
//이러한 목표달성을 위해 스위치를 최소 몇번 눌러야하는가?
/* 제약조건
하나의 입력에서 1개 이상 1000개 이하의 테스트 케이스를 해결해야 한다.
A와 B는 한 자리 이상 다섯 자리 이하의 자연수이다.
A와 B는 숫자 0으로 시작하지 않는다.
A와 B는 서로 다르다.
*/
//테스트 케이스 수 T
//다음 T개의 줄에는 한줄에 테스트 케이스 하나씩 주어짐
// 각각의 줄에는 두자연수 A와 B 공백 하나를 사이로 두고 주어진다
int T;//테스트 케이스 수
cin>>T;
for(int i=0;i<T;i++){
int A,B;
cin>>A>>B;
//A전공판 만들기
string sA=to_string(A);
string sB=to_string(B);
//cout<<sA.length();
//cout<<(int)sA[0]-48 ;
// cout<<endl;
int fivePlaceA=-1;
int fourPlaceA=-1;
int threePlaceA=-1;
int twoPlaceA=-1;
int onePlaceA=-1;
int fivePlaceB=-1;
int fourPlaceB=-1;
int threePlaceB=-1;
int twoPlaceB=-1;
int onePlaceB=-1;
countPlace(fivePlaceA,fourPlaceA,threePlaceA,twoPlaceA,onePlaceA,sA);
countPlace(fivePlaceB,fourPlaceB,threePlaceB,twoPlaceB,onePlaceB,sB);
// cout<<onePlace;
// cout<<endl;
//전공판 스위치 번호
/*
_
| |
_
| |
_
0
1 2
3
4 5
6
0// 0,1,2,4,5,6
1// 2,5
2// 0,2,3,4,6
3// 0,2,3,5,6
4// 1,2,3,5
5// 0,1,3,5,6
6// 1,3,4,5,6
7// 0,1,2,5
8// 0,1,2,3,4,5,6
9// 0,1,2,3,5,6
*/
//스위치 a,b
map<int,bool>switch1;
map<int,bool>switch2;
map<int,bool>switch3;
map<int,bool>switch4;
map<int,bool>switch5;
for(int i=0;i<7;i++){
switch1[i]=false;
switch2[i]=false;
switch3[i]=false;
switch4[i]=false;
switch5[i]=false;
}
int countA=0;
vector<int> countAs;
for(int i=0;i<5;i++){
countAs.push_back(0);
}
//A상태 표기
if(fivePlaceA!=-1){
bulbSwitch(fivePlaceA, switch5,countAs[0]); // 2
}
if(fourPlaceA!=-1){
bulbSwitch(fourPlaceA, switch4,countAs[1]); //2
}
if(threePlaceA!=-1){
bulbSwitch(threePlaceA, switch3,countAs[2]);
}
if(twoPlaceA!=-1){
bulbSwitch(twoPlaceA, switch2,countAs[3]);
}
if(onePlaceA!=-1){
bulbSwitch(onePlaceA, switch1,countAs[4]);
}
int countB=0;
//A상태 ->B변경
//전공판 변경 갯수
//fivePlaceA,fourPlaceA,threePlaceA,twoPlaceA,onePlaceA
//fivePlaceB,fourPlaceB,threePlaceB,twoPlaceB,onePlaceB
bulbSwitch(fivePlaceB, switch5,countB); // 2
bulbSwitch(fourPlaceB, switch4,countB); //2
bulbSwitch(threePlaceB, switch3,countB);
bulbSwitch(twoPlaceB, switch2,countB);
bulbSwitch(onePlaceB, switch1,countB);
if(fivePlaceB==-1&&fivePlaceA!=-1){
countB+=countAs[0];
}
if(fourPlaceB==-1&&fourPlaceA!=-1){
countB+=countAs[1];
}
if(threePlaceB==-1&&threePlaceA!=-1){
countB+=countAs[2];
}
if(twoPlaceB==-1&&twoPlaceA!=-1){
countB+=countAs[3];
}
if(onePlaceB==-1&&onePlaceA!=-1){
countB+=countAs[4];
}
cout<<countB;
cout<<endl;
}
return 0;
}
'알고리즘 공부' 카테고리의 다른 글
택배 마스터 광우 (0) | 2023.03.02 |
---|---|
비밀 메뉴 (0) | 2023.03.02 |
회의실 예약 (0) | 2023.03.02 |
프로그래머스 -2019 KAKAO BLIND RECRUITMENT 오픈채팅방-연습문제 (0) | 2021.09.26 |
<UnionFind> 경로 연결 문제 (0) | 2021.05.20 |