Technology Sharing

[Solution] NC50043 Kotori and Queen N (Hash Table)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

https://ac.nowcoder.com/acm/problem/50043
insert image description here
insert image description here

// 定义数据类型和变量
typedef long long LL; // 定义长整型别名
int ret = 0; // 用于记录第一个冲突的皇后编号

// 定义四个集合,分别存储皇后的x坐标、y坐标、x-y和x+y的值
unordered_set<LL> set_x;
unordered_set<LL> set_y;
unordered_set<LL> set_x_sub_y;
unordered_set<LL> set_x_add_y;

// 读取皇后的数量k
cin >> k;

// 外层循环遍历每个皇后
for (int i = 1; i <= k; ++i) {
    LL x, y; // 存储当前皇后的坐标
    cin >> x >> y; // 读取当前皇后的坐标

    // 如果已经有冲突,则跳过当前皇后的检查
    if (ret) continue;

    // 检查当前皇后是否与之前放置的皇后存在冲突
    // 通过检查x, y, x-y, x+y是否已经在对应的集合中
    if (set_x.count(x) || set_y.count(y) || set_x_sub_y.count(x-y) || set_x_add_y.count(x+y)) {
        ret = i; // 记录第一个冲突的皇后编号
    } else {
        // 如果没有冲突,则将当前皇后的坐标和计算值加入集合
        set_x.insert(x);
        set_y.insert(y);
        set_x_sub_y.insert(x - y);
        set_x_add_y.insert(x + y);
    }
}

// 读取查询次数t
cin >> t;

// 处理查询
while (t--) {
    int i; // 当前查询的皇后编号
    cin >> i; // 读取查询的皇后编号

    // 如果查询的皇后编号大于等于第一个冲突的皇后编号,输出"Yes"
    // 否则输出"No"
    if (i >= ret && ret != 0) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}
  • 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