zoj 2576 Queen Collisions

1、http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2576

 

2、比赛时只想到了暴搜,殊不知有这么简单地方法,抓狂抓狂抓狂

只需要简单的记数字即可,都不需要判断,居然写了近200行。。。。。

Queen Collisions

Time Limit: 1000MS    Memory limit: 65536K

题目描述

                       
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.

输入

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 + 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.

输出

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.

示例输入

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

示例输出

0
4
5

3、ac代码::

#include<stdio.h>
#include<string.h>
#define N 60005
int r[N],c[N],d1[N],d2[N];
int main()
{
    int n,m,k,x,y,s,t;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
            memset(c,0,sizeof(c));
            memset(r,0,sizeof(r));
            memset(d1,0,sizeof(d1));
            memset(d2,0,sizeof(d2));
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d%d%d",&k,&x,&y,&s,&t);
            int a,b;
                for(int j=0; j<k; j++)
                {
                    a=x+j*s;
                    b=y+j*t;
                    c[b]++;
                    r[a]++;
                    d1[a+b]++;
                    d2[a-b+n]++;
                }
        }
        int ans=0,sum=n*2;
        for(int i=0; i<=sum; i++)
        {
            if(c[i]>1)
                ans+=c[i]-1;
            if(r[i]>1)
                ans+=r[i]-1;
            if(d1[i]>1)
                ans+=d1[i]-1;
            if(d2[i]>1)
                ans+=d2[i]-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
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
*/

wrong 的代码:

#include<stdio.h>
#define N 30005
struct node
{
    int x;
    int y;
} a[N];
int visit[N][N];
int main()
{
    int n,m,k,xx,yy,s,t,j;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        scanf("%d",&m);
        j=1;
        while(m--)
        {
            scanf("%d%d%d%d%d",&k,&xx,&yy,&s,&t);

            for(int i=0; i<k; i++)
            {
                a[j].x=xx+i*s;
                a[j].y=yy+i*t;
                visit[a[j].x][a[j].y]=1;
                j++;
            }
        }

        int flag=0,count=0;
        for(int i=1; i<j; i++)
        {
            for(int p=i+1; p<j; p++)
            {
                flag=0;
                if(a[i].x==a[p].x)
                {
                    for(int o=a[i].y+1; o<a[p].y; o++)
                    {
                        if(visit[a[i].x][o]==1)
                            flag=1;
                    }
                    if(flag==0)
                        count++;
                }

                else if(a[i].y==a[p].y)
                {
                    for(int o=a[i].x+1; o<a[p].x; o++)
                    {
                        if(visit[o][a[i].y]==1)
                            flag=1;
                    }
                    if(flag==0)
                        count++;
                }
                //flag=0;
                else if(((a[p].y-a[i].y)+(a[p].x-a[i].x)==0)||((a[p].y-a[i].y)==(a[p].x-a[i].x)))
                {
                    if((a[p].y-a[i].y)>0&&(a[p].x-a[i].x)<0)
                    {
                        int pp=a[i].y+1;
                        for(int o=a[i].x-1; o>a[p].x; o--)
                        {
                            if(visit[o][pp]==1)
                                flag=1;
                            pp++;
                        }
                        if(flag==0)
                            count++;
                    }
                    //flag=0;
                    else if((a[p].y-a[i].y)<0&&(a[p].x-a[i].x)<0)
                    {
                        int pp=a[i].y-1;
                        for(int o=a[i].x-1; o>a[p].x; o--)
                        {
                            if(visit[o][pp]==1)
                                flag=1;
                            pp--;
                        }
                        if(flag==0)
                            count++;
                    }
                    //flag=0;
                    else if((a[p].y-a[i].y)<0&&(a[p].x-a[i].x)>0)
                    {
                        int pp=a[i].y-1;
                        for(int o=a[i].x+1; o<a[p].x; o++)
                        {
                            if(visit[o][pp]==1)
                                flag=1;
                            pp--;
                        }
                        if(flag==0)
                            count++;
                    }
                    //flag=0;
                    else if((a[p].y-a[i].y)>0&&(a[p].x-a[i].x)>0)
                    {
                        int pp=a[i].y+1;
                        for(int o=a[i].x+1; o<a[p].x; o++)
                        {
                            if(visit[o][pp]==1)
                                flag=1;
                            pp++;
                        }
                        if(flag==0)
                            count++;
                    }
                }
            }
        }
        printf("%d\n",count);
    }
    return 0;
}
/*
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
*/



/**************************************
	Problem id	: SDUT OJ A 
	User name	: pingqing 
	Result		: Compile Error 
	Take Memory	: 0K 
	Take Time	: 0MS 
	Submit Time	: 2013-03-30 17:21:20  
**************************************/


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值