paiza Aランクレベルアップメニューの C# へび問題回答例
paiza Aランクレベルアップメニューの C# へび問題を解いてみました。
using System; using System.Collections.Generic; class Program { static void Main() { // 自分の得意な言語で // Let's チャレンジ!! var arr = Console.ReadLine().Split(' '); var H = int.Parse(arr[0]); var W = int.Parse(arr[1]); var y = int.Parse(arr[2]); var x = int.Parse(arr[3]); var N = int.Parse(arr[4]); var map = new char[H,W]; for(int i=0;i<H;i++){ var tmpStr = Console.ReadLine(); for(int j=0;j<W;j++){ map[i,j] = tmpStr[j]; } } var direction = new Dictionary<int,char>(); for(int i=0;i<N;i++){ arr = Console.ReadLine().Split(' '); direction.Add(int.Parse(arr[0]),arr[1][0]); } var moveTo = 'N'; //first direction map[y,x] = '*'; //first position var time = 0; while(time < 100){ if(direction.ContainsKey(time)){ switch(moveTo){ case 'N': if(direction[time] == 'L'){ moveTo = 'W'; } else { moveTo = 'E'; } break; case 'E': if(direction[time] == 'L'){ moveTo = 'N'; } else { moveTo = 'S'; } break; case 'S': if(direction[time] == 'L'){ moveTo = 'E'; } else { moveTo = 'W'; } break; case 'W': if(direction[time] == 'L'){ moveTo = 'S'; } else { moveTo = 'N'; } break; } } switch(moveTo){ case 'N': if(y == 0) { time = 100; } else if(map[y-1,x] == '*' || map[y-1,x] == '#'){ time = 100; } else { map[y-1,x] = '*'; y--; } break; case 'E': if(x == W-1) { time = 100; } else if(map[y,x+1] == '*' || map[y,x+1] == '#'){ time = 100; } else { map[y,x+1] = '*'; x++; } break; case 'S': if(y == H-1) { time = 100; } else if(map[y+1,x] == '*' || map[y+1,x] == '#'){ time = 100; } else { map[y+1,x] = '*'; y++; } break; case 'W': if(x == 0) { time = 100; } else if(map[y,x-1] == '*' || map[y,x-1] == '#'){ time = 100; } else { map[y,x-1] = '*'; x--; } break; } time++; } for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ Console.Write(map[i,j]); } Console.WriteLine(); } } }