크게 어려운것은 없었다.

왼쪽이나 오른쪽 하나를 정해 그쪽부터 채워넣으면 된다.

거리가 먼 것부터 로봇이 물건을 집는다는 뜻이다.

#include<iostream>
#include<string>

using namespace std;

int main(int argc, char** argv)
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int N, K; cin >> N >> K;

	string in; cin >> in;

	int ans = 0;
	for (int i = 0; i < N; i++) {
		if (in[i] == 'P') {
			for (int j = i - K; j <= i + K; j++) {
				if (j < 0 || i == j || j >= N)
					continue;
				if (in[j] == 'H') {
					in[j] = 'X';
					ans++;
					break;
				}
			}
		}
	}

	cout << ans << '\n';

	return 0;
}

+ Recent posts