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

Compiler report

input/Tontti.java:107: error: unmappable character for encoding UTF8
                //molemmat suunnat ovat mahdollisia --> kokeillaan kaikki l?hell? olevat
                                                                           ^
input/Tontti.java:107: error: unmappable character for encoding UTF8
                //molemmat suunnat ovat mahdollisia --> kokeillaan kaikki l?hell? olevat
                                                                                ^
2 errors

Code

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

    static int height;
    static int width;
    static int[][] kenttaSumma;
    static int wantedTrees;
    //static long runAmount;

    public static void main(String[] args){

        /*//todo
        int futureWidth = 500;
        int futureHeight = 500;
        int futurewantedTrees = (int) (Math.random() * futureHeight);
        height = futureHeight;
        width = futureWidth;
        wantedTrees = futurewantedTrees;

        kentta = new byte[width+1][height+1];
        kenttaSumma = new int[width+1][height+1];

        String[] lines = new String[height+1];
        for (int y=1; y<=height; y++) {
            //todo
            lines[y] = "";
            for (int x2=1; x2<=width; x2++){
                if (Math.random() > 0.3){
                    lines[y] = lines[y] + ".";
                }else {
                    lines[y] = lines[y] + "*";
                }
            }
        }*/


        IO io = new IO();

        height = io.nextInt();
        width = io.nextInt();
        wantedTrees = io.nextInt();

        //long aika = System.nanoTime();


        kenttaSumma = new int[width+1][height+1];

        int foundArea = 0;
        int minSize = (int) (Math.sqrt(wantedTrees) + 0.99);

        //calculate trees and save it in to array and summed area table
        for (int y=1; y<=height; y++) {
            String line = io.next();
            //String line = lines[y];

            int sumRow = 0;
            for (int x=1; x<=width; x++){
                if (line.charAt(x-1) == '*'){
                    sumRow ++;
                }
                kenttaSumma[x][y] = sumRow + kenttaSumma[x][y-1];

                int maxSize = min(x, y);

                if (x==4 && y==3){
                    x=4;
                }

                if(maxSize >= minSize){
                    foundArea += countAreasB(maxSize,minSize,x,y,0);
                }

            }
        }

        io.println(foundArea);

        /*aika = (System.nanoTime()-aika)/1000000;
        io.println("---");
        io.println("Time: " + aika);
        io.println("Loops: " + runAmount);
        */

        io.close();
    }

    //rekursiivinen binaarihaku
    // x2 = x -1
    // y2 = y -1
    private static int countAreasB(int maxSize,int minSize, int x, int y, int prevSize){
        int areasFound = 0;
        int tSize = (int) ((maxSize + minSize)/2.0 + 0.99);

        //lopetaan kun sama on jo katsottu
        if (prevSize != tSize){
            prevSize = tSize;

            int sum = kenttaSumma[x][y]+kenttaSumma[x-tSize][y-tSize]-kenttaSumma[x][y-tSize]-kenttaSumma[x-tSize][y];

            if(sum > wantedTrees){
                areasFound += countAreasB(tSize, minSize, x, y, prevSize);
            }else if (sum < wantedTrees){
                areasFound += countAreasB(maxSize, tSize, x, y, prevSize);
            }else {
                //molemmat suunnat ovat mahdollisia --> kokeillaan kaikki lähellä olevat
                areasFound ++;
                areasFound += countAreasL(tSize+1, maxSize, x, y, 1);
                areasFound += countAreasL(tSize-1, minSize, x, y, -1);
            }
        }
        
        return areasFound;
    }

    //suoraviivainen haku
    // x2 = x-1
    // y2 = y-1
    private static int countAreasL(int startSize,int endSize, int x, int y, int direction){
        int areasFound = 0;

        for (int tSize=startSize; direction*tSize<=endSize*direction; tSize=tSize+direction){

            int sum = kenttaSumma[x][y]+kenttaSumma[x-tSize][y-tSize]-kenttaSumma[x][y-tSize]-kenttaSumma[x-tSize][y];
            if (sum == wantedTrees){
                areasFound ++;
            }else{
                break;
            }

        }

        return areasFound;
    }

    //just for lulz
    private static int max(final int a, final int b){
        return (a>b)?a:b;
    }
    private static int min(final int a, final int b){
        return (a<b)?a:b;
    }


}