HOJ. 12061 Queen Collisions

Problem Description

HOJ. 12061 Queen Collisions_第1张图片
Lots of time has been spent by computer science students dealing with queens on a chess board. Two queens on a chessboard collide if they lie on the same row, column or diagonal, and there is no piece between them. Various sized square boards and numbers of queens are considered. For example,
Figure 1, with a 7 x 7 board, contains 7 queens with no collisions. In Figure 2 there is a 5 x 5 board with 5 queens and 4 collisions. In Figure 3, a traditional 8 x 8 board, there are 7 queens and 5 collisions.
On an n x n board, queen positions are given in Cartesian coordinates (x, y) where x is a column number, 1 to n, and y is a row number, 1 to n. Queens at distinct positions (x1, y1) and (x2, y2) lie on the same diagonal if (x1- x2) and (y1- y2) have the same magnitude. They lie on the same row or column if x1= x2 or y1= y2, respectively. In each of these cases the queens have a collision if there is no other queen directly between them on the same diagonal, row, or column, respectively. For example, in Figure 2, the collisions are between the queens at (5, 1) and (4, 2), (4, 2) and (3, 3), (3, 3) and (2, 4), and finally (2, 4) and (1, 5). In Figure 3, the collisions are between the queens at (1, 8) and (4, 8), (4, 8) and (4, 7), (4, 7) and (6, 5), (7, 6) and (6, 5), and finally (6, 5) and (2, 1). Your task is to count queen collisions.
In many situations there are a number of queens in a regular pattern. For instance in Figure 1 there are 4 queens in a line at (1,1), (2, 3), (3, 5), and (4, 7). Each of these queens after the first at (1, 1) is one to the right and 2 up from the previous one. Three queens starting at (5, 2) follow a similar pattern. Noting these patterns can allow the positions of a large number of queens to be stated succinctly.

Input

The input will consist of one to twenty data sets, followed by a line containing only 0.
The first line of a dataset contains blank separated positive integers n g, where n indicates an n x n board size, and g is the number of linear patterns of queens to be described, where n < 30000, and g < 250.
The next g lines each contain five blank separated integers, k x y s t, representing a linear pattern of k queens at locations (x + is, y +it), for i = 0, 1, …, k-1. The value of k is positive. If k is 1, then the values of s and t are irrelevant, and they will be given as 0. All queen positions will be on the board.
The total number of queen positions among all the linear patterns will be no more than n, and all these queen positions will be distinct.

Output

There is one line of output for each data set, containing only the number of collisions between the queens.
The sample input data set corresponds to the configuration in the Figures.

Sample Input

7 2
4 1 1 1 2
3 5 2 1 2
5 1
5 5 1 -1 1
8 3
1 2 1 0 0
3 1 8 3 -1
3 4 8 2 -3
0

Sample Output

0
4
5

本题中,每添加一个皇后时,当其行中存在其他皇后,会导致碰撞数+1,列和斜线一样的原理,存在其他皇后则每添加一个,碰撞数+1,因此在输入中解决问题即可:

/*
 * Copyright (c) 2019 Ng Kimbing(吴剑兵), HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author:201707020122__Cantonese name:NgKimbing (Mandarin name: Wu Jianbing), CS 1705, College of Computer Scienceand Electronic Engineering Hunan University.
 * @LastModified:2019-05-18 T 14:43:21.521 +08:00
 */
package ACMProblems;

import java.util.Arrays;

import static ACMProblems.ACMIO.*;

public class QueenProblem {
    private static final int MAX_N = 30000 + 5;
    private static int[] row = new int[MAX_N];
    private static int[] col = new int[MAX_N];
    private static int[] tilt1 = new int[2 * MAX_N];
    private static int[] tilt2 = new int[2 * MAX_N];

    private static boolean inputAnsSolve() throws Exception {
        int cnt = 0;
        int n = nextInt();
        if (n == 0)
            return false;
        int patternNum = nextInt();
        int k, x, y, s, t;
        memSet();
        for (int i = 0; i < patternNum; ++i) {
            k = nextInt();
            x = nextInt();
            y = nextInt();
            s = nextInt();
            t = nextInt();
            for (int j = 0; j < k; ++j) {
                int currX = x + j * s;
                int currY = y + j * t;
                ++row[currX];
                if (row[currX] >= 2)
                    cnt++;
                ++col[currY];
                if (col[currY] >= 2)
                    cnt++;
                ++tilt1[currX + currY];
                if (tilt1[currX + currY] >= 2)
                    cnt++;
                ++tilt2[currX - currY + n];
                if (tilt2[currX - currY + n] >= 2)
                    cnt++;
            }
        }
        out.println(cnt);
        return true;
    }

    private static void memSet() {
        Arrays.fill(row, 0);
        Arrays.fill(col, 0);
        Arrays.fill(tilt1, 0);
        Arrays.fill(tilt2, 0);
    }

    public static void main(String[] args) throws Exception {
        setStream(System.in);
        while (inputAnsSolve()) ;
        out.flush();
    }
}

你可能感兴趣的:(ACM)