[BASIC-25] return number

Source: Internet
Author: User

Basic exercise retrieval time limit: 1.0 s memory limit: 512.0MB Problem description: The number of retrieval is the number of edges in the matrix. If there are countless edges in the current direction, turn left 90 degrees. At the beginning, it is in the upper left corner of the matrix and the direction is down. In the input format, the first line is two positive integers (m, n) not greater than 200, indicating the rows and columns of the matrix. N integers in each row in the next m line indicate this matrix. The output format outputs only one row, with a total of mn numbers. The result is the number of input matrix return operations. Separate the numbers with one space. Do not add any extra space at the end of the line. Example input 3 3
1 2 3
4 5 6
7 8 9 sample output 1 4 7 8 9 6 3 2 5 sample input 3 2
1 2
3 4
5 6 sample output 1 3 5 6 4 2
Import java. util. extends; public class Main {public static void main (String [] args) {extends = new loads (System. in); while (partial. hasNext () {int m = seconds. nextInt (); int n = bytes. nextInt (); int [] [] flag = new int [m] [n]; // mark the path int [] [] nums = new int [m] [n]; // record data for (int I = 0; I <m; I ++) {for (int j = 0; j <n; j ++) {nums [I] [j] = numeric. nextInt () ;}} int count = m * n; int I = 0, j = 0; While (count> 0) {while (I <m & flag [I] [j] = 0) {System. out. print (nums [I] [j]); System. out. print (-- count = 0? "\ R \ n": ""); flag [I] [j] = 1; I ++;} I --; j ++; while (j <n & flag [I] [j] = 0) {System. out. print (nums [I] [j] + ""); System. out. print (-- count = 0? "\ R \ n": ""); flag [I] [j] = 1; j ++;} j --; I --; while (I>-1 & flag [I] [j] = 0) {System. out. print (nums [I] [j] + ""); System. out. print (-- count = 0? "\ R \ n": ""); flag [I] [j] = 1; I --;} I ++; j --; while (j>-1 & flag [I] [j] = 0) {System. out. print (nums [I] [j] + ""); System. out. print (-- count = 0? "\ R \ n": ""); flag [I] [j] = 1; j --;} j ++; I ++ ;}}}}

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.