U E D R , A S I H C RSS

Spiral Array/임��

� 방향별로 ��터가 ��하는 회수를 세는 방법으로 만들어봄.
가령 4x5 � array�면,
1 2 3 4
14 15 16 5
13 20 17 6
12 19 18 7
11 10 9 8

�런 순으로 배열� �게 �는�, (시작�과 ��� 제외하고) ��터� ��방향� 나열해보면 →→→ ↓↓↓↓↓ ��� ↑↑↑ →→ ↓↓ � ↑ 순으로 �다. 여기�는 간단한 규칙성� �기는� (첫줄� 움�임� 제외) 다�과 같� 수�으로 �현� 수 있다.

현재 방향으로 진행횟수 = 반대 방향� 진행회수+1

� �으로 간단한 수열� 만들 수 있고, � 수열로 배열� ��스를 바꿔가며 값� 채워나간다.

처��는 다른 디��으로 접근� 했는�, �단� 문제를 풀어보�는 취지 하� 제� 쉽게 풀릴것 같� 방법� 사용하였다.

~cpp 
# -*- coding: UTF-8 -*
import unittest

def makeRotList(row, col):
	rotList = []
	row -= 1
	col -= 1

	rotList.append(row)
	while True:
		#cols
		rotList.append(col)
		if col == 1:
			rotList.append(row)
			break

		#rows
		rotList.append(row)
		if row == 1:
			rotList.append(col-1)	# 제� 첫줄� (루프전� )�미 리스트� 넣었기 때문� 1� 빼야 함
			break
		
		row-=1
		col-=1
	return rotList

def makeSpirialArray(rows, cols):
	s = Sequence()

	SpirialArray = []
	for i in range(rows):
		SpirialArray += [[0]*cols]

	index = [0,0]
	value = 0

	SpirialArray[index[0]][index[1]] = value + 1
	value += 1
	
	rotList = makeRotList(rows, cols)
	
	for i in rotList:
		dir = s.next()
		for j in range(i):
			index[0] += dir[0]
			index[1] += dir[1]
			SpirialArray[index[0]][index[1]] = value + 1
			value += 1

			print index

	return SpirialArray


class Sequence:
	def __init__(self):
		self.sequence = [[1,0],[0,1],[-1,0],[0,-1]]
		self.idx = 3
	def next(self):
		if self.idx == 3:
			self.idx = 0
		else:
			self.idx += 1
		return self.sequence[self.idx]

class MyTest(unittest.TestCase):
	def testNextSeqence(self):
		s = Sequence()
		self.assertEquals([1,0], s.next())
		self.assertEquals([0,1], s.next())
		self.assertEquals([-1,0], s.next())
		self.assertEquals([0,-1], s.next())
		self.assertEquals([1,0], s.next())

	def testRotList(self):
		expected = [5,3,5,2,4,1,3]
		self.assertEquals(expected, makeRotList(6,4))
		expected = [3,5,3,4,2,3,1,2]
		self.assertEquals(expected, makeRotList(4,6))
		expected = [2,2,2,1,1]
		self.assertEquals(expected, makeRotList(3,3))
		expected = [3,3,3,2,2,1,1]
		self.assertEquals(expected, makeRotList(4,4))

	def testArrayCreation(self):
		testArr = [[0]*3]*3
		expected = [[0,0,0],[0,0,0],[0,0,0]]
		self.assertEquals(expected, testArr)

if __name__=='__main__':
	#unittest.main(argv=('','-v'))
	print makeSpirialArray(3,3)
	print makeSpirialArray(6,4)

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:05
Processing time 0.0038 sec