~cpp 

package RandomWalk;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class RandomWalk {
    private int board[][];
    private String schedule;
    private int curX, curY;
    private int size;
    private int dirX[] = {0,1,1,1,0,-1,-1,-1};
    private int dirY[] = {-1,-1,0,1,1,1,0,-1};
    
    public RandomWalk() {
        walk();
        result();
    }
    

    private void walk() {
        readData();
        goWithScheduledDirection();
    }
    
    private void goWithScheduledDirection() {
        // 시작위치
        ++board[curX][curY];
        
        // 스케쥴된 방향에 따라 이동..
        for(int i=0; i<schedule.length(); ++i) {
            char c = schedule.charAt(i);
            
            if( c == '9' )
                return;
            
            int idx = (int)(c - '0');
            
            curX += dirX[idx];
            if( curX<0 ) curX = size-1;	// warp
            else if( curX >= size ) curX = 0; // warp

            curY += dirY[idx];
            if( curY<0 ) curY = size-1;	// warp
            else if( curY >= size ) curY = 0; //warp
            
            ++board[curX][curY];
        }
    }

    private void readData() {
        try {
            DataInputStream din 
            	= new DataInputStream(new FileInputStream(new File("input2.txt")));
            size = Integer.parseInt(din.readLine());
            board = new int[size][size];
            
            // 시작위치
            String pos = din.readLine();
            String startPoint[] = pos.split(" ");
            curX = Integer.parseInt(startPoint[0]);
            curY = Integer.parseInt(startPoint[1]);
            
            emptyBoard();
            
            schedule = din.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void emptyBoard() {
        for(int i=0; i<size; ++i) {
            for(int j=0; j<size; ++j) {
                board[i][j] = 0;
            }
        }
    }


    private void result() {
        for(int i=0; i<size; ++i) {
            for(int j=0; j<size; ++j ) {
                System.out.print(board[j][i] + " ");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        new RandomWalk();
    }
}

Retrieved from http://wiki.zeropage.org/wiki.php/ScheduledWalk/임인택
last modified 2021-02-07 05:27:59