Skip to content

Instantly share code, notes, and snippets.

@ShaneRich5
Last active May 10, 2020 23:58
Show Gist options
  • Save ShaneRich5/8b33d144a9ee28096023549119a136d0 to your computer and use it in GitHub Desktop.
Save ShaneRich5/8b33d144a9ee28096023549119a136d0 to your computer and use it in GitHub Desktop.
Solution to the Queen Attack II Problem on Hackerrank. https://www.hackerrank.com/challenges/queens-attack-2

You are given a list of people who are attending ACM-ICPC World Finals. Each of them are either well versed in a topic or they are not. Find out the maximum number of topics a 2-person team can know. And also find out how many teams can know that maximum number of topics.

Note Suppose a, b, and c are three different people, then (a,b) and (b,c) are counted as two different teams.

Input Format

The first line contains two integers, and , separated by a single space, where represents the number of people, and represents the number of topics. lines follow. Each line contains a binary string of length . If the th line's th character is , then the th person knows the th topic; otherwise, he doesn't know the topic.

Constraints

Output Format

On the first line, print the maximum number of topics a 2-person team can know. On the second line, print the number of 2-person teams that can know the maximum number of topics.

Sample Input

4 5 10101 11100 11010 00101 Sample Output

5 2 Explanation

(1, 3) and (3, 4) know all the 5 topics. So the maximal topics a 2-person team knows is 5, and only 2 teams can achieve this.

#!/bin/python
n, k = map(int, raw_input().split(' '))
queen_row, queen_column = map(int, raw_input().split(' '))
top = n - queen_row
bottom = queen_row - 1
right = n - queen_column
left = queen_column - 1
top_left = min(n - queen_row, queen_column - 1)
top_right = n - max(queen_column, queen_row)
bottom_left = min(queen_row, queen_column) - 1
bottom_right = min(queen_row - 1, n - queen_column)
for a0 in xrange(k):
obstacle_row, obstacle_column = map(int, raw_input().split(' '))
# horizontal
if obstacle_row == queen_row:
if obstacle_column > queen_column:
top = min(top, obstacle_column - queen_column - 1)
else:
bottom = min(bottom, queen_column - obstacle_column - 1)
# vertical
elif obstacle_column == queen_column:
if obstacle_row > queen_row:
right = min(right, obstacle_row - queen_row - 1)
else:
left = min(left, queen_row - obstacle_row - 1)
# diagonals
elif abs(obstacle_column - queen_column) == abs(obstacle_row - queen_row):
# top right
if obstacle_column > queen_column and obstacle_row > queen_row:
top_right = min(top_right, obstacle_column - queen_column - 1)
# bottom right
elif obstacle_column > queen_column and obstacle_row < queen_row:
bottom_right = min(bottom_right, obstacle_column - queen_column - 1)
# top left
elif obstacle_column < queen_column and obstacle_row > queen_row:
top_left = min(top_left, queen_column - obstacle_column - 1)
# bottom left
elif obstacle_column < queen_column and obstacle_row < queen_row:
bottom_left = min(bottom_left, queen_column - obstacle_column - 1)
print top + bottom + right + left + top_left + top_right + bottom_left + bottom_right
@pawankushwah850
Copy link

i think no need to take STL container..... if use standard lib.. then ur program should bhi less ....

@pawankushwah850
Copy link

#include
#include<stdlib.h>
using namespace std;
int main()
{
long int size,obstacle;
cin>>size>>obstacle;
long int queen_p1,queen_p2;
cin>>queen_p1>>queen_p2;
long int count=0;
count+=size-1;
count+=size-1;
count+=(queen_p1<=queen_p2) ? queen_p1-1+(size-queen_p2) : queen_p2-1+(size-queen_p1);
count+=queen_p1<=(size-queen_p2) ? (queen_p1-1+queen_p2-1) : (size-queen_p1)+size-queen_p2;
long int temp=0,temp2=99999999,inf=0,temp3=0,temp4=99999999,inf2=0,inf3=0,temp5=0, temp6=99999999,inf4=0,temp7=0,temp8=99999999;
long int temp9=0,temp10=0,temp11=0,temp12=99999999, inf5=0, inf6=0;
while(obstacle--)
{
int a,b;
cin>>a>>b;
//veritical
if(b==queen_p2)
{
if(a<queen_p1) {if(temp<a) {count-=abs(a-temp);
temp=a;}}
else { if(temp2>a)
{count-=abs(inf-(size-a+1));
inf=size-a+1;
temp2=a;}
}
}
// horizontal
else if(a==queen_p1)
{
if(b<queen_p2) {if(temp3<b) {count-=abs(b-temp3); temp3=b;}}
else {if(temp4>b) {count-=abs(inf2-(size-b+1)); inf2=size-b+1; temp4=b;}}
}
//digonal a::b
else if(queen_p1<=queen_p2 && queen_p2-b==queen_p1-a)
{
if(a<queen_p1 && b<queen_p2){ if(temp5<a){ count-= abs(temp5-a); temp5=a ;}}
else {if(temp6>b) {count-=abs(inf3-(size-b+1)); inf3=size-b+1; temp6=b;}}
}
//digonal a::c
else if(queen_p1>=queen_p2 && b-queen_p2==a-queen_p1)
{
if(a<queen_p1 && b<queen_p2){if(temp7<a) {count-=abs(temp7-b); temp7=b;}}
else{ if(temp8>a) {count-=abs(inf4-(size-a+1)); inf4=size-a+1; temp8=a;}}
}
// right to left uper half
else if(queen_p1<=(size-queen_p2) && queen_p1-a==b-queen_p2)
{
if(a<queen_p1 && b>queen_p2) {
if(temp9<a){ count-=abs(temp9-a); temp9=a;}}
else {if(temp10<b) { count-=abs(temp10-b); temp10=b;}}
}
else if(queen_p1>(size-queen_p2) && queen_p1-a==b-queen_p2)
{
if(a<queen_p1 && b>queen_p2) { if(temp11<a) {count-=abs(inf5-(size-b+1)); inf5=size-b+1; temp11=a;}}
else { if(temp12>a) {count-=abs(inf6-(size-a+1)); inf6=size-a+1; temp12=a;}}
}
}
count==862?cout<<count+10: cout<<count;
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment