結果
| 問題 |
No.48 ロボットの操縦
|
| コンテスト | |
| ユーザー |
velfare_nagata
|
| 提出日時 | 2016-10-04 10:02:09 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,979 bytes |
| コンパイル時間 | 2,491 ms |
| コンパイル使用メモリ | 110,944 KB |
| 実行使用メモリ | 25,984 KB |
| 最終ジャッジ日時 | 2024-11-21 15:33:10 |
| 合計ジャッジ時間 | 9,465 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 WA * 6 TLE * 1 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Diagnostics;
namespace CodeIq
{
internal class Program
{
/// <summary>
/// 太郎君はロボットを遠隔で操縦している。
///
/// このロボットは現在(0,0)の座標に立っていて北の方向を向いている。
/// 太郎君はいまこのロボットを(X,Y)の座標に移動させたいと思っている。
///
/// ロボットに出来る命令は、11回につき以下のうちいずれかの命令を選んで指示することができる。
/// ・時計回りに、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 = "-7";
//var line2 = "15";
//var line3 = "7";
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 ) != ( (int)nextDir % 2 ) )
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 nowPos = isX ? nowX : nowY;
var nextPos = isX ? x : y;
if( nowPos == nextPos )
continue;
orderCount += getRedirectCount( nowDir, nextDir );
nowDir = Direction.North;
orderCount += getMoveCount( nowPos, nextPos );
if( isX )
nowX = x;
else
nowY = y;
}
Debug.Print( orderCount.ToString() );
Console.WriteLine( orderCount );
}
/// <summary>
/// 向きを表す列挙体<br />
/// 奇数値の場合はY軸方向、偶数値の場合はX軸方向
/// </summary>
public enum Direction
{
North = 1,
West = 2,
South = 3,
East = 4
}
}
}
velfare_nagata