whiplk

[CODE] - Matriz Caracol

May 10th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. //By Willian Luigi
  2.  
  3. import java.util.*;
  4.  
  5. class Caracol {
  6.     public static void main(String[] args) {
  7.         Scanner inp = new Scanner(System.in);
  8.        
  9.         int n = inp.nextInt();
  10.         int[][] caracol = new int[n][n];
  11.        
  12.         int
  13.             x = 0,
  14.             y = 0,
  15.             px = 0,
  16.             py = 0
  17.         ;
  18.        
  19.         for (int i = 0; i < n * n + 1; ++i) {
  20.             caracol[y][x] = i;
  21.            
  22.             x += px;
  23.             y += py;
  24.            
  25.             if (caracol[y][x] != 0) {
  26.                 if (px == 1) {
  27.                     px = 0;
  28.                     py = 1;
  29.                     x -= 1;
  30.                     y += 1;
  31.                 }
  32.                 else if (px == -1) {
  33.                     px = 0;
  34.                     py = -1;
  35.                     x += 1;
  36.                     y -= 1;
  37.                 }
  38.                 else if (py == 1) {
  39.                     px = -1;
  40.                     py = 0;
  41.                     x -= 1;
  42.                     y -= 1;
  43.                 }
  44.                 else if (py == -1) {
  45.                     px = 1;
  46.                     py = 0;
  47.                     x += 1;
  48.                     y += 1;
  49.                 }
  50.             }
  51.             if (x == 0 && y == 0) {
  52.                 px = 1;
  53.                 py = 0;
  54.             }
  55.             else if (x == 0 && y == n - 1) {
  56.                 px = 0;
  57.                 py = -1;
  58.             }
  59.             if (x == n - 1 && y == n - 1) {
  60.                 px = -1;
  61.                 py = 0;
  62.             }
  63.             else if (x == n - 1) {
  64.                 px = 0;
  65.                 py = 1;
  66.             }
  67.         }
  68.         System.out.printf("\n\n\n\n");
  69.         for (int i = 0; i < n; ++i) {
  70.             for (int j = 0; j < n; ++j) {
  71.                 System.out.printf("%d\t", caracol[i][j]);
  72.             }
  73.             System.out.println("");
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment