結果

問題 No.48 ロボットの操縦
ユーザー velfare_nagatavelfare_nagata
提出日時 2016-10-04 10:15:04
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,354 bytes
コンパイル時間 3,578 ms
コンパイル使用メモリ 107,312 KB
実行使用メモリ 111,816 KB
最終ジャッジ日時 2023-08-13 21:25:33
合計ジャッジ時間 10,572 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
22,884 KB
testcase_01 AC 56 ms
22,876 KB
testcase_02 AC 56 ms
23,040 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Diagnostics;

namespace CodeIq
{
	internal class Program
	{
		/// <summary>
		/// 太郎君はロボットを遠隔で操縦している。
		/// 
		/// このロボットは現在(0,0)の座標に立っていて北の方向を向いている。
		/// 太郎君はいまこのロボットを(X,Y)の座標に移動させたいと思っている。
		/// 
		/// ロボットに出来る命令は、1回につき以下のうちいずれかの命令を選んで指示することができる。
		/// ・時計回りに、90∘ その場で向き(進行方向)を変える。
		/// ・反時計回りに、90∘ その場で向き(進行方向)を変える。
		/// ・向いている方向に K距離だけ前進する。Kは、(1≤K≤L) の範囲で、命令のたびに指定することができる。
		/// </summary>
		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<Direction, Direction, int>( ( 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<int, int, int>( ( 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 );
		}

		/// <summary>
		/// 向きを表す列挙体<br />
		/// 奇数値の場合はY軸方向、偶数値の場合はX軸方向<br />
		/// 正数の場合は加算方向、負数値の場合は減算方向
		/// </summary>
		public enum Direction
		{
			North = 1,
			West = 2,
			South = -1,
			East = -2
		}
	}
}
0