ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1127. Colored Bricks

I have WA1, here is my code, help me, please!
Posted by [SPbSU ITMO] Dennis Yolkin 11 Aug 2007 02:45
I used dynamic programming for solving this problem, on the sample test program's output is correct, I have WA1 and do not know, were is the bug. Help me, please!

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Solution implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer strtok;

    int nextInt() throws Exception {
        while (strtok == null || !strtok.hasMoreTokens()) {
            strtok = new StringTokenizer(in.readLine());
        }
        return Integer.parseInt(strtok.nextToken());
    }

    int n;
    int[][] d;
    String[] c;
    int[][] pos = {{0, 1, 3, 2, 4, 5}, // stay on bottom
                  {0, 4, 3, 5, 1, 2},  // stay on left
                  {5, 1, 4, 2, 3, 0}};

    boolean match(int c1, int pos1, int c2, int pos2) {
        char[] s1 = new char[6], s2 = new char[6];
        for (int i = 0; i < 6; ++i) {
            s1[i] = c[c1].charAt(pos[pos1][i]);
            s2[i] = c[c2].charAt(pos[pos2][i]);
        }

        boolean ok1, ok2;
        for (int shift = 0; shift < 4; ++shift) {
            ok1 = true;
            ok2 = true;
            for (int i = 0; i < 4; ++i) {
                if (s1[i] != s2[(i + shift) % 4]) {
                    ok1 = false;
                    break;
                }
            }
            for (int i = 0; i < 4; ++i) {
                if (s1[3 - i] != s2[(i + shift) % 4]) {
                    ok2 = false;
                    break;
                }
            }
            if (ok1 || ok2) {
                return true;
            }
        }
        return false;
    }

    void solve() {
        d = new int[pos.length][n];
        for (int curbox = 0; curbox < n; ++curbox) {
            for (int curpos = 0; curpos < pos.length; ++curpos) {
                d[curpos][curbox] = 1;
                for (int prevbox = 0; prevbox < curbox; ++prevbox) {
                    for (int prevpos = 0; prevpos < pos.length; ++prevpos) {
                        if (match(prevbox, prevpos, curbox, curpos)) {
                            d[curpos][curbox] = Math.max(d[curpos][curbox], d[prevpos][prevbox] + 1);
                        }
                    }
                }
            }
        }
    }

    void outputDP() {
        for (int i = 0; i < pos.length; ++i) {
            for (int j = 0; j < n; ++j) {
                out.printf("%d ", d[i][j]);
            }
            out.println();
        }
    }

    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);

            n = nextInt();
            c = new String[n];
            for (int i = 0; i < n; ++i) {
                c[i] = in.readLine();
            }
            solve();
            //outputDP();

            int max = 0;
            for (int i = 0; i < pos.length; ++i) {
                max = Math.max(d[i][n - 1], max);
            }
            out.println(max);

            /*c = new String[2];
            c[0] = in.readLine();
            int aa = nextInt();
            c[1] = in.readLine();
            int bb = nextInt();
            out.println(match(0, aa, 1, bb));*/

        } catch (Exception er) {
            er.printStackTrace();
            System.exit(13);
        } finally {
            out.close();
        }
    }

    public static void main(String[] args) {
        new Thread(new Solution()).start();
    }
}
Re: I have WA1, here is my code, help me, please!
Posted by Sandro (USU) 11 Aug 2007 23:14
>I have WA1 and do not know, were is the bug

That was Timus bug. Old test#1 had a brick with 2 sides of the same color. Now test#1 is a sample test. You have TL now.
Re: I have WA1, here is my code, help me, please!
Posted by [SPbSU ITMO] Dennis Yolkin 12 Aug 2007 03:13
Thank you!