Cod sursa(job #2875617)

Utilizator NedusNedelcu Alexandru Nedus Data 21 martie 2022 23:55:47
Problema Fractal Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 2.28 kb
package com.company;

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 n, x, y;

        public void solve() {
            readInput();
            writeOutput(getResult(n, x, y));
        }

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

        private void writeOutput(int answer) {
            try {
                PrintWriter pw = new PrintWriter(new File(OUTPUT_FILE));
                pw.printf("%d\n", answer);
                pw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        private int getResult(int n, int x, int y) {
            // TODO: Calculati valoarea de pe pozitia (x, y) din matricea de dimensiune 2^n * 2^n.

            System.out.println(n + " " + x + " " + y);

            if(n==1){
                if(x==1 && y==1) return 0;
                if(x==1 && y==2) return 1;
                if(x==2 && y==1) return 3;
                if(x==2 && y==2) return 2;
            }
            int putere = (int) Math.pow(2,n-1);
            int de_adaugat = (int) Math.pow(4,n-1);
            if(x>putere){
                if(y>putere) {
                    return 2 * de_adaugat + getResult(n-1,x-putere,y-putere);
                }
                else {
                    return 3 * de_adaugat + getResult(n-1,putere-y+1,putere-(x-putere)+1);
                }
            }
            else{
                if(y>putere){
                    return de_adaugat + getResult(n-1,x,y-putere);
                }
                else {
                    return getResult(n-1,y,x);
                }
            }
        }
    }

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