Cod sursa(job #3295331)

Utilizator dianav13Diana V dianav13 Data 4 mai 2025 14:32:41
Problema Fractal Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 2.4 kb
// SPDX-License-Identifier: BSD-3-Clause

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    static class Task {
        public final static String INPUT_FILE = "fractal.in";
        public final static String OUTPUT_FILE = "fractal.out";

        int k;
        int x;
        int y;

        public void solve() {
            readInput();
            int result = getResult(k, x - 1, y - 1); // Convertim la 0-based
            writeOutput(result); // Output este 1-based
        }

        private void readInput() {
            try {
                Scanner sc = new Scanner(new File(INPUT_FILE));
                k = sc.nextInt();
                x = sc.nextInt();
                y = sc.nextInt();
                sc.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        private void writeOutput(int result) {
            try {
                PrintWriter pw = new PrintWriter(new File(OUTPUT_FILE));
                pw.println(result);
                pw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        private int getResult(int k, int x, int y) {
            int res = 0;
            int n = (int) Math.pow(2, k); // dimensiunea grilei 2^k
            int size = n / 2;

            while (size > 0) {
                int rx = (x / size) % 2;
                int ry = (y / size) % 2;

                int quadrant = 0;
                if (rx == 0 && ry == 0) {
                    // rotim 90° dreapta
                    int temp = x;
                    x = y;
                    y = temp;
                    quadrant = 0;
                } else if (rx == 0 && ry == 1) {
                    quadrant = 1;
                } else if (rx == 1 && ry == 1) {
                    quadrant = 2;
                } else if (rx == 1 && ry == 0) {
                    // rotim 90° stanga și oglindim
                    int temp = x;
                    x = n - 1 - y;
                    y = n - 1 - temp;
                    quadrant = 3;
                }

                res += quadrant * size * size;

                x %= size;
                y %= size;
                size /= 2;
            }

            return res;
        }


    }

    public static void main(String[] args) {
        new Task().solve();
    }
}