import java.util.Scanner; public class espiral { public static void main(String[] args) { Scanner leer = new Scanner (System.in); int N, M, f, c; int Mat[][] = new int [50][50]; System.out.println("Introduzca tamaño de Filas"); N = leer.nextInt(); System.out.println("Introduzca tamaño de Columnas"); M = leer.nextInt(); //Llenado de ceros for (f=1; f<=N; f++) { for (c=1; c<=M; c++) { Mat[f][c]=0; } } int cont = 1; int int int int x1=1, x2=M, a1=1; y1=2, y2=N, a2=M; z1=M-1,z2=1, a3=N; w1=N-1, w2=2, a4=1; while(cont <= N*M) { for (c=x1; c<=x2; c++) { if(Mat[a1][c]==0) { Mat[a1][c]=cont; cont++;} } x1++; x2--; a1++; for (f=y1; f<=y2; f++) { if(Mat[f][a2]==0) { Mat[f][a2]=cont; cont++;} } y1++; y2--; a2--; for (c=z1; c>=z2; c--) { if(Mat[a3][c]==0) { Mat[a3][c]=cont; cont++;} } z1--; z2++; a3--; for (f=w1; f>=w2; f--) { if(Mat[f][a4]==0) { Mat[f][a4]=cont; cont++;} } w1--; w2++; a4++; } //Mostrar Matriz System.out.println("La Matriz Espiral es:"); for (f=1; f<=N; f++) { for (c=1; c<=M; c++) { System.out.print("\t"+Mat[f][c]); } System.out.print("\n"); } } }