Sicily 1694 spiral snake Matrix

Source: Internet
Author: User

The question shows a snake matrix, which requires that the given value be calculated at the position of the snake matrix.

Note that the value in the upper right corner of the matrix is the square of an odd number, which is 1, 3, 5... . First, obtain the coordinates of the number in the upper right corner of the required number, and then calculate the coordinates of the specified number based on the distance.

 

#include 
  
    # include 
   
     using namespace STD; int n, m; void find () {int place_zero = n/2 + 1; int Len = 1, right_up = 1; int S = 1; // calculate the value while (right_up 
    
      = 0 & CNT 
     
       = Len & CNT <= 2 * (len-1) // left {Y = Y-(LEN-1 ); X = x + CNT-len + 1;} else if (CNT> 2 * (len-1) & CNT <= 3*(len-1 )) // The following {x = x + len-1; y = Y-(LEN-1) + CNT-2 * (len-1 );} elsex = x + len-1-(CNT-3 * (len-1); // right printf ("% d \ n", x, y );} int main () {INT cases; scanf ("% d", & cases); While (cases --) {scanf ("% d", & N, & M); find () ;}return 0 ;}
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.