Jake and Sara are playing around with a chessboard one night after working with their avatars all day. They decide it would be interesting to place some rooks on the chessboard in a way that no rook can threaten another rook. Since rooks move along rows and columns, this means two rooks may not be on the same row or column. Your goal is to write a program to determine whether any rooks are threatened.
Chessboards are 8x8 boards with positions between (1,1) and (8,8).

Input Format:
Input consists of 2n+1 integers. The first integer corresponds to the number of rooks. The second and third integer corresponds to the column and row of the first rook, the fourth and fifth integer corresponds to the column and row of the second rook and so on.
Output Format:
Your program should output the words ”SAFE” or ”NOT SAFE” on a single line.
Sample Input:
3 1 1 2 6 8 8
Sample Output:
SAFE
Sample Input:
2 2 3 1 3
Sample Output:
NOT SAFE

Program Code:
#include<stdio.h>
int main()
{
    int a[9][9],i,j,n,c,r;
    for(i=1;i<=8;i++)
    {
        for(j=1;j<=8;j++)
        {
            a[i][j]=0;
        }
    } 
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
     scanf("%d%d",&c,&r);
     a[r][c]=1;
    }
    int flag=0,count,count1;
    for(i=1;i<=8;i++)
    {
        count=0,count1=0;
        for(j=1;j<=8;j++)
        {
           if(a[i][j]==1)
           {
               count++;
           }
             if(a[j][i]==1)
           {
               count1++;
           }
        }
        if(count>=2 || count1>=2)
        {
            flag=1;
        }
    } 
   if(flag==1)
    printf("NOT SAFE");
    else
    printf("SAFE");   
    return 0;
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: