Queen Collision

该问题涉及在n*n的棋盘上放置皇后棋,并计算由于处于同一行、列或对角线而发生的碰撞数量。输入包含棋盘大小n和线性皇后模式的数量g,以及每个模式的详细坐标。输出是所有碰撞的计数。样例输入和输出分别对应了不同的棋盘配置。

Description

   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 × 7 board, contains 7 queens with no collisions. In Figure 2 there is a 5 × 5 board with 5 queens and 4 collisions. In Figure 3, a traditional 8 × 8 board, there are 7 queens and 5 collisions.

                                  

   On an n × 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×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 + i ∗ s, y + i ∗ t), 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. Take some care with your algorithm, or else your solution may take too long.

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

解析

  大致题意是在一个n*n的棋盘中放入g个皇后棋,每个皇后棋的坐标是 (x + i ∗ s, y + i ∗ t) for i = 0, 1, . . . , k − 1。若有任意两个皇后棋在同一行或同一列或同一条对角线,那么视这两个皇后为碰撞,输出的最终结果是碰撞的皇后棋的个数。

代码

#include<stdio.h>
#include<string.h>
#define MAX 40000
int main()
{
    int n,T;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        scanf("%d",&T);
        int row[MAX],col[MAX],dia[2*MAX],obdia[2*MAX];
        //row数组记录在同行出现的皇后棋数,col记录在同列出现的皇后棋数,dia数组记录在同条对角线
        //出现的皇后棋数,obdia记录在同条斜对角线出现的皇后棋数
        memset(row,0,sizeof(row));
        memset(col,0,sizeof(col));
        memset(dia,0,sizeof(dia));
        memset(obdia,0,sizeof(obdia));
        int k,x,y,s,t,i;
        while(T--)
        {
            scanf("%d%d%d%d%d",&k,&x,&y,&s,&t);
            int tempx,tempy;
            for(i=0;i<k;i++)
            {
                tempx=x+i*s; //皇后棋的横坐标
                tempy=y+i*t; //皇后棋的纵坐标
                row[tempx]+=1;
                col[tempy]+=1;
                dia[tempx+tempy]+=1; //对角线
                obdia[tempx-tempy+n]+=1; //斜对角线
            }
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            if(row[i]>1)
                ans+=row[i]-1;
            if(col[i]>1)
                ans+=col[i]-1;
        }
        for(i=1;i<2*n;i++)
        {
            if(dia[i]>1)
                ans+=dia[i]-1;
            if(obdia[i]>1)
                ans+=obdia[i]-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

基于径向基函数神经网络RBFNN的自适应滑模控制学习(Matlab代码实现)内容概要:本文介绍了基于径向基函数神经网络(RBFNN)的自适应滑模控制方法,并提供了相应的Matlab代码实现。该方法结合了RBF神经网络的非线性逼近能力和滑模控制的强鲁棒性,用于解决复杂系统的控制问,尤其适用于存在不确定性和外部干扰的动态系统。文中详细阐述了控制算法的设计思路、RBFNN的结构与权重更新机制、滑模面的构建以及自适应律的推导过程,并通过Matlab仿真验证了所提方法的有效性和稳定性。此外,文档还列举了大量相关的科研方向和技术应用,涵盖智能优化算法、机器学习、电力系统、路径规划等多个领域,展示了该技术的广泛应用前景。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的研究生、科研人员及工程技术人员,特别是从事智能控制、非线性系统控制及相关领域的研究人员; 使用场景及目标:①学习和掌握RBF神经网络与滑模控制相结合的自适应控制策略设计方法;②应用于电机控制、机器人轨迹跟踪、电力电子系统等存在模型不确定性或外界扰动的实际控制系统中,提升控制精度与鲁棒性; 阅读建议:建议读者结合提供的Matlab代码进行仿真实践,深入理解算法实现细节,同时可参考文中提及的相关技术方向拓展研究思路,注重理论分析与仿真验证相结合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值