大家好。问题太长了,无法将其粘贴到此处,因此我将粘贴一个URL。我得到50%的无效回报。我添加了一个检查,看看猫是否陷入僵局,无法在该牢房中收集食物/灵魂。我从早上6点开始就解决这个问题,现在是现在的11点,这让我感到沮丧。
简而言之,问题在于: 您的任务是计算Kitty收集的食物和灵魂,或者输出她陷入僵局的情况。
在输入的第一行,您将以字符串形式接收编码器灵魂(“ @”),食物(“ *”)和死锁(“ x”)的位置。
在输入的第二行,您将收到Kitty的路径,该字符串为字符串,整数之间用单个空格分隔。正值表示向右移动,负值表示向左移动。
起始位置始终位于索引0。
最终结果是灵魂,食物和僵局的计数或一串通知Kitty陷入僵局的字符串。格式显示在零测试和示例中。
@-编码器灵魂的符号 *-食物的符号 x-死锁符号
以下更多详细信息: https://judge.telerikacademy.com/problem/30kitty
string input = Console.ReadLine();
int deadlocks = 0;
string input2 = Console.ReadLine();
string[] output = input2.Split(' ');
int position = 0;
int startposition = 0;
int codersoulscollected = 0;
int foodcollected = 0;
int iterations = Math.Max(input.Length, output.Length);
bool[] isCollected = new bool[input.Length];
for (int i = 0; i <= iterations; i++)
{
startposition += position;
if (startposition < 0)
{
startposition = input.Length + startposition;
}
if (startposition >= input.Length)
{
startposition = startposition - input.Length;
}
char a = input[startposition];
if (a == '@' && (isCollected[startposition] == false))
{
codersoulscollected++;
isCollected[startposition] = true;
}
if (a == '*' && (isCollected[startposition] == false))
{
foodcollected++;
isCollected[startposition] = true;
}
if (a == 'x' && (isCollected[startposition] == false))
{
deadlocks++;
if (startposition % 2 == 0)
{
codersoulscollected--;
isCollected[startposition] = true;
}
else
{
foodcollected--;
isCollected[startposition] = true;
}
}
else if (a == 'x' && (isCollected[startposition] == true))
{
if (startposition % 2 == 0)
{
codersoulscollected++;
}
else
{
foodcollected++;
}
}
if (output.Length == i)
{
break;
}
position = int.Parse(output[i]);
if (foodcollected < 0 || codersoulscollected < 0)
{
Console.WriteLine("You are deadlocked, you greedy kitty!");
Console.WriteLine($"Jumps before deadlock: {i}");
return;
}
}
if (foodcollected >= 0 || codersoulscollected >= 0)
{
Console.WriteLine($"Coder souls collected: {codersoulscollected}\r\nFood collected: {foodcollected}\r\nDeadlocks: {deadlocks}");
}
答案 0 :(得分:0)
由于我有一些时间在手,所以我为您编写了一个简单的解决方案,以OOP的方式逐步引导您。希望您也能看到您的问题。
这是你的猫。它可以在给定的路径上走一定数量的步骤。它还收集食物和灵魂等。
public class Cat
{
/// <summary>
/// Amount of collected coder souls.
/// </summary>
private int _coderSouls;
/// <summary>
/// Amount of collected food.
/// </summary>
private int _food;
/// <summary>
/// Amount of deadlocks collected.
/// </summary>
private int _deadlocks;
/// <summary>
/// Number of jumps before deadlocking.
/// Starts from -1 because When we set the path
/// The kitty starts from the 0th tile.
/// </summary>
private int _numberOfJumps = -1;
/// <summary>
/// If Cat can still move.
/// </summary>
private bool _deadLocked;
/// <summary>
/// Path to follow.
/// </summary>
private Path _path;
/// <summary>
/// Creates a Kitty
/// </summary>
/// <param name="path">Path for Kitty</param>
public Cat(Path path)
{
SetPath(path);
}
/// <summary>
/// Set the path for Kitty to follow.
/// </summary>
/// <param name="path">Path to follow.</param>
private void SetPath(Path path)
{
_path = path;
Walk(0);
}
/// <summary>
/// Walks the Kitty with the given amount of steps.
/// </summary>
/// <param name="step">Amount of steps</param>
/// <returns>If kitty can move any more.</returns>
public bool Walk(int step)
{
// If Kitty is deadlocked it can not move any more
if (_deadLocked)
{
return false;
}
// Walks the cat with the given step amount
var got = _path.MoveToNext(step);
// Increase the number of Jumps
_numberOfJumps++;
// Rule written in the question
switch (got)
{
case ItemType.CoderSoul:
_coderSouls++;
break;
case ItemType.Food:
_food++;
break;
case ItemType.DeadLock:
_deadlocks++;
var isEven = _path.GetPosition() % 2 == 0;
if (isEven)
{
if (_coderSouls > 0)
{
_coderSouls--;
return true;
}
_deadLocked = true;
return false;
}
if (_food > 0)
{
_food--;
return true;
}
_deadLocked = true;
return false;
}
return true;
}
/// <summary>
/// When Kitty finished moving, Gets Summary.
/// </summary>
/// <returns>Summary of movemebt</returns>
public string Summarize()
{
return _deadLocked ? PrepareDeadLockMessage() : PrepareSummaryMessage();
}
/// <summary>
/// Deadlock message.
/// </summary>
/// <returns>Deadlock message.</returns>
private string PrepareDeadLockMessage()
{
return $"You are deadlocked, you greedy kitty!{Environment.NewLine}Jumps before deadlock: {_numberOfJumps}";
}
/// <summary>
/// Normal finish.
/// </summary>
/// <returns>Normal finish.</returns>
private string PrepareSummaryMessage()
{
return $"Coder souls collected: {_coderSouls}{Environment.NewLine}Food collected: {_food}{Environment.NewLine}Deadlocks: {_deadlocks}";
}
}
这是你的路。您必须按照问题中的说明进行解析。
public class Path
{
private readonly Item[] path;
private int _currentIndex;
public Path(string pathElements)
{
path = pathElements.Select(t => new Item(t)).ToArray();
_currentIndex = 0;
}
public ItemType MoveToNext(int increase)
{
_currentIndex += increase;
if (_currentIndex > path.Length)
{
_currentIndex -= path.Length;
}
if (_currentIndex < 0)
{
_currentIndex += path.Length;
}
return path[_currentIndex].Collect();
}
public int GetPosition()
{
return _currentIndex;
}
}
这是您给定单元格中的单个项目。
public class Item
{
/// <summary>
/// Kitty already collected this cell or not?
/// </summary>
public bool IsCollected { get; private set; }
/// <summary>
/// ItemType in this cell
/// </summary>
public ItemType ItemType { get; }
/// <summary>
/// Represents a single item in each cell.
/// </summary>
/// <param name="c">Type of the item decided by char.</param>
public Item(char c)
{
switch (c)
{
case '@':
ItemType = ItemType.CoderSoul;
break;
case '*':
ItemType = ItemType.Food;
break;
case 'x':
ItemType = ItemType.DeadLock;
break;
}
}
/// <summary>
/// Collect the item in this cell.
/// </summary>
/// <returns>The collected item.</returns>
public ItemType Collect()
{
if (IsCollected)
{
return ItemType.None;
}
IsCollected = true;
return ItemType;
}
}
最后这是可以在每个单元格中包含的ItemTypes
/// <summary>
/// The type of item located in each single cell.
/// </summary>
public enum ItemType
{
None,
CoderSoul,
Food,
DeadLock,
}
这是您使用此示例的方式。请通过调试器完成每个步骤。
var cat = new Cat(new Path("x@*@*@*"));
var walkinOrder = "1 -1 -1 4";
var intOrder = walkinOrder.Split(' ').Select(int.Parse);
foreach (var step in intOrder) {
if (cat.Walk(step) == false)
{
break;
}
}
Console.WriteLine(cat.Summarize());