using System; using System.Diagnostics; namespace CodeIq { internal class Program { /// /// 太郎君はロボットを遠隔で操縦している。 /// /// このロボットは現在(0,0)の座標に立っていて北の方向を向いている。 /// 太郎君はいまこのロボットを(X,Y)の座標に移動させたいと思っている。 /// /// ロボットに出来る命令は、1回につき以下のうちいずれかの命令を選んで指示することができる。 /// ・時計回りに、90∘ その場で向き(進行方向)を変える。 /// ・反時計回りに、90∘ その場で向き(進行方向)を変える。 /// ・向いている方向に K距離だけ前進する。Kは、(1≤K≤L) の範囲で、命令のたびに指定することができる。 /// private static void Main() { // パラメータ取得 var line1 = Console.ReadLine(); var line2 = Console.ReadLine(); var line3 = Console.ReadLine(); //var line1 = "0"; //var line2 = "-1"; //var line3 = "1000000000"; if( string.IsNullOrEmpty( line1 ) ) return; if( string.IsNullOrEmpty( line2 ) ) return; if( string.IsNullOrEmpty( line3 ) ) return; var x = int.Parse( line1 ); var y = int.Parse( line2 ); var l = int.Parse( line3 ); var nowX = 0; var nowY = 0; var nowDir = Direction.North; var orderCount = 0; var getRedirectCount = new Func( ( dir, nextDir ) => { if( dir == nextDir ) return 0; else if( ( (int)dir % 2 == 0 ) != ( (int)nextDir % 2 == 0 ) ) return 1; else return 2; } ); var getMoveCount = new Func( ( pos, nextPos ) => { var addValue = ( pos < nextPos ) ? 1 : -1; var moveCount = 0; while( pos != nextPos ) { moveCount++; if( Math.Abs( nextPos - pos ) > l ) pos += addValue * l; else pos = nextPos; } return moveCount; } ); // 向き変更を最小限に抑える順番 // ①↑ ②(←、→) ③↓ var nextDirections = new Direction[3]; nextDirections[0] = Direction.North; nextDirections[1] = ( nowX < x ) ? Direction.West : Direction.East; nextDirections[2] = Direction.South; foreach( var nextDir in nextDirections ) { var isX = ( (int)nextDir % 2 == 0 ); var isPositive = ( nextDir > 0 ); var nowPos = isX ? nowX : nowY; var nextPos = isX ? x : y; // 移動する必要がない場合は処理しない if( nowPos == nextPos ) continue; // 対応する向きへの計算が不要の場合も処理しない if( isPositive && nowPos > nextPos ) continue; if( !isPositive && nowPos < nextPos ) continue; orderCount += getRedirectCount( nowDir, nextDir ); nowDir = nextDir; orderCount += getMoveCount( nowPos, nextPos ); if( isX ) nowX = x; else nowY = y; } Debug.Print( orderCount.ToString() ); Console.WriteLine( orderCount ); } /// /// 向きを表す列挙体
/// 奇数値の場合はY軸方向、偶数値の場合はX軸方向
/// 正数の場合は加算方向、負数値の場合は減算方向 ///
public enum Direction { North = 1, West = 2, South = -1, East = -2 } } }