< Pair > 과 start,second

헤더 :  utility 

 

개념 :

pair < [type1] [type2] > p    :    사용할 데이터타입 1,2 를 넣고 그 타입의 pair 클래스인 p 생성

p.first    :    p의 첫번째 인자를 반환

p.second    :    p의 두번째 인자를 반환

make_fair(변수1,변수2)    :    변수1,2가 들어간 pair 를 생성

operator 사용가능

sort 알고리즘으로 정렬 가능

 

 

 

#include <iostream>
#include <utility>
#include <algorithm>

using namespace std;

#define max 50

string board[max];

int wb_cnt(string WB[],int x,int y){
    int cnt = 0;
    for(int i=0; i<8; i++){
        for(int j=0; j<8; j++){
            if(board[x+i][y+j] != WB[i][j]) cnt++;
        }
    }
    return cnt;
}

int bw_cnt(string BW[],int x,int y){
    int cnt = 0;
    for(int i=0; i<8; i++){
        for(int j=0; j<8; j++){
            if(board[x+i][y+j] != BW[i][j]) cnt++;
        }
    }
    return cnt;
}

int main(){
    string WB[8] = {
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW"
    };
    string BW[8] = {
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB",
        "BWBWBWBW",
        "WBWBWBWB"
    };

    int MIN = 999999;
    int cnt,tmp;
    pair<int,int> p;
    cin >> p.first >> p.second;
    for(int i=0; i<p.first; i++){
        cin >> board[i];
    }

    for(int i=0; i+8 <= p.first; i++){
        for(int j=0; j+8 <= p.second; j++){
            // 8*8 크기로 짤라내기만 하면 되므로 +8을 해줌
            tmp = min(wb_cnt(WB,i,j),bw_cnt(BW,i,j));
            if(tmp < MIN) MIN = tmp;
        }
    }
    cout << MIN;
    return 0;
}

 

'백준알고리즘' 카테고리의 다른 글

백준 10989 C++  (0) 2021.07.17
백준 2751 C++  (0) 2021.07.17
백준 7568 C++  (0) 2021.07.16
백준 2231 C++  (0) 2021.07.15
백준 2798 C++  (0) 2021.07.15

+ Recent posts