Comment detail

あみだくじ (Nested Flatten)

 Playerオブジェクトにあみだくじを引かせてみました。この方が"このあみだくじをたどって"という題意に近いと思います。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//http://ja.doukaku.org/103/ 投稿用
using System;
using System.Collections.Generic;
class Program2 {
    static void Main(string[] args) {
        string amidaStr = @"A B C D E
| | |-| |
|-| | |-|
| |-| |-|
|-| |-| |
|-| | | |";

        List<char[]> amida = new List<char[]>();

        //あみだをPlayerが解釈できる形式に変換
        foreach(string line in amidaStr.Split(new char[] { '\n' })) {
            amida.Add(line.ToCharArray());
        }
        amida.Add(new char[amida[0].Length]);//Playlerが回答を記入する欄

        //Player生成
        for(int i = 0; i < amida[0].Length; i = i + 2) {
            new Player(i, amida);
        }

        //出力
        foreach(char[] line in amida) {
            Console.WriteLine(line);
        }
        Console.ReadLine();
    }
}

class Player {
    public Player(int start,List<char[]> amida) {
        Play(start, 1, amida, amida[0][start]);     
    }

    private void Play(int x,int y, List<char[]> amida, char name){
        if(amida[y][x] == '|') { //あみだが終わっていなければ
            if(x >= 2 && amida[y][x - 1] == '-') {
                Play(x - 2, y + 1, amida, name); //左に移動して進む
            } else if(x <= amida[0].Length - 3 && amida[y][x + 1] == '-') {
                Play(x + 2, y + 1, amida, name); //右に移動して進む
            } else {
                Play(x, y + 1, amida, name); //移動せずに下に進む
            }
        } else {
            amida[y][x] = name; //終わったら結果を記入
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...