알고리즘 문제/C++

[프로그래머스] Level1 : 크레인 인형뽑기 게임 - C++

lingk 2020. 6. 30. 16:07
int solution(vector<vector<int>> board, vector<int> moves) {
	int answer = 0;
	vector<int> newM;
	for (int i = 0; i<moves.size(); i++) {
		int j = 0;
		while (true) {
			if (board[j][moves[i] - 1] != 0) {
				newM.push_back(board[j][moves[i] - 1]);
				board[j][moves[i] - 1] = 0;
				break;
			}
			j++;
			if (j == board.size())
				break;
		}
		if(newM.size()>=2)
			if (newM[newM.size() - 1] == newM[newM.size() - 2]) {
				newM.pop_back();
				newM.pop_back();
				answer += 2;
			}

	}
	return answer;
}

 

반응형