とんちゃんといっしょ

Cloudに関する技術とか日常とかについて書いたり書かなかったり

Cubes Without Holes

Cubes Without Holes


ikeが昨日ブログに書いてた問題を研修の休み時間にbyte型の3次元配列を使ったら、
メモリから溢れたので別の実装にしてみた。


研修先で書いたソースを家に帰ってから書き直してみたけど、
コードをサブミットするのにアカウントがいるらしいので困った。
とりあえず、こんな感じになったけどサンプルは動く。
最大のパターンはわかんないので、誰か通してみて欲しい。


以下ソースコード

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        solve();
    }

    static void solve(){
        Scanner sc = new Scanner(System.in);
        int n, h;

        for(;;){
            n = sc.nextInt();
            h = sc.nextInt();
            if(n==0&&h==0) break;

            Set<String> set = new TreeSet<String>();
            int ans = n * n * n;

            for(int i = 0; i < h; i++){
                String c = sc.next();
                int a = sc.nextInt();
                int b = sc.nextInt();

                String key = "";

                for(int j = 0; j < n; j++){
                    if("xy".equals(c)){
                        key = a + "," + b + "," + j;
                    }else if("xz".equals(c)){
                        key = a + "," + j + "," + b;
                    }else if("yz".equals(c)){
                        key = j + "," + a + "," + b;
                    }

                    if(!set.contains(key)){
                        set.add(key);
                        ans--;
                    }
                }
            }
            System.out.println(ans);
        }
    }
}