There is a chessboard of size n by n. The square in the i-th row from top and j-th column from the left is labelled (i,j).
Currently, Gregor has some pawns in the n-th row. There are also enemy pawns in the 1-st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from (i,j) to (i−1,j)) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from (i,j) to either (i−1,j−1) or (i−1,j+1)) if and only if there is an enemy pawn in that square. The enemy pawn is also removed.
Gregor wants to know what is the maximum number of his pawns that can reach row 1?
Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row 1, it is stuck and cannot make any further moves.
이런 설명인데 간단하게 말하면
- 앞에 적이 있을 경우에는 대각선 좌, 우 위로 갈 수 있다
- 적이 없을 경우 앞으로 한칸 이동가능하다
- 내 폰을 최대한 많이 1행에 놓을때 그 수는 ? 이런문제이다
틀린 이유 : 마지막에 시간 초과가 났었는데 왜 나는지 몰랐다 ...
알고보니 배열을 ll arr[2][200010] 이렇게 잡았는데 memset으로 초기화 하면
memset도 시간이 n만큼 걸리기 때문에 시간초과가 났다 이부분을 해결한 코드는
for (int i = 0; i < n; ++i) {
arr[0][i] = 0;
arr[1][i] = 0;
}
//memset(arr, 0, sizeof(arr));
배열로 n만 초기화를 해주었다
풀이 방법 :
1행을 기준으로 풀었다
- 1행에 아무것도 없고 2행에 폰이 있을 경우 가능
- 1행에 있을 경우에는 2행 폰을 볼때 아래의 좌 우를 잘 살피자
- 좌 우 살필때 범위도 잘 살펴보자
중요 부분 코드
for (int j = 0; j < n; j++) {
if (arr[0][j] == 0 && arr[1][j] == 1) {
ans++;
arr[0][j] = 1;
arr[1][j] = 0;
continue;
}
else if (arr[0][j] == 1) {
if (j - 1 >= 0 && arr[1][j - 1] == 1) {
arr[1][j - 1] = 0;
ans++;
continue;
}
else if (j + 1 < n && arr[1][j + 1] == 1) {
arr[1][j + 1] = 0;
ans++;
continue;
}
}
}
'코드포스 > div2' 카테고리의 다른 글
A. Find The Array (0) | 2021.08.09 |
---|---|
B. Two Tables (0) | 2021.08.02 |
A. PizzaForces (3) | 2021.07.31 |
div2 - Cherry (0) | 2021.07.30 |