rotate the outer ring of matrix in anticlockwise by k element and inner ring in clockwise by k element in java

£可爱£侵袭症+ 提交于 2020-08-26 07:27:43

问题


please help me to solve the below problem in java to rotate the outer ring of matrix in anticlockwise by k element and inner ring in clockwise by k element in java and the middle element remains constant. The sample input is
m=5,n=6,k=1 where m is no of rows,n is no of column and k is the number of required shift and the input matrix is

1  2  3  4  5  6
7  8  9  10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30

and the expected output is

2  3  4  5  6  12
1  14 8  9  10 18
7  20 15 16 11 24
13 21 22 23 17 30
19 25 26 27 28 29

But I am getting error while I tried to solve this problem by the following steps 1.converting the martix into spiralarray. 2.perform reversal algorithm for array rotation. 3.Again convert the array into matrix form and the sample code is

    import java.util.*;
    public class MyClass {
    public static void main(String args[]) {
        int m,n,k;
        Scanner sc=new Scanner(System.in);
        m=sc.nextInt();
        n=sc.nextInt();
        k=sc.nextInt(); 
        int mat[][]=new int[m][n];
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                mat[i][j]=sc.nextInt();
            }     
        }
        spiralToArray(mat,m,n,k);
    }
    static void fillSpiralPrint(int temp[],int m,int n)
    {
        int r=0;
        int c=0;
        int co=0;
        int rl=m;
        int cl=n;
        int mat[][]=new int[m][n];
        while(r<rl && c<cl)
        {
            for(int i=c;i<cl;i++)
            {
                mat[r][i]=temp[co++];  
            }
            r++;
            for(int i=r;i<rl;i++)
            {
                mat[i][cl-1]=temp[co++];
            }
            cl--;
            if(r<rl)
            {
                for(int i=cl-1;i>=c;i--)
                {
                    mat[rl-1][i]=temp[co++];
                }
                rl--;
            }
            if(c<cl)
            {
                for(int i=rl-1;i>=r;i--)
                {
                    mat[i][c]=temp[co++];
                }
                c++;
            }
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(mat[i][j]+" ");
            }
            System.out.println();
        }   
    }
    static void reverseA(int temp[],int start,int end)
    {
        int t;
        while(start<end)
        {
            t=temp[start];
            temp[start]=temp[end];
            temp[end]=t;
            start++;
            end--;
        }
    }
    /* 
    static void rotateC(int arr[],int k)
    {
        int n=arr.length;
        reverseA(arr,0,n-k-1);
        reverseA(arr,n-k,n-1);
        reverseA(arr,0,n-1);
    }
    */
    static void rotateAC(int temp[],int k)
    {
        int n=temp.length;
        reverseA(temp,0,k-1);
        reverseA(temp,k,n-1);
        reverseA(temp,0,n-1);
    }

static void spiralToArray(int mat[][],int m,int n,int k)
{
    int r=0;
    int c=0;
    int co=0;
    int rl=m;
    int cl=n;
    int start=0;
    int temp[]=new int[m*n];
    while(r<rl && c<cl)
    {
        int end=start;
        for(int i=c;i<cl;i++)
        {
            temp[co++]=mat[r][i]; 
            end++;
        }
        r++;
        for(int i=r;i<rl;i++)
        {
            temp[co++]=mat[i][cl-1];
            end++;
        }
        cl--;
        if(r<rl)
        {
            for(int i=cl-1;i>=c;i--)
            {
                temp[co++]=mat[rl-1][i];
                end++;
            }
            rl--;
        }
        if(c<cl)
        {
            for(int i=rl-1;i>=r;i--)
            {
                temp[co++]=mat[i][c];
                end++;
            }
            c++;
        }
        for(int i=0;i<co;i++)
        {
            System.out.print(temp[i]+" ");
        }
        if(end-start>k)
        {
            rotateAC(temp,k);
            start=end;
        }
        else
            break;
    }
    fillSpiralPrint(temp,m,n);
}

来源:https://stackoverflow.com/questions/61913516/rotate-the-outer-ring-of-matrix-in-anticlockwise-by-k-element-and-inner-ring-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!