Пример
Ввод
Hero is (2,2)
Field is (20,20)
Obstacle is (2,4), position is (4,15)
Moves:
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,0)
(0,-1)
(-1,-1)
(-1,-1)
(-1,0)
(-1,1)
(-1,1)
(0,1)
(0,1)
(0,1)
(0,1)
(0,1)
(0,1)
(1,0)
(1,0)
(0,-1)
(1,0)
(1,0)
(-1,-1)
(1,-1)
(1,-1)
(1,-1)
(1,-1)
(1,-1)
(0,-1)
(1,0)
(1,0)
(0,-1)
(0,-1)
(0,-1)
(1,-1)
(1,-1)
(1,-1)
(1,-1)
(1,0)
(1,0)
(1,0)
(1,0)
(1,0)
(1,0)
(1,0)
(1,0)
(0,1)
(1,1)
(1,1)
(1,1)
(1,1)
(0,1)
(0,1)
(0,1)
(-1,1)
(-1,1)
(1,1)
(1,1)
(1,1)
(1,1)
(1,1)
(1,1)
(0,1)
(0,1)
(0,-1)
(0,-1)
(-1,0)
(-1,1)
(1,0)
(1,-1)
(1,0)
(1,1)
(1,1)
(0,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
(-1,1)
Stop
Вывод
10, 10
9, 11
8, 12
7, 13
6, 14
We got collision by X
6, 14
6, 13
We got collision by X
6, 12
5, 11
4, 11
3, 12
We got collision by Y
2, 12
2, 13
2, 14
2, 15
2, 16
2, 17
2, 18
3, 18
4, 18
We got collision by Y
4, 18
5, 18
6, 18
We got collision by X and Y
6, 18
7, 17
8, 16
9, 15
10, 14
11, 13
11, 12
12, 12
13, 12
13, 11
13, 10
13, 9
14, 8
15, 7
16, 6
17, 5
18, 5
19, 5
20, 5
Teleported
1, 5
2, 5
3, 5
4, 5
5, 5
5, 6
6, 7
7, 8
8, 9
9, 10
9, 11
9, 12
9, 13
8, 14
7, 15
8, 16
9, 17
10, 18
11, 19
12, 20
Teleported
13, 1
13, 2
13, 3
13, 2
13, 1
12, 1
11, 2
12, 2
13, 1
14, 1
15, 2
16, 3
16, 4
15, 5
14, 6
13, 7
12, 8
11, 9
10, 10
9, 11
8, 12
7, 13
6, 14
We got collision by X
6, 15
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NTO
{
public partial class NTO
{
Hero hero;
Field field;
public static void Main(string[] args)
{
List<string> inputLines = new List<string>();
string line;
while ((line = Console.ReadLine()) != "Stop")
{
inputLines.Add(line);
}
string heroLine = inputLines[0].Replace("Hero is ", "").Replace("(", "").Replace(")", "");
var heroDimensions = heroLine.Split(',').Select(int.Parse).ToArray();
Hero hero = new Hero(heroDimensions[0], heroDimensions[1]);
string fieldLine = inputLines[1].Replace("Field is ", "").Replace("(", "").Replace(")", "");
var fieldDimensions = fieldLine.Split(',').Select(int.Parse).ToArray();
Field field = new Field(fieldDimensions[0], fieldDimensions[1], hero);
hero.x_position = field.width / 2;
hero.y_position = field.height / 2;
int index = 2;
while (!inputLines[index].StartsWith("Moves:"))
{
string obstacleLine = inputLines[index]
.Replace("Obstacle is ", "")
.Replace("position is ", "")
.Replace("(", "")
.Replace(")", "");
var parts = obstacleLine.Split(',');
int obstacleWidth = int.Parse(parts[0]);
int obstacleHeight = int.Parse(parts[1]);
int obstacleX = int.Parse(parts[2]);
int obstacleY = int.Parse(parts[3]);
field.obstacles.Add(new Obstacle(obstacleWidth, obstacleHeight, obstacleX, obstacleY));
index++;
}
hero.OnUpdate();
for (int i = index + 1; i < inputLines.Count; i++)
{
string moveLine = inputLines[i].Replace("(", "").Replace(")", "");
var moveParts = moveLine.Split(',').Select(int.Parse).ToArray();
DirectionVector moveVector = (moveParts[0], moveParts[1]);
field.MoveHero(moveVector);
hero.OnUpdate();
}
}
}
public class Field
{
public int width;
public int height;
public Hero hero;
public List<Obstacle> obstacles = new List<Obstacle> { };
public Field(int _width, int _height, Hero _hero)
{
width = _width;
height = _height;
hero = _hero;
}
// Rectangle structure for collision detection
private struct Rectangle
{
public int left;
public int right;
public int bottom;
public int top;
}
// Helper method to get rectangle bounds based on center position
private Rectangle GetRectangle(int x, int y, int width, int height)
{
return new Rectangle
{
left = x - width / 2,
right = x + width / 2,
bottom = y - height / 2,
top = y + height / 2
};
}
// Helper method to check if two rectangles overlap
private bool Overlaps(Rectangle a, Rectangle b)
{
return a.left < b.right &&
a.right > b.left &&
a.bottom < b.top &&
a.top > b.bottom;
}
public void MoveHero(DirectionVector vector)
{
int dx = (int)vector.x_dir;
int dy = (int)vector.y_dir;
bool collision_x = false;
bool collision_y = false;
bool teleport = false;
// Current hero position
int current_x = hero.x_position;
int current_y = hero.y_position;
// Attempt to move in X direction
if (dx != 0)
{
int new_x = current_x + dx;
bool wrapped_x = false;
// Handle wrap-around for X
if (new_x > width)
{
new_x = 1;
wrapped_x = true;
}
else if (new_x < 1)
{
new_x = width;
wrapped_x = true;
}
// Create hypothetical rectangle after moving in X
Rectangle hypothetical_hero_rect_x = GetRectangle(new_x, current_y, hero.width, hero.height);
// Check collision with all obstacles
foreach (var obstacle in obstacles)
{
Rectangle obstacle_rect = GetRectangle(obstacle.x_position, obstacle.y_position, obstacle.width, obstacle.height);
if (Overlaps(hypothetical_hero_rect_x, obstacle_rect))
{
collision_x = true;
break;
}
}
if (!collision_x)
{
// Move in X direction
hero.x_position = new_x;
if (wrapped_x)
{
teleport = true;
}
}
}
// Attempt to move in Y direction
if (dy != 0)
{
int new_y = current_y + dy;
bool wrapped_y = false;
// Handle wrap-around for Y
if (new_y > height)
{
new_y = 1;
wrapped_y = true;
}
else if (new_y < 1)
{
new_y = height;
wrapped_y = true;
}
// Create hypothetical rectangle after moving in Y
Rectangle hypothetical_hero_rect_y = GetRectangle(hero.x_position, new_y, hero.width, hero.height);
// Check collision with all obstacles
foreach (var obstacle in obstacles)
{
Rectangle obstacle_rect = GetRectangle(obstacle.x_position, obstacle.y_position, obstacle.width, obstacle.height);
if (Overlaps(hypothetical_hero_rect_y, obstacle_rect))
{
collision_y = true;
break;
}
}
if (!collision_y)
{
// Move in Y direction
hero.y_position = new_y;
if (wrapped_y)
{
teleport = true;
}
}
}
// Determine and print collision messages
if (collision_x && collision_y)
{
Console.WriteLine("We got collision by X and Y");
}
else if (collision_x)
{
Console.WriteLine("We got collision by X");
}
else if (collision_y)
{
Console.WriteLine("We got collision by Y");
}
// Print teleport message if applicable
if (teleport)
{
Console.WriteLine("Teleported");
}
}
}
public class Obstacle
{
public int width;
public int height;
public int x_position;
public int y_position;
public Obstacle(int _width, int _height, int _x, int _y)
{
width = _width;
height = _height;
x_position = _x;
y_position = _y;
}
}
public class Hero
{
public int width;
public int height;
public int x_position;
public int y_position;
public Hero(int _width, int _height)
{
width = _width;
height = _height;
x_position = width / 2;
y_position = height / 2;
}
public void OnUpdate()
{
Console.WriteLine(String.Format("{0}, {1}", x_position, y_position));
}
}
public class DirectionVector
{
public enum HorizontalDirection
{
left = -1,
stay = 0,
right = 1
}
public enum VerticalDirection
{
down = -1,
stay = 0,
up = 1
}
public HorizontalDirection x_dir { get; set; }
public VerticalDirection y_dir { get; set; }
public DirectionVector()
{
x_dir = HorizontalDirection.stay;
y_dir = VerticalDirection.stay;
}
public DirectionVector((int, int) directions)
{
x_dir = (HorizontalDirection)directions.Item1;
y_dir = (VerticalDirection)directions.Item2;
}
public static implicit operator DirectionVector((int, int) directions)
{
return new DirectionVector(directions);
}
public static implicit operator (int, int)(DirectionVector vector)
{
return ((int)vector.x_dir, (int)vector.y_dir);
}
public override string ToString()
{
return $"({(int)x_dir}, {(int)y_dir})";
}
}
}