CSES - Datatähti 2016 alku - Results
Submission details
Task:Tontti
Sender:testUser
Submission time:2015-10-07 14:15:16 +0300
Language:Java
Status:COMPILE ERROR

Compiler report

input/Tontti.java:52: error: unmappable character for encoding UTF8
                //haluttu m??r?
                           ^
input/Tontti.java:52: error: unmappable character for encoding UTF8
                //haluttu m??r?
                            ^
input/Tontti.java:52: error: unmappable character for encoding UTF8
                //haluttu m??r?
                              ^
3 errors

Code

/**
 * Created by Frans on 5.10.2015.
 */
public class Tontti{

    static int height;
    static int width;
    static int[][] kentta;
    static int treeWanted;
    static int trees;

    public static void main(String[] args){
        IO io = new IO();

        height = io.nextInt();
        width = io.nextInt();
        treeWanted = io.nextInt();
        trees = 0;

        kentta = new int[width][height];


        //calculate trees and save it in to array
        for (int y=0; y<height; y++) {
            String line = io.next();
            for (int x=0; x<width; x++){
                if (line.charAt(x) == '*'){
                    kentta[x][y] = 1;
                    trees ++;
                }else{
                    kentta[x][y] = 0;
                }
            }
        }

        //optimized test area
        double density = (double) trees/ (double) (width*height);
        int optimizeW = Math.max((int) Math.ceil(Math.pow((treeWanted / density), 0.5)), 2);

        int foundArea = 0;

        int direction =0;

        //-1, koska oikea reuna turha kayda
        for(int y=0; y<height-1; y++){
            for (int x=0; x<width-1; x++){
                int testW = optimizeW;

                testW = Math.min(Math.min(testW, height-y), width-x);
                int amount = CountTree(testW, x,y);

                //haluttu määrä
                if (amount == treeWanted){
                    foundArea ++;
                    foundArea += IncreaseArea(testW, x, y);
                    foundArea += DecreaseArea(testW, x, y);
                } else if (amount < treeWanted){
                    foundArea += IncreaseArea(testW, x, y);
                } else { //amount > treeWanted
                    foundArea += DecreaseArea(testW, x, y);
                }

            }
        }

        io.println(foundArea);
        io.close();

    }

    private static int CountTree(int testW, int x, int y) {
        int squareTreeAmount = 0;
        for (int y2 = y; y2 < y + testW; y2++) {
            for (int x2 = x; x2 < x + testW; x2++) {
                squareTreeAmount += kentta[x2][y2];
            }
        }
        return squareTreeAmount;
    }

    private static int DecreaseArea(int testW, int x, int y){
        int foundArea = 0;
        testW --;
        while(testW >= 2){
            int squareTreeAmount = CountTree(testW, x, y);
            if(squareTreeAmount == treeWanted){
                foundArea ++;
            }
            testW --;
        }
        return foundArea;
    }

    private static int IncreaseArea(int testW, int x, int y){
        int foundArea = 0;
        testW ++;
        while(testW + y < height && testW + x < width){
            int squareTreeAmount = CountTree(testW, x, y);
            if(squareTreeAmount == treeWanted){
                foundArea ++;
            }
            testW ++;
        }
        return foundArea;
    }


}