How to write offline real-time Java implementation example of F01 problem

Problem: http://nabetani.sakura.ne.jp/hena/ordf01_twicel/ Implementation links: http://qiita.com/Nabetani/items/8e02ede04315b4eadd6d

I don't know much about Java, so I may be writing something strange, but I posted it without worrying about it.

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" );
    }
}

Unlike the case of ruby and C99 (http://qiita.com/Nabetani/items/7814edd97911a80946f1), I decided to count the number of squares in the depth-first search.

I tried using the in-class in-class for the first time in my life. I also tried using a rare inner class that is not static.

Recommended Posts

How to write offline real-time Java implementation example of F01 problem
Offline real-time how to write F06 implementation example
Offline real-time how to write F03 ruby and C implementation example
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
How to write offline real time Implementation example by ruby and C99 of F04
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
How to write java comments
Comparison of how to write Callback function (Java, JavaScript, Ruby)
Studying Java # 6 (How to write blocks)
How to write Java variable declaration
Basics of Java development ~ How to write programs (variables and types) ~
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
[Java] [Maven3] Summary of how to use Maven3
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
JDBC promises and examples of how to write
[java] Summary of how to handle character strings
How to write offline 15th reference question answer
Java Development Basics ~ How to Write Programs * Exercise 1 ~
[Java] Summary of how to abbreviate lambda expressions
How to solve an Expression Problem in Java
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
How to write Java String # getBytes in Kotlin?
Basics of Java development ~ How to write a program (flow and conditional branching) ~
[Java] How to use compareTo method of Date class
How to write Rails
Initialization of for Try to make Java problem TypeScript 5-4
java: How to write a generic type list [Note]
[Java] How to get the maximum value of HashMap
As of April 2018 How to get Java 8 on Mac
How to write Mockito
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
How to write migrationfile
How to write modern Java. Immutable Java, consider Null safety.
How to execute WebCamCapture sample of NyARToolkit for Java
An unsupported Java version How to get rid of errors
How to use trained model of tensorflow2.0 with Kotlin / Java
How to derive the last day of the month in Java
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to write good code
Java --How to make JTable
Bit Tetris (how to write)
[Refactoring] How to write routing
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to use Java Map
How to set Java constants
How to write Junit 5 organized