Wuhan University of Science and technology acm:1008:0 start-up algorithm 64--matrix

Source: Internet
Author: User

Problem Description

return matrix of Output n*m

Input

Multiple sets of test data
Enter 2 integers n and m per group (not greater than 20)

Output

Output n*m matrix, the upper left corner of the element is required to be 1, (each element occupies 2 positions, on the right)

Sample Input
4 3
Sample Output
1  2  310  4 9 5 8 7  6

1#include <iostream>2#include <string.h>3#include <iomanip>4 using namespacestd;5 intMain ()6 {7     intm,n,i,j,k,lastx,lasty;8      while(cin>>m>>N)9     {        Ten         int**a=New int*[m]; One          for(i=0; i<m;i++) A         { -* (a+i) =New int[n]; -memset (A[i],0Nsizeof(int)); the         } -I=0, j=0, k=0; -         intState=0;//0 for left-to-right assignment, 1 for top-to-bottom, 2 for right-to-left, and 3 for bottom-up -          while(k<m*N) +{//to assign a value of m*n times -             Switch(state) +             { A              Case 0: at                  for(; j<n;++j) -                 { -                     if(a[i][j]==0) -                     { -a[i][j]=k+1; -++K; inlastx=i; -Lasty=j;//record the next assignment start position to                     } +                 } -i=lastx+1; thej=lasty; *State=1; $                  Break;Panax Notoginseng              Case 1: -                  for(; i<m;++i) the                 { +                     if(a[i][j]==0) A                     { thea[i][j]=k+1; +++K; -lastx=i; $lasty=J; $                     } -                 } -I=Lastx; thej=lasty-1; -State=2;Wuyi                  Break; the              Case 2: -                  for(; j>=0;--j) Wu                 { -                     if(a[i][j]==0) About                     { $a[i][j]=k+1; -++K; -lastx=i; -lasty=J; A                     } +                 } thei=lastx-1; -j=lasty; $State=3; the                  Break; the              Case 3: the                  for(; i>=0;--i) the                 { -                     if(a[i][j]==0) in                     { thea[i][j]=k+1; the++K; Aboutlastx=i; thelasty=J; the                     } the                 } +I=Lastx; -j=lasty+1; theState=0;Bayi                  Break; the             default: the                  Break;  -             } -              the         } the          the          for(i=0; i<m;i++) the         { -              for(j=0; j<n;j++) the                 if(j==0) the                 { theCOUT&LT;&LT;SETW (2) <<A[i][j];94                 } the                 Else the                 { theCOUT&LT;&LT;SETW (3) <<A[i][j];98                 }                 Aboutcout<<Endl; - delete []a[i];101         }102 delete []a;103         104     } the     return 0;106}

Wuhan University of Science and technology acm:1008:0 start-up algorithm 64--matrix

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.