========================================================================
#include<stdio.h>
void main()
{
int i,j;
int r,c;
scanf("%d %d",&r,&c);
char a[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf(" %c",&a[i][j]);
int check=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
int x,y;
x=i;
y=j;
int up,right;
up=0;
right=0;
//ANALYSING
while(x>0)
{
//printf("checking up\n");
x--;
if(a[x][y]==a[i][j])
up++;
else
break;
}
x=i;
while(y<c)
{
//printf("checking right\n");
y++;
if(a[x][y]==a[i][j])
right++;
else
break;
}
//printf("%d %d",up,right);
if(up!=0 && up==right)
{
printf("Yes");
check=1;break;
}
}
if(check==1)
{break;}
}
if(check==0)
printf("No");
}
if(up!=0 && up==right)
{
printf("Yes");
check=1;break;
}
}
if(check==1)
{break;}
}
if(check==0)
printf("No");
}
Nice Work ...
ReplyDelete:)
DeleteThis comment has been removed by the author.
Deletethe initial values of x and y are 0 then how is the programme running further? :/
ReplyDeleteNo , i initialize the values of x and y to be i and j respectively ,thus the program runs.(lines 17 and 18).
DeleteThis comment has been removed by the author.
ReplyDeleteThe nested for loop cannot be avoided in order to access each element.Yes , i know i could have made a much faster program by replacing the while loops with a single if statement,but i wanted to show the concept of traversing the columns and rows of each element as it is used in many pattern recognition problems.(Ex:8 Queen problem)
DeleteHOW Can you give variable indexes in the array declaration
ReplyDeleteplz check line no. 7 of this code,where you have declared
a 2-d character array as
char a[r][c];
Yes it is valid as long as r and c have integer values. It compiled when I checked.
DeleteThis comment has been removed by the author.
DeleteI think there are compiler-specific extensions that allow such a thing. You might as well use this feature rather than allocate dynamically which may complicate things.
DeleteU should use fflush(stdin) before every character or string input using scanf() because while pressing enter the compiler interprets the "enter" as a character literal and leads to unexpected results. Its required to flush the input buffer and it should be placed in j loop when entering the colour of coin from user.
ReplyDeleteThe space before the %c is used for the same purpose.Its not as efficient but does the work!
DeleteThis comment has been removed by the author.
ReplyDeleteinclude { after the first for loop bro in the 9th line and a closing } in the 10th line. :)
ReplyDeleteIf there is only a single statement((in this case the scanf) to be executed inside the for loop,we can avoid using the parenthesis.
Delete