📄 myanswer.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using GrapeCity.Competition.TreasureHouse;
using System.Drawing;
using System.Diagnostics;
namespace Sample
{
public class MyAnswer : IExplorer
{
#region IExplorer Members
public List<Direction> Explore(IMap map)
{
Stopwatch sw = Stopwatch.StartNew();
List<Direction> result = new List<Direction>();
int halfMaxStep = map.MaxStep / 2;
Point current = new Point(0, 0);
int currentStep = 0;
Random r = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
while (currentStep < halfMaxStep)
{
Direction d = this.GetRandomDirection(r);
if (this.CanMove(map, current, d))
{
result.Add(d);
current = this.Move(current, d);
currentStep++;
}
if (sw.ElapsedMilliseconds > 9000)
{
break;
}
}
for (int i = result.Count - 1; i >= 0; i--)
{
result.Add(this.ReverseDirection(result[i]));
}
return result;
}
#endregion
private Direction GetRandomDirection(Random r)
{
switch (r.Next(4))
{
case 0:
return Direction.Left;
case 1:
return Direction.Up;
case 2:
return Direction.Right;
case 3:
return Direction.Down;
default:
throw new InvalidOperationException();
}
}
private bool CanMove(IMap map, Point current, Direction d)
{
Point next = this.Move(current, d);
if (next.X < 0 || next.X >= map.Size.Width)
{
return false;
}
if (next.Y < 0 || next.Y >= map.Size.Height)
{
return false;
}
if (map[next.X, next.Y] == Tag.Wall)
{
return false;
}
return true;
}
private Point Move(Point current, Direction d)
{
switch (d)
{
case Direction.Up:
return new Point(current.X, current.Y - 1);
case Direction.Down:
return new Point(current.X, current.Y + 1);
case Direction.Left:
return new Point(current.X - 1, current.Y);
case Direction.Right:
return new Point(current.X + 1, current.Y);
default:
throw new InvalidOperationException();
}
}
private Direction ReverseDirection(Direction d)
{
switch (d)
{
case Direction.Up:
return Direction.Down;
case Direction.Down:
return Direction.Up;
case Direction.Left:
return Direction.Right;
case Direction.Right:
return Direction.Left;
default:
throw new InvalidOperationException();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -