Ver Mensaje Individual
  #2 (permalink)  
Antiguo 25/04/2013, 13:09
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Un problema mas

Este problema esta sencillo cuando usas el algoritmo correcto. En este caso yo use este.
http://en.wikipedia.org/wiki/Breadth-first_search y este http://en.wikipedia.org/wiki/Flood_fill

Código Python:
Ver original
  1. n = int(raw_input())
  2. m = 2 * n  - 1
  3.  
  4. matrix = [[' ' for j in range(m)] for i in range(m)]
  5.  
  6. def bfs(matrix, x, y, n):
  7.     l = [(0, 1), (1, 0), (-1, 0), (0, -1)]
  8.     q = [(x, y, n)]
  9.     matrix[x][y] = str(n)
  10.     while q:
  11.         x, y, n = q.pop(0)
  12.         if n == 1: continue
  13.         for i, j in l:
  14.             try:
  15.                 if matrix[x+i][y+j] == ' ':
  16.                     matrix[x+i][y+j] = str(n - 1)
  17.                     q.append((x+i, y+j, n-1))
  18.             except:
  19.                 continue
  20.  
  21.  
  22. bfs(matrix, n-1, n-1, n)
  23.  
  24. for row in matrix:
  25.     row = ''.join(row)
  26.     print ' %s ' % (row,)

Animación


Por cierto deberías decirnos el nombre de la comunidad que pone estos retos.