square puzzle solution

前端 未结 5 953
一生所求
一生所求 2021-01-01 02:51

Question: given an integer number n, print the numbers from 1 up to n2 like this:

n = 4

result is:

01 02 03 04
12 13 14 05
11 16 1         


        
5条回答
  •  一生所求
    2021-01-01 03:41

    I have solved your problem using C++. I don't know if it will be helpful for you. But posting it. If it works for you it will be a pleasure.

    Here is the Code:

        #include
        #include
        using namespace std;
    
        bool valid(int n,int r,int c)
        {
            if(r>=1 && r<=n && c>=1 && c<=n)
                return true;
            return false;
        }
    
    
        int main()
        {
            paird1,d2,d3,d4,temp;
            d1 = make_pair(0,1);
            d2 = make_pair(1,0);
            d3 = make_pair(0,-1);
            d4 = make_pair(-1,0);
            /**********************direction******************************/
    
            int n, i, j, counter=1, newR = 1, newC = 0, direction = 4;
            bool changeDir=true;
            /**************************variables*************************/
    
            cin>>n;
            int arr[n+1][n+1];
            int visited[n+1][n+1];
            /*************************arrays********************************/
    
            memset(visited,0,sizeof(visited));
            memset(arr,0,sizeof(arr));
            /***************initializing the array**************************/
    
            while(counter<=n*n)
            {
                if(direction==1 && changeDir)
                {
                    temp = make_pair(d2.first,d2.second);
                    direction=2;
                    changeDir=false;
                }
                else if(direction==2&& changeDir)
                {
                    temp = make_pair(d3.first,d3.second);
                    direction=3;
                    changeDir=false;
                }
                else if(direction==3&& changeDir)
                {
                    temp = make_pair(d4.first,d4.second);
                    direction=4;
                    changeDir=false;
                }
                else if(direction==4&& changeDir)
                {
                    temp = make_pair(d1.first,d1.second);
                    direction=1;
                    changeDir=false;
                }
                while(counter<=(n*n) && !changeDir)
                {
                    newR =newR+temp.first;
                    newC=newC+temp.second;
                    if(valid(n,newR,newC) && !visited[newR][newC])
                    {
                        arr[newR][newC]=counter;
                        visited[newR][newC]=1;
                        counter++;
                    }
                    else
                    {
                        newR-=temp.first;
                        newC-=temp.second;
                        changeDir=true;
                        break;
                    }
                }
            }
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(arr[i][j]<10)
                        cout<<0;
                    cout<

    Here is the output where N=5:

    01 02 03 04 05 
    16 17 18 19 06 
    15 24 25 20 07 
    14 23 22 21 08 
    13 12 11 10 09
    

    Thank you.

提交回复
热议问题