The square field consists of M ∗ M cells. Each cell is colored in one of three colors (1,2,3). The initial state
is chosen in one of the cells of color 1. In each step one allowed to move one cell up, down, left or right
remaining inside the field.
You are to define the minimal amount of steps one should make to get a cell of color 3 independent on the
initial state. NOTE: this means you could be starting at any of the 1.
Also note that the field contains at least one cell of color 1 and at least one cell of color 3.
1.1
Input Format
The input consists of several input blocks. The first line of each block contains integer M, the size of the
field. Then there are M lines with colors of the cells.
1.2
Output Format
For each input block the output should consist of one line with the integer, the minimal amount of steps one
should make to get a cell of color 3 independent on the initial state.
1.3
Sample Input
4
1223
2123
2213
3212
2
12
33
1.4
Sample Output
3
1
Program:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
typedef struct node
{
int n;
char v;
struct node*next;
}node;
int main(void)
{
char val[10005];
int t,i,m,j,k,in[10005];
node* a[10005],*tmp;
char d,c='a';
m=0;
while((c=getchar())!=EOF)//scan char by char till EOF
{
while(c>='0'&&c<='9')//find m
{
m=m*10+c-'0';
c=getchar();
}
k=0;
for(i=0;i<m*m;i++)//scan the matrix
{
scanf("%c",&val[i]);//store the values in val array
a[i]=NULL;//set ith node to NULL
if(val[i]=='1')//store indices of all 1's in in array
in[k++]=i;
if(((i+1)%m)==0)//scan \n
d=getchar();
}
c=d;
for(i=0;i<m*m;i++)// create linked lists for adjacency list representation of matrix as a graph
{
if(i%m!=0)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i-1;
a[i]->v=val[i-1];
a[i]->next=tmp;
}
if(i+1<m*m&&(i+1)%m!=0)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i+1;
a[i]->v=val[i+1];
a[i]->next=tmp;
}
if(i-m>=0)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i-m;
a[i]->v=val[i-m];
a[i]->next=tmp;
}
if(i+m<m*m)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i+m;
a[i]->v=val[i+m];
a[i]->next=tmp;
}
}
int min=0;
for(i=0;i<k;i++)//find minimum distance b/w each '1' and a '3' and find the maximum of these minimum distances
{
int d[10005],flag;
for(j=0;j<m*m;j++)
d[j]=INT_MAX;
int f=0,r=0,vis[10005]={0},queue[10005];
queue[r++]=in[i];
d[in[i]]=0;
vis[in[i]]=1;
flag=0;
while(flag==0&&f!=r)
{
t=queue[f++];
for(tmp=a[t];tmp!=NULL;tmp=tmp->next)
{
vis[tmp->n]=1;
d[tmp->n]=d[t]+1;
queue[r++]=tmp->n;
if(tmp->v=='3')
{
flag=1;
if(d[tmp->n]>min)
min=d[tmp->n];
}
}
}
}
printf("%d\n",min);
m=0;
}
return 0;
}
is chosen in one of the cells of color 1. In each step one allowed to move one cell up, down, left or right
remaining inside the field.
You are to define the minimal amount of steps one should make to get a cell of color 3 independent on the
initial state. NOTE: this means you could be starting at any of the 1.
Also note that the field contains at least one cell of color 1 and at least one cell of color 3.
1.1
Input Format
The input consists of several input blocks. The first line of each block contains integer M, the size of the
field. Then there are M lines with colors of the cells.
1.2
Output Format
For each input block the output should consist of one line with the integer, the minimal amount of steps one
should make to get a cell of color 3 independent on the initial state.
1.3
Sample Input
4
1223
2123
2213
3212
2
12
33
1.4
Sample Output
3
1
Program:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
typedef struct node
{
int n;
char v;
struct node*next;
}node;
int main(void)
{
char val[10005];
int t,i,m,j,k,in[10005];
node* a[10005],*tmp;
char d,c='a';
m=0;
while((c=getchar())!=EOF)//scan char by char till EOF
{
while(c>='0'&&c<='9')//find m
{
m=m*10+c-'0';
c=getchar();
}
k=0;
for(i=0;i<m*m;i++)//scan the matrix
{
scanf("%c",&val[i]);//store the values in val array
a[i]=NULL;//set ith node to NULL
if(val[i]=='1')//store indices of all 1's in in array
in[k++]=i;
if(((i+1)%m)==0)//scan \n
d=getchar();
}
c=d;
for(i=0;i<m*m;i++)// create linked lists for adjacency list representation of matrix as a graph
{
if(i%m!=0)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i-1;
a[i]->v=val[i-1];
a[i]->next=tmp;
}
if(i+1<m*m&&(i+1)%m!=0)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i+1;
a[i]->v=val[i+1];
a[i]->next=tmp;
}
if(i-m>=0)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i-m;
a[i]->v=val[i-m];
a[i]->next=tmp;
}
if(i+m<m*m)
{
tmp=a[i];
a[i]=malloc(sizeof(node));
a[i]->n=i+m;
a[i]->v=val[i+m];
a[i]->next=tmp;
}
}
int min=0;
for(i=0;i<k;i++)//find minimum distance b/w each '1' and a '3' and find the maximum of these minimum distances
{
int d[10005],flag;
for(j=0;j<m*m;j++)
d[j]=INT_MAX;
int f=0,r=0,vis[10005]={0},queue[10005];
queue[r++]=in[i];
d[in[i]]=0;
vis[in[i]]=1;
flag=0;
while(flag==0&&f!=r)
{
t=queue[f++];
for(tmp=a[t];tmp!=NULL;tmp=tmp->next)
{
vis[tmp->n]=1;
d[tmp->n]=d[t]+1;
queue[r++]=tmp->n;
if(tmp->v=='3')
{
flag=1;
if(d[tmp->n]>min)
min=d[tmp->n];
}
}
}
}
printf("%d\n",min);
m=0;
}
return 0;
}