あみだくじ
Posted feedbacks - C#
テキストの2行目から文字でループを回し、次の文字が"-"の時に、1行目の文字を入れ替えてます。 ループが終わったら1行目を最終行に足して、入れ替えた1行目を復元して出力してます。
see: 貧脚レーサーのサボり日記
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 | //http://ja.doukaku.org/103/ 投稿用
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
Amida(@"A B C D E
| | |-| |
|-| | |-|
| |-| |-|
|-| |-| |
|-| | | |");
Console.ReadLine();
}
static void Amida(string amidaText) {
string[] amidaTemp = amidaText.Split(new char[]{'\n'});
List<char[]> amidaRows = new List<char[]>();
amidaRows.Add(amidaTemp[0].ToCharArray());
for(int i = 1; i < amidaTemp.Length; i++) {
amidaRows.Add(amidaTemp[i].ToCharArray());
for(int j = 0; j < amidaTemp[i].Length; j = j + 2) {
if(j > 0 && amidaTemp[i][j - 1] == '-') {
char tmp;
tmp = amidaRows[0][j];
amidaRows[0][j] = amidaRows[0][j - 2];
amidaRows[0][j - 2] = tmp;
}
}
}
amidaRows.Add(amidaRows[0]);
amidaRows.RemoveAt(0);
amidaRows.Insert(0, amidaTemp[0].ToCharArray());
foreach(char[] line in amidaRows) {
Console.WriteLine(line);
}
}
}
|
殆ど書き直しです。エレガントに書けたと思います。
see: 貧脚レーサーのサボり日記
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 | //http://ja.doukaku.org/103/ 投稿用
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
Amida(@"A B C D E
| | |-| |
|-| | |-|
| |-| |-|
|-| |-| |
|-| | | |");
Console.ReadLine();
}
static void Amida(string amidaText) {
List<string> temp = new List<string>(amidaText.Split(new char[] { '\n' }));
List<char> lastLine = new List<char>(temp[0].ToCharArray());
foreach(string line in temp) {
Console.WriteLine(line);
for(int i = 1; i < line.Length; i = i + 2) {
if(line[i] == '-'){
lastLine.Reverse(i - 1, 3);
}
}
}
Console.WriteLine(lastLine.ToArray());
}
}
|
Playerオブジェクトにあみだくじを引かせてみました。この方が"このあみだくじをたどって"という題意に近いと思います。
see: 貧脚レーサーのサボり日記
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; //終わったら結果を記入
}
}
}
|


greentea #4476() Rating4/6=0.67
[ reply ]