#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Function to check if given indices are valid in the matrix
int isValid(int row, int col, int N, int M) {
return (row >= 0 && row < N && col >= 0 && col < M);
}
// Function to count the number of qualities a girl possesses
int countQualities(int matrix[][100], int row, int col, int N, int M) {
int count = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (isValid(i, j, N, M) && !(i == row && j == col) && matrix[i][j] == 1) {
count++;
}
}
}
return count;
}
// Function to find the suitable bride for Sam
void findBride(int matrix[][100], int N, int M) {
int minDistance = N + M; // Initialize to maximum possible distance
int maxQualities = 0;
int brideRow = -1, brideCol = -1;
// Loop through the matrix to find the bride with maximum qualities
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (matrix[i][j] == 1) {
int qualities = countQualities(matrix, i, j, N, M);
if (qualities > maxQualities) {
maxQualities = qualities;
brideRow = i;
brideCol = j;
} else if (qualities == maxQualities) {
// If multiple brides have the same number of qualities,
// find the one with minimum distance to Sam's house
int distance = abs(0 - i) + abs(0 - j);
if (distance < minDistance) {
minDistance = distance;
brideRow = i;
brideCol = j;
} else if (distance == minDistance) {
printf("Polygamy not allowed\n");
return;
}
}
}
}
}
// If no suitable bride found
if (brideRow == -1 || brideCol == -1) {
printf("No suitable girl found\n");
} else {
printf("%d:%d:%d\n", brideRow + 1, brideCol + 1, maxQualities);
}
}
int main() {
int N, M;
scanf("%d %d", &N, &M);
int matrix[100][100];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
scanf("%d", &matrix[i][j]);
}
}
findBride(matrix, N, M);
return 0;
}
#include<stdio.h>
int main()
{
int n,m,i,g[50][50],j,p,q,max=0,cnt=0,k=1,c=0,u=1,x[30],y[30],t1,min=0, sc[50],e,f,ct=0,a[50],count=0,t2=0,t=0;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&g[i][j]);
} }
g[1][1]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{ cnt=0;
if(g[i][j]==1)
{
t++;
for(p=i-1;p<=i+1;p++)
{
for(q=j-1;q<=j+1;q++)
{
if(g[p][q]==1)
{ cnt++;
} } }
cnt=cnt-1;
a[k]=cnt;
k++;
} } }
for(k=1;k<=t;k++)
{ if(a[k]>max) max=a[k];
}
if(max==0)
{ printf("No suitable girl found"); goto x; }
for(k=1;k<=t;k++)
{ if(a[k]==max)
c++; }
for(k=1;k<=t;k++)
{ t2=0;
if(a[k]==max)
{ for(i=1;i<=n;i++)
{ for(j=1;j<=m;j++)
{ if(g[i][j]==1) t2++;
if(t2==k)
{ x[u]=i; y[u]=j; u++;
} } } } }
t1=u-1;
if(c==1)
printf("%d:%d:%d",x[1],y[1],max);
else
{ for(u=1;u<=t1;u++)
{ e=x[u]-1;
f=y[u]-1;
if(e>=f)
{ sc[u]=e;
}
else sc[u]=f;
}
min=sc[1];
for(u=1;u<=t1;u++)
{ if(sc[u]<min) min=sc[u];
}
for(u=1;u<=t1;u++)
{ if(sc[u]==min) count++;
}
if(count>1)
printf("Polygamy not allowed");
if(count==1)
{ for(u=1;u<=t1;u++)
{ if(sc[u]==min)
printf("%d:%d:%d",x[u],y[u],max);
} } }
x: return 0;
}
OUTPUT:
6 6
1 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 0 0 0 0
Related Links:
good work mate
ReplyDeleteNice
ReplyDeleteGood job.
ReplyDeletevery nice bro
ReplyDeleteCan anyone provide some more test cases?
ReplyDeleteyeeaah sure!
Deletetest case1:
4 5
1 0 1 1 1
0 1 0 1 0
1 1 1 0 0
0 0 0 1 0
2:2:3
test case 2:
3 3
1 1 1
1 1 0
1 0 0
polygamy not allowed
test case 3:
4 4
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
no suitable girl found
Is it ok?
Bro please explain me this code. I am not able to catch that properly.
Deleteyeah sure!! can yu say till what you understood!! and in what you hav doubt?
Deletecan you explain from
Deletefor(p=i-1;p<=i+1;p++)
yeah! that loop is for checking the neighbouring elements whether it is 1 or 0 then we are checking for the maximam number!
Delete4 5
Delete1 0 1 1 1
0 1 0 1 0
1 1 1 0 0
0 0 0 1 0
here ans is 2:2:5 ......gone mad??
Can you read the question properly before commenting?
DeleteBecause I guess you are saying 2:2:5 as you are counting (1,1) also as a brides quality. "Similar to the condition above, 1 at location (1, 1) should not be considered as the quality of a Girl." so the correct answer is 2:2:4.
Such a nice and easy code.....
ReplyDeletehey can you break it in to function it will be nice and clean.
ReplyDeleteYou delivered such an impressive piece to read, giving every subject enlightenment for us to gain information. Thanks for sharing such information with us due to which my several concepts have been cleared. https://archerytopic.com/
ReplyDeleteimport numpy as np
ReplyDeletex = [int(i) for i in input().split()]
N= x[0]
M = x[1]
girl = [[int(j) for j in input().split()] for i in range(N)]
summer = np.zeros([N,M])
x = [[0 for i in range(M)] for j in range(N)]
def check(girl,summer) :
c = 0
max1 = 0
girl[0][0] = 0
for i in range(0,N) :
for j in range(0,M) :
c = 0
if girl[i][j] == 1 :
if j+1=0 and girl[i][j-1] == 1 :
c = c+1
if i-1 >= 0 and girl[i-1][j] == 1 :
c = c+1
if i-1 >= 0 and j-1>= 0 and girl[i-1][j-1] == 1 :
c = c+1
if i-1 >= 0 and j+1< M :
if girl[i-1][j+1] == 1 :
c = c+1
if i+1 < N :
if girl[i+1][j] == 1 :
c = c+1
if j-1>=0 :
if girl[i+1][j-1] == 1 :
c = c+1
if j+1 0) :
if c>max1 :
max1 = c
#print( i+1 ,'=', j+1 , '=', c)
summer[i][j] = c
min = 10000000
dis = 0
for i in range (0,N) :
for j in range(0,M ) :
if summer[i][j] == max1 :
dis = abs(i-0) + abs(j-0)
if dis < min :
min = dis
saveI = i
saveJ = j
print(saveI+1,':',saveJ+1,':',max1)
check(girl,summer)
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHey your code fails for the following test cases:
ReplyDeleteTest case 1:
3 3
1 1 1
0 0 0
0 1 0
Test case 2:
5 6
1 0 0 0 0 1
0 1 0 0 0 1
0 1 0 0 1 1
0 0 1 1 0 0
1 0 0 0 0 1
--------------------
Here's my code for verification purposes:
https://ide.geeksforgeeks.org/wsCpbRmGOp
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHere is a simple code
ReplyDelete#include
int main ()
{
int r, c, a[100][100], q[100], cq = 0, sq = 0, cr = 0, cc = 0, max = 0, e, b = 0,p=0;
scanf ("%d %d", &r, &c);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
scanf ("%d", &a[i][j]);
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if ((a[i][j]) == 1)
{
sq = (a[i][j - 1] == 1) + (a[i][j + 1] == 1) +
(a[i - 1][j - 1] == 1) + (a[i - 1][j] == 1) +
(a[i - 1][j + 1] == 1) + (a[i + 1][j - 1] == 1) +
(a[i + 1][j] ==1) + (a[i + 1][j + 1] == 1);
if (sq > max)
{
max = sq;
cr = i;
cc = j;
p=0;
}
else if (sq == max)
{
if (cr + cc == i + j)
{ p=max;
}
}
}
}
}
if (max == 0 )
printf ("No suitable girl found");
else if(p==0)
printf ("%d:%d:%d", cr + 1, cc + 1, max);
else if(p==max)
printf ("polygamy is not allowed");
return 0;
}
Great !!!
DeleteBro! whats your logic behind calculation shortest path from to bride from [1,1]
ReplyDeleteI think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. meopta meostar r2 2.5 15x56 rd prezzo
ReplyDeleteI didn't get the logic behind the shortest path
ReplyDelete