0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

オフラインリアルタイムどう書くF01の問題の Java による実装例

Posted at

問題 : http://nabetani.sakura.ne.jp/hena/ordf01_twicel/
実装リンク集 : http://qiita.com/Nabetani/items/8e02ede04315b4eadd6d

Java のことをよく知らないので変なことを書いているかもしれないんだけど、気にせず投稿。

import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class TwinCell {
    private static class Solver {
        private class Searcher
        {
            int c=0;
            int col;
            Searcher( int x, int y)
            {
                col = color(x,y);
                search( x, y );
            }
            void search( int x, int y )
            {
                if ( col != color(x,y) || ( attended[y] & (1<<x)) != 0 ){
                    return;
                }
                c+=1;
                attended[y] |= 1<<x;
                search( x+1, y );
                search( x-1, y );
                search( x, y+1 );
                search( x, y-1 );
            }
            int size()
            {
                return c;
            }
        }
        int[] bits;
        int[] attended = new int[8];
        static final int WX = 8;
        static final int WY = 8;
        public Solver(int[] _bits)
        {
            bits = _bits;
        }
        boolean onField( int x, int y)
        {
            return !( x<0 || WX<=x || y<0 || WY<=y );
        }
        private int color( int x, int y ){
            if ( !onField( x, y )){
                return -1;
            }
            return (bits[y] & (1<<x) )==0 ? 0 : 1;
        }
        public int[] solve() {
            int[] r = new int[2];
            for( int y=0 ; y<WY ; ++y ){
                for( int x=0 ; x<WX ; ++x ){
                    if ( new Searcher( x, y ).size()==2 ){
                        ++r[color(x,y)];
                    }
                }
            }
            return r;
        }
    }
    public String solve( String src )
    {
        String[] lines = src.split("\\/");
        int[] bits = new int[lines.length];
        for( int i =0 ; i<lines.length ; ++i ){
            bits[i]=Integer.parseInt(lines[i], 16);
        }
        int[] wb = new Solver( bits ).solve();
        return String.format( "%d,%d", wb[0], wb[1] );
    }

    void test( String src, String expected )
    {
        Assert.assertEquals( expected, solve( src ));
    }
    @Test
    void runAllTests()
    {
        /*0*/ test( "dc/bc/a7/59/03/d5/d4/ea", "2,3" );
        /*1*/ test( "ff/ff/ff/ff/ff/ff/ff/ff", "0,0" );
        /*2*/ test( "00/00/00/00/00/00/00/00", "0,0" );
    }
}

rubyとC99 のとき( http://qiita.com/Nabetani/items/7814edd97911a80946f1 )とは異なり、深さ優先探索でマス目の数を数えることにした。

生まれて初めてクラス内クラス内クラスを使ってみた。
あと、static じゃない inner class という珍しいものも使ってみた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?