-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
57 lines (43 loc) · 2.23 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
namespace Queens_Attack_II {
class Program {
public static int QueensAttack(int boardLength, int boardObstacles, int queenRowPosition, int queenColPosition, HashSet<(int, int)> obstacles) {
int count = 0;
// Define the eight possible directions the queen can attack
int[] rowDirection = {-1, -1, 0, 1, 1, 1, 0, -1 };
int[] colDirection = { 0, 1, 1, 1, 0, -1, -1, -1 };
// Iterate through each direction
for (int i = 0; i < 8; i++) {
int row = queenRowPosition + rowDirection[i];
int col = queenColPosition + colDirection[i];
while (row >= 1 && row <= boardLength && col >= 1 && col <= boardLength) {
if (obstacles.Contains((row, col))) {
break; // Obstacle found, stop checking in this direction
} else {
count++; // Obstacle not found, the queen can attack this square
}
// Move to the next square in the current direction
row += rowDirection[i];
col += colDirection[i];
}
}
return count;
}
static void Main(string[] args) {
string[] boardParameters = Console.ReadLine().Trim().Split(' ');
int boardLength = Convert.ToInt32(boardParameters[0]);
int boardObstacles = Convert.ToInt32(boardParameters[1]);
string[] queenParameters = Console.ReadLine().Trim().Split(' ');
int queenRowPosition = Convert.ToInt32(queenParameters[0]);
int queenColPosition = Convert.ToInt32(queenParameters[1]);
HashSet<(int, int)> obstacleSet = new HashSet<(int, int)>();
for (int i = 0; i < boardObstacles; i++) {
int[] obstacle = Array.ConvertAll(Console.ReadLine().Split(' '), positionTemp => Convert.ToInt32(positionTemp));
obstacleSet.Add((obstacle[0], obstacle[1]));
}
int result = QueensAttack(boardLength, boardObstacles, queenRowPosition, queenColPosition, obstacleSet);
Console.WriteLine(result);
}
}
}