https://www.acmicpc.net/problem/17822
λ¬Έμ μ€λͺ
λ°μ§λ¦μ΄ 1,2,...,NμΈ μνμ΄ ν¬κΈ°κ° μμμ§λ μμ΄κ³ , μ€μ¬μ΄ λͺ¨λ κ°μ
λ°μ§λ¦μ΄ iμ΄λ©΄ iλ²μ§Έ μν
κ°κ°μ μνμλ Mκ°μ μ μκ° μ‘΄μ¬, (i,j) π iλ²μ§Έ μνμ μ ν jλ²μ§Έ μμ μμΉ
- (i,1)μ (i,2), (i,M)κ³Ό μΈμ
- (i,M)μ (i,1), (i,M-1)κ³Ό μΈμ
- (i,j)λ (i,j-1), (i,j+1)κ³Ό μΈμ (2<=j<= M-1)
- (1,j)λ (2,j)μ μΈμ
- (N,j)λ (N-1,j)μ μΈμ
- (i,j)λ (i-1,j), (i+1,j)μ μΈμ (2<=i<=N-1)
κ° μνμ νμ μ λ 립μ μΌλ‘ μ§νλ¨
μ΄ Tλ² νμ , iλ²μ§Έ νμ ν λ μ¬μ©λλ λ³μλ $x_i, d_i, k_i$
- λ²νΈκ° $x_i$μ λ°°μμΈ μνμ $d_i$λ°©ν₯μΌλ‘ $k_i$μΉΈ νμ
- $d_i$ == 0 π μκ³ λ°©ν₯
- $d_i$ == 1 π λ°μκ³ λ°©ν₯
- μνμ μκ° λ¨μμμΌλ©΄, μΈμ νλ©΄μ κ°μ μλ₯Ό λͺ¨λ μ°Ύμ
1) μΈμ νκ³ κ°μ μκ° μλ€λ©΄
2) μλ€λ©΄, μ°Ύμ μλ₯Ό λͺ¨λ μ§μ- μ‘΄μ¬νλ μμ νκ· μ ꡬνκ³ , νκ· λ³΄λ€ ν° μ -= 1, νκ· λ³΄λ€ μμ μ += 1
Tλ² νμ ν, μνμ μ ν μμ ν©μ μΆλ ₯
λ¬Έμ νμ΄
disc[N+1][M]
: 1λ² μν ~ Nλ² μνμ κ° μ«μλ€μ΄ μ μ₯λ λ°°μ΄
1) νμ
rotateDisc(x, d, k)
int dir = d == 0 ? 1 : -1;
↑ λ°μκ³ λ°©ν₯μ΄λΌλ©΄ μμΌλ‘ μ΄λ, μκ³λ°©ν₯μ΄λΌλ©΄ λ€λ‘ μ΄λμ΄κΈ°λλ¬Έμ
μμμ λ°°μ΄ tmpμ μ΄λκ²°κ³Ό μ μ₯tmp[j + (dir * (k % M)) + M) % M] = disc[i][j];
κ·Έ ν, μλ λ°°μ΄ discμ μ μ₯
2) μΈμ ν κ°μ μ«μ μ‘΄μ¬ νμΈ
discλ΄μ 0μ΄ μλ λͺ¨λ μμ λν΄BFS
μ΄μ©ν΄μ κ°μ κ° μ°ΎκΈ°
ν μΈλ±μ€μ λν΄μλ 0 < nx <= NμΈ λΆλΆλ§ μ§ν,
μ΄ μΈλ±μ€μ λν΄μλ
1) if(ny == M) ny = 0;
2) if(ny == -1) ny = M-1;
λ‘ λ³κ²½ν΄μ€
Boolean isSame
π κ·Έ κ°μ λν΄μ λ³κ²½νκΈ° μν λ³μBoolean isChange
π μ 체 νμΈ ν, μΈμ ν κ°μ κ° μμ νμΈ μν λ³μ
3) νκ· κ΅¬νκ³ μ«μ λ³κ²½
λ§μ½ μμ isChange
λ³μκ° falseλΌλ©΄,
νκ· μ ꡬνκΈ° π calAverage()
κ·Έ ν, νκ· κ°μ λ°λΌ μ«μ λ³κ²½ π changeNum(float sum)
3) λͺ¨λ μνμ μ«μ ν© μΆλ ₯
μ½λ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_BOJ_17822_μν_λ리기 {
static int N, M, T, x, d, k;
static int[][] disc;
static boolean[][] isVisited;
static int[] dx = {-1, 0, 0, 1};
static int[] dy = {0, -1, 1, 0};
static boolean isSame, isChange = false;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
N = Integer.parseInt(stringTokenizer.nextToken());
M = Integer.parseInt(stringTokenizer.nextToken());
T = Integer.parseInt(stringTokenizer.nextToken());
disc = new int[N + 1][M];
for (int i = 1; i < N + 1; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
disc[i][j] = Integer.parseInt(stringTokenizer.nextToken());
}
}
while (T-- > 0) {
stringTokenizer = new StringTokenizer(br.readLine());
x = Integer.parseInt(stringTokenizer.nextToken());
d = Integer.parseInt(stringTokenizer.nextToken());
k = Integer.parseInt(stringTokenizer.nextToken());
rotateDisc(x, d, k);
// System.out.println("x : " +x +" d: " + d +" k: " +k );
// tostring();
isChange = false;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < M; j++) {
isSame = false;
if(disc[i][j] != 0) {
checkAdjNum(i, j, disc[i][j]);
if(isSame) {
disc[i][j] = 0;
/* System.out.println("i : "+i+" j : "+j );
tostring();*/
}
}
}
}
if (!isChange) {
calAverage();
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < M; j++) {
ans += disc[i][j];
}
}
System.out.println(ans);
}
private static void tostring() {
for (int[] di : disc) {
for (int d : di) {
System.out.print(d + " ");
}
System.out.println();
}
System.out.println();
}
private static void calAverage() {
// System.out.println("calAverage()");
float sum = 0;
float cnt = 0;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < M; j++) {
sum += disc[i][j];
if(disc[i][j] != 0) cnt += 1;
}
}
sum /= cnt;
changeNum(sum);
}
private static void changeNum(float sum) {
// System.out.println("sum: " + sum);
for (int i = 1; i <= N; i++) {
for (int j = 0; j < M; j++) {
if(disc[i][j] != 0){
if(disc[i][j] < sum) disc[i][j] += 1;
else if(disc[i][j] > sum) disc[i][j] -= 1;
}
}
}
// tostring();
}
private static void checkAdjNum(int x, int y, int value) {
isVisited = new boolean[N + 1][M];
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{x, y});
isVisited[x][y] = true;
while (!q.isEmpty()) {
int[] now = q.poll();
for (int i = 0; i < 4; i++) {
int nx = now[0] + dx[i];
int ny = now[1] + dy[i];
if(ny == M) ny = 0;
if(ny == -1) ny = M - 1;
if(nx <= 0 || N < nx || isVisited[nx][ny] || disc[nx][ny] != value) continue;
q.add(new int[]{nx, ny});
isVisited[nx][ny] = true;
disc[nx][ny] = 0;
isSame = true;
isChange = true;
}
}
}
private static void rotateDisc(int x, int d, int k) {
// νμ
int dir = d == 0 ? 1 : -1;
for (int i = x; i < N + 1; i += x) {
int[] tmp = new int[M];
for (int j = 0; j < M; j++) {
tmp[(j + (dir * (k % M)) + M) % M] = disc[i][j];
}
disc[i] = Arrays.copyOf(tmp, M);
}
}
}
'μκ³ λ¦¬μ¦ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ λ°±μ€ / BOJ 19236 ] μ²μλ μμ΄ ( μλ° / JAVA ) (0) | 2022.02.08 |
---|---|
[ λ°±μ€ / BOJ 19237 ] μ΄λ₯Έ μμ΄ ( μλ° / JAVA ) (0) | 2022.02.08 |
[ BOJ / λ°±μ€ 20058 ] λ§λ²μ¬ μμ΄μ νμ΄μ΄μ€ν° ( μλ° / JAVA ) (0) | 2022.02.05 |
[ BOJ / λ°±μ€ 19238 ] μ€ννΈ νμ ( μλ° / JAVA ) (0) | 2022.02.04 |
[ λ°±μ€ / BOJ 17779 ] κ²λ¦¬λ©λλ§_2 ( μλ° / JAVA ) (0) | 2022.02.02 |