Showing posts with label count steps in matrix solution. Show all posts
Showing posts with label count steps in matrix solution. Show all posts

Saturday, 25 November 2017

count steps in matrix solution in C

#include<stdio.h>
 #include <conio.h>
#include <math.h>
int a[10][10],y,z,r,c;
int fun(int n)
{
                int k,i,j,f=0;
                for(i=0;i<r;i++)
                {
                                for(j=0;j<c;j++)
                                {
                                                if(n == a[i][j])
                                                {       f=1;
                                                                break;
                                                }
                                }
                                if(f ==1)
                                                break;
                }
      //      printf("\n%d\t%d",i,j);
                k=abs(y-i)+abs(z-j);
                y=i;
                z=j;
                return k;
}
int main()
{
           int count,i,j,c1,t,test;
           scanf("%d",&test);
           while(test--)
            {           
                count=0;
                scanf("%d%d",&r,&c);
                for(i=0;i<r;i++)
                                for(j=0;j<c;j++)
                                                scanf("%d",&a[i][j]);
                y=0;z=0;
                 for(i=a[0][0]+1,c1=0;c1<r*c-1;c1++,i++)
                {
                                t=fun(i);
                                count =count+t;
                }
                printf("%d",count);
          }
           return 0;

}