Consider a nxn board game with four types of coins red, green, blue and yellow. Given the state of the board with coins in all cells, develop an algorithm and write a C program to check if the same coins are placed in ‘L’ shape on the board. The number of cells in the vertical and horizontal line of ‘L’ shape should be same and should be greater than 1. Red coins are represented by ‘r’, blue coins are represented by ‘b’, green coins are represented by ‘g’ and yellow coins are represented by ‘y’.
For example, given the configuration of a 4 X 4 board with coins as shown below,
b r y r
r r y b
y r b b
b r r r
the program must print 'Yes'. As there is a 'L' shape as shown in the figure below.
Input Format
Number of rows, r
Number of columns, c
Next ‘r x c’ lines contains the elements in the matrix
Elements in the matrix given row by row
Output Format
Print Yes or No
Input:
the number of rows and columns and the 'r x c' board
Processing:
#include<stdio.h>int main()
{
int r,n,i,j;
int f=0;
char c[10][10];
scanf("%d%d",&r,&n);
for(i=0;i < r;i++)
for(j=0;j < n;j++)
scanf("%c",&c[i][j]);
if(r > 2 && n > 2)
{
for(i=0;i < r-1;i++)
for(j=0;j < n-2;j++)
{
if((c[i][j]==c[i+1][j])&&(c[i][j]==c[i+1][j+1])&&(c[i][j]==c[i+1][j+2])) {
f=1;
printf("Yes");
break;
}
}
if(f==0)
printf("No");
}
else
printf("No");
return 0;
}
Output:
display yes or no
Program:
#include<stdio.h>
int main()
{
int r,n,i,j;
int f=0;
char c[10][10];
scanf("%d%d",&r,&n);
for(i=0;i < r;i++)
for(j=0;j < n;j++)
scanf("%c",&c[i][j]);
if(r > 2 && n > 2)
{
for(i=0;i < r-1;i++)
for(j=0;j < n-2;j++)
{
if((c[i][j]==c[i+1][j])&&(c[i][j]==c[i+1][j+1])&&(c[i][j]==c[i+1][j+2])) {
f=1;
printf("Yes");
break;
}
}
if(f==0)
printf("No");
}
else
printf("No");
return 0;
}
Pseudocode:
Step1. get the number of rows and number of columns
Step2. get the r x c matrix
Step3. repeat till i goes from 0 to r-1 and j goes from 1 to n-2
Step3.1 check if the 2x2 L shape is formed if yes then assign f as 1 and display yes and break out from the loop else continue
Step4. if f has a value 0 then display no
Step5.End
No comments:
Post a Comment