Problem:


Koch Curve

Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n.

The Koch curve is well known as a kind of fractals.

You can draw a Koch curve in the following algorithm:

Divide a given segment (p1, p2) into three equal segments.

Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.

Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).

You should start (0, 0), (100, 0) as the first segment.

Input

An integer n is given.

Output

Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4.

Constraints

 0 ≤ n ≤ 6

Sample Input 1

1

Sample Output 1

0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Sample Input 2

2

Sample Output 2

0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000

Notes



Sample input:
1


Sample output:
0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 # 宇都宮涼
 # J5-170029
 # Kannoudou
 # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0168
 # 右中央

import math
import copy

def EqualParts(a,b):
    li = []
    s = [0,0]
    t = [0,0]
    s[0] = (2*a[0]+b[0])/3.0
    s[1] = (2*a[1]+b[1])/3.0
    t[0] = (a[0]+2*b[0])/3.0
    t[1] = (a[1]+2*b[1])/3.0
    li.append(s)
    li.append(t)
    return li

def triangle(a,b):
    u = [0,0]
    if a[1] == b[1]:
        u[0] = (a[0]+b[0])/2.0
        u[1] = (b[0]-a[0])/2.0*math.sqrt(3)
    else:
        if a[0] < b[0]:
            if a[1] > b[1]:
                u[0] = b[0]+(b[0]-a[0])
                u[1] = a[1]
            else:
                u[0] = a[0]-(b[0]-a[0])
                u[1] = b[1]
        else:
            if a[1] > b[1]:
                u[0] = a[0]-(b[0]-a[0])
                u[1] = b[1]
            else:
                u[0] = b[0]+(b[0]-a[0])
                u[1] = a[1]
    return u

def koch(n):
    if n == 0:
        return ([[0.0,0.0],[100.0, 0.0]])
    else:
        li = koch(n-1)
        answer = copy.copy(li)
        for i in range(len(li)-1):
            temp = EqualParts(li[i],li[i+1])
            s = temp[0]
            t = temp[1]
            u = triangle(s,t)
            answer.insert(3*i+1+i,s)
            answer.insert(3*i+2+i,u)
            answer.insert(3*i+3+i,t)
        return answer

n = int(input())
answer = koch(n)
for j in range(len(answer)):
    print(' '.join(map(str,answer[j])))

Submission status: Wrong Answer

Solution code:
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        print("{} {}".format(self.x,self.y))
        
def divide_line_n(a,b,dots,n):
    dx=abs(a.x-b.x)
    dy=abs(a.y-b.y)
    for i in range(1,n):
        dots.append(Dot((dx*i)/n,(dy*i)/n))

if __name__ == "__main__":
    dots=[]
    dots.append(Dot(0,0))
    dots.append(Dot(100,0))
    n=int(input())
    for i in range(n):
        divide_line_n(dots[i],dots[i+1],dots,3)
        divide_line_n(dots[-1],dots[-2],dots,2)
    dots.sort()
    for i in dots:
        i.print()

Submission status: Wrong Answer

Solution code:
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        print("{} {}".format(self.x,self.y))
def divide_line_2(a,b,dots):
    dx=abs(a.x-b.x)
    dy=abs(a.y-b.y)
    dot1=Dot(dx/2,dy/2)
    dot1.x+=dy/2
    dot1.y+=dx/2
    dots.append(dot1)      
def divide_line_3(a,b,dots):
    dx=abs(a.x-b.x)
    dy=abs(a.y-b.y)
    for i in range(1,3):
        dots.append(Dot((dx*i)/3,(dy*i)/3))

if __name__ == "__main__":
    dots=[]
    dots.append(Dot(0,0))
    dots.append(Dot(100,0))
    n=int(input())
    for i in range(n):
        divide_line_3(dots[i],dots[i+1],dots)
        divide_line_2(dots[-1],dots[-2],dots)
    dots.sort()
    for i in dots:
        i.print()

Submission status: Wrong Answer

Solution code:
import math

def pr8(a):
	print("{:.10}".format(a[0]),end=" ")
	print("{:.10}".format(a[1]))

def koch(n,p1,p2):
	if n==0:
		return
	s=[0 for i in range(2)]
	t=[0 for i in range(2)]
	u=[0 for i in range(2)]
	
	s[0]=(2*p1[0]+1*p2[0])/3
	s[1]=(2*p1[1]+1*p2[1])/3
	t[0]=(1*p1[0]+2*p2[0])/3
	t[1]=(1*p1[1]+2*p2[1])/3
	
	rad=math.radians(60)
	
	u[0]=(t[0]-s[0])*math.cos(rad)-(t[1]-s[1])*math.sin(rad)+s[0]
	u[1]=(t[0]-s[0])*math.sin(rad)+(t[1]-s[1])*math.cos(rad)+s[1]
	
	koch(n-1,p1,s)
	#pr8(s)
	print(*s)	
	koch(n-1,s,u)
	#pr8(t)
	print(*t)
	koch(n-1,u,t)
	#pr8(u)
	print(*u)
	koch(n-1,t,p2)
	
n=int(input())
p1=[0,0]
p2=[100,0]

print(*p1)
koch(n,p1,p2)
print(*p2)

Submission status: Wrong Answer

Solution code:
import math
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        print("{} {}".format(self.x,self.y))
def divide_line_2(a,b,dots):
    x=(b.x-a.x)*math.cos(60*math.pi/180)-(b.y-a.y)*math.sin(60*math.pi/180)+a.x
    y=(b.x-a.x)*math.sin(60*math.pi/180)-(b.y-a.y)*math.cos(60*math.pi/180)+a.y
    dot1=Dot(x,y)
    dots.append(dot1)       
def divide_line_3(a,b,dots):
    dot1=Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3)
    dot2=Dot((a.x+2*b.x)/3,(a.y+2*b.y)/3)
    dots.append(dot1)
    dots.append(dot2)
    divide_line_2(dot1,dot2,dots)

if __name__ == "__main__":
    dots=[]
    dots.append(Dot(0,0))
    dots.append(Dot(100,0))
    n=int(input())
    for i in range(n):
        m=len(dots)
        for j in range(1,m):
            divide_line_3(dots[j-1],dots[j],dots)
        dots.sort()
    for i in dots:
        i.print()

Submission status: Wrong Answer

Solution code:
import math
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        print("{} {}".format(self.x,self.y))       
def kock(n,a,b):
    if n == 0:
        return
    d1=Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3)
    d2=Dot((a.x+2*b.x)/3,(a.y+2*b.y)/3)
    x=(d2.x-d1.x)*math.cos(60*math.pi/180)-(d2.y-d1.y)*math.sin(60*math.pi/180)+d1.x
    y=(d2.x-d1.x)*math.sin(60*math.pi/180)-(d2.y-d1.y)*math.cos(60*math.pi/180)+d1.y
    d3=Dot(x,y)
    kock(n-1,a,d1)
    d1.print()
    kock(n-1,d1,d3)
    d3.print()
    kock(n-1,d3,d2)
    d2.print()
    kock(n-1,d2,b)

if __name__ == "__main__":
    n=int(input())
    dot1=Dot(0,0)
    dot2=Dot(100,0)
    dot1.print()
    kock(n,dot1,dot2)
    dot2.print()

Submission status: Wrong Answer

Solution code:
import math
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        print("{0:.8f} {0:.8f}".format(self.x,self.y))       
def kock(n,a,b):
    if n == 0:
        return
    d1=Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3)
    d2=Dot((a.x+2*b.x)/3,(a.y+2*b.y)/3)
    x=(d2.x-d1.x)*math.cos(60*math.pi/180)-(d2.y-d1.y)*math.sin(60*math.pi/180)+d1.x
    y=(d2.x-d1.x)*math.sin(60*math.pi/180)-(d2.y-d1.y)*math.cos(60*math.pi/180)+d1.y
    d3=Dot(x,y)
    kock(n-1,a,d1)
    d1.print()
    kock(n-1,d1,d3)
    d3.print()
    kock(n-1,d3,d2)
    d2.print()
    kock(n-1,d2,b)

if __name__ == "__main__":
    n=int(input())
    dot1=Dot(0,0)
    dot2=Dot(100,0)
    dot1.print()
    kock(n,dot1,dot2)
    dot2.print()

Submission status: Wrong Answer

Solution code:
import math
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        x=self.x
        y=self.y
        print("{:.8f} {:.8f}".format(x,y))       
def kock(n,a,b):
    if n == 0:
        return
    d1=Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3)
    d2=Dot((a.x+2*b.x)/3,(a.y+2*b.y)/3)
    x=(d2.x-d1.x)*math.cos(60*math.pi/180)-(d2.y-d1.y)*math.sin(60*math.pi/180)+d1.x
    y=(d2.x-d1.x)*math.sin(60*math.pi/180)-(d2.y-d1.y)*math.cos(60*math.pi/180)+d1.y
    d3=Dot(x,y)
    kock(n-1,a,d1)
    d1.print()
    kock(n-1,d1,d3)
    d3.print()
    kock(n-1,d3,d2)
    d2.print()
    kock(n-1,d2,b)

if __name__ == "__main__":
    n=int(input())
    dot1=Dot(0,0)
    dot2=Dot(100,0)
    dot1.print()
    kock(n,dot1,dot2)
    dot2.print()

Submission status: Wrong Answer

Solution code:
import math
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        x=self.x
        y=self.y
        print("{:.8f} {:.8f}".format(x,y))       
def kock(n,a,b):
    if n == 0:
        return
    s=Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3)
    t=Dot((a.x+2*b.x)/3,(a.y+2*b.y)/3)
    rad=math.radians(60)
    x=(t.x-s.x)*math.cos(rad)-(t.y-s.y)*math.sin(rad)+s.x
    y=(t.x-s.x)*math.sin(rad)-(t.y-s.y)*math.cos(rad)+s.y
    u=Dot(x,y)

    kock(n-1,a,s)
    s.print()
    kock(n-1,s,u)
    u.print()
    kock(n-1,u,t)
    t.print()
    kock(n-1,t,b)

if __name__ == "__main__":
    n=int(input())
    dot1=Dot(0,0)
    dot2=Dot(100,0)
    dot1.print()
    kock(n,dot1,dot2)
    dot2.print()

Submission status: Wrong Answer

Solution code:
import math

n = int(input())

sin60 = (math.sqrt)(3)/2
cos60 = 1/2


def koch(n, p1_x, p2_x, p1_y, p2_y):
    if n == 0:
        return

    s_x = (2*p1_x + 1*p2_x) / 3
    s_y = (2*p1_y + 1*p2_y) / 3
    t_x = (1*p1_x + 2*p2_x) / 3
    t_y = (1*p1_y + 2*p2_y) / 3
    u_x = (t_x - s_x)*cos60 - (t_y - s_y)*sin60 + s_x
    u_y = (t_x - s_x)*sin60 + (t_y - s_y)*cos60 + s_y

    koch(n-1, p1_x, s_x, p1_y, s_y)
    print(s_x, s_y)
    koch(n-1, s_x, u_x, s_y, u_y)
    print(u_x, u_y)
    koch(n-1, u_x, t_x, u_y, t_y)
    print(t_x, t_y)
    koch(n-1, t_x, p2_x, t_y, p2_y)


print(0, 0)
koch(n, 0, 100, 0, 0)
print(0, 100)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())

sin60 = (math.sqrt)(3)/2
cos60 = 1/2


def koch(n, p1_x, p2_x, p1_y, p2_y):
    if n == 0:
        return

    s_x = (2*p1_x + 1*p2_x) / 3
    s_y = (2*p1_y + 1*p2_y) / 3
    t_x = (1*p1_x + 2*p2_x) / 3
    t_y = (1*p1_y + 2*p2_y) / 3
    u_x = (t_x - s_x)*cos60 - (t_y - s_y)*sin60 + s_x
    u_y = (t_x - s_x)*sin60 + (t_y - s_y)*cos60 + s_y

    koch(n-1, p1_x, s_x, p1_y, s_y)
    print(s_x, s_y)
    koch(n-1, s_x, u_x, s_y, u_y)
    print(u_x, u_y)
    koch(n-1, u_x, t_x, u_y, t_y)
    print(t_x, t_y)
    koch(n-1, t_x, p2_x, t_y, p2_y)


print(0, 0)
koch(n, 0, 100, 0, 0)
print(0, 100)

Submission status: Wrong Answer

Solution code:
import math
def f(n, p1, p2):
    if n == 0: return
    s = []
    t = []
    u = []
    s.append((2*p1[0] + p2[0])/3)
    s.append((2*p1[1] + p2[1])/3)
    t.append((p1[0] + 2*p2[0])/3)
    t.append((p1[1] + 2*p2[1])/3)
    u.append((t[0]-s[0])*math.cos(math.radians(60))
             - (t[1]-s[1])*math.sin(math.radians(60)) + s[0])
    u.append((t[0]-s[0])*math.sin(math.radians(60))
             - (t[1]-s[1])*math.cos(math.radians(60)) + s[1])
    f(n-1, p1, s)
    print(s[0],s[1])
    f(n-1, s, u)
    print(u[0],u[1])
    f(n-1, u, t)
    print(t[0],t[1])
    f(n-1, t, p2)


n = int(input())
p1 = [0,0]
p2 = [100,0]
print(p1[0],p1[1])
f(n, p1, p2)
print(p2[0],p2[1])

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
p1 = (0, 0)
p2 = (100, 0)
points = []

def koch(n, p, q):
    if n == 0:
        return

    x1, y1 = p
    x2, y2 = q
    sx = (x1 + x2) / 3
    sy = (y1 + y2) / 3
    s = (sx, sy)
    tx = (x1 + x2) * 2 / 3
    ty = (y1 + y2) * 2 / 3
    t = (tx, ty)
    theta = math.pi / 3
    ux = (tx - sx) * math.cos(theta) - (ty - sy) * math.sin(theta) + sx
    uy = (tx - sx) * math.sin(theta) + (ty - sy) * math.cos(theta) + sy
    u = (ux, uy)

    koch(n - 1, p, s)
    print(*s)
    koch(n - 1, s, u)
    print(*u)
    koch(n - 1, u, t)
    print(*t)
    koch(n - 1, t, q)


print(*p1)
koch(n, p1, p2)
print(*p2)

Submission status: Wrong Answer

Solution code:
import math

a=math.sin(math.radians(60))
b=math.cos(math.radians(60))

n=int(input())

def f(n,x1,y1,x2,y2):
	if n==0:
		return
		
	sx=(x1*2+x2*1)/3
	sy=(y1*2+y2*1)/3
	tx=(x1*1+x2*2)/3
	ty=(y1*1+y2*2)/3
	ux=(tx-sx)*b-(ty-sy)*a+sx
	uy=(tx-sx)*a-(ty-sy)*b+sy
	
	f(n-1,x1,y1,sx,sy)
	print(sx,sy)
	f(n-1,sx,sy,ux,uy)
	print(ux,uy)
	f(n-1,ux,uy,tx,ty)
	print(tx,ty)
	f(n-1,tx,ty,x2,y2)
	
	
print(0.00000000,0.00000000)
f(n,0,0,100,0)
print(100.00000000,0.00000000)

Submission status: Wrong Answer

Solution code:
import math

def koch(n,p1x,p1y,p2x,p2y):
	if n==0:
		return
	sx=p1x+(p2x-p1x)/3.0
	sy=p1y+(p2y-p1y)/3.0
	ux=p1x+sx*3.0/2.0
	uy=p1y+sx*math.sqrt(3.0)/2.0
	tx=p1x+(p2x-p1x)*2.0/3.0
	ty=p1y+(p2y-p1y)*2.0/3.0
	koch(n-1,p1x,p1y,sx,sy)
	koch(n-1,sx,sy,ux,uy)
	koch(n-1,ux,uy,tx,ty)
	koch(n-1,tx,ty,p2x,p2y)

	print(sx,sy)
	print(ux,uy)
	print(tx,ty)

n=int(input())

print(0,0)
koch(n,0,0,100,0)
print(100,0)

Submission status: Wrong Answer

Solution code:
def stdinput():
    from sys import stdin
    return stdin.readline().strip()

def main():
    n = int(stdinput())
    nodes = [[0., 0.], [100., 0.]]

    for _ in range(n):
        next_nodes = []
        for s_node, e_node in zip(nodes[:], nodes[1:]):
            next_nodes += koch(s_node, e_node)
        nodes = next_nodes

    for node in nodes:
        print(*map(lambda a: f'{round(a,8):.8f}', node))

def koch(start, end):
    import math
    nodes = [start]

    s = [
        start[0] + (end[0] - start[0]) / 3,
        start[1] + (end[1] - start[1]) / 3
    ]
    t = [
        start[0] + (end[0] - start[0]) * 2 / 3,
        start[1] + (end[1] - start[1]) * 2 / 3
    ]

    nodes.append(s)

    dx = (t[0] - s[0]) * math.sqrt(3) / 2
    dy = (t[1] - s[1]) * math.sqrt(3) / 2

    u0 = [s[0] - dy, s[1] + dx]
    u1 = [t[0] - dy, t[1] + dx]

    u = [
        (u0[0] + u1[0]) / 2,
        (u0[1] + u1[1]) / 2
    ]
    nodes.append(u)

    nodes.append(t)

    nodes.append(end)

    return nodes

if __name__ == '__main__':
    main()
    # import cProfile
    # cProfile.run('main()')

Submission status: Accepted

Solution code:
import math

class Pos:
	def __init__(self, x, y):
		self.x = x
		self.y = y
	def __str__(self):
		return str(self.x) + ' ' + str(self.y)

def koch_curve(i, n, start, end):
	if i < n:
		f = get_pos(start, end, 1/3)
		t = get_pos(start, end, 2/3)
		s = get_sec_pos(start, end, f, t)
		koch_curve(i + 1, n, start, f)
		print(f)
		koch_curve(i + 1, n, f, s)
		print(s)
		koch_curve(i + 1, n, s, t)
		print(t)
		koch_curve(i + 1, n, t, end)

def get_pos(start, end, m):
	dX = (end.x - start.x) * m
	dY = (end.y - start.y) * m
	return Pos(start.x + dX, start.y + dY)

def get_sec_pos(start, end, f, t):
	x = y = 0
	if f.y == t.y:
		x = start.x + (end.x - start.x) / 2
		len = math.fabs(t.x - f.x)
		h = math.sqrt(len**2 - (len/2)**2)
		if start.x < end.x:
			y = start.y + h
		else:
			y = start.y - h
	elif f.x < t.x and f.y < t.y:
		x = start.x
		y = t.y
	elif f.x > t.x and f.y < t.y:
		x = end.x
		y = f.y
	elif f.x > t.x and f.y > t.y:
		x = start.x
		y = t.y
	else:
		x = end.x
		y = f.y
	return Pos(x, y)
	
n = int(input())
start = Pos(0, 0)
end = Pos(100, 0)
print(start)
koch_curve(0, n, start, end)
print(end)

Submission status: Accepted

Solution code:
import math

class Pos:
	def __init__(self, x, y):
		self.x = x
		self.y = y
	def __str__(self):
		return str(self.x) + ' ' + str(self.y)

def koch_curve(i, n, start, end):
	if i < n:
		f = get_pos(start, end, 1/3)
		t = get_pos(start, end, 2/3)
		s = get_sec_pos(start, end, f, t)
		koch_curve(i + 1, n, start, f)
		print(f)
		koch_curve(i + 1, n, f, s)
		print(s)
		koch_curve(i + 1, n, s, t)
		print(t)
		koch_curve(i + 1, n, t, end)

def get_pos(start, end, m):
	dX = (end.x - start.x) * m
	dY = (end.y - start.y) * m
	return Pos(start.x + dX, start.y + dY)

def get_sec_pos(start, end, f, t):
	if f.y == t.y:
		x = start.x + (end.x - start.x) / 2
		len = math.fabs(t.x - f.x)
		h = math.sqrt(len**2 - (len/2)**2)
		if start.x < end.x:
			y = start.y + h
		else:
			y = start.y - h
	elif f.x < t.x and f.y < t.y:
		x = start.x
		y = t.y
	elif f.x > t.x and f.y < t.y:
		x = end.x
		y = f.y
	elif f.x > t.x and f.y > t.y:
		x = start.x
		y = t.y
	else:
		x = end.x
		y = f.y
	return Pos(x, y)
	
n = int(input())
start = Pos(0, 0)
end = Pos(100, 0)
print(start)
koch_curve(0, n, start, end)
print(end)

Submission status: Accepted

Solution code:
def recKF(p1,p2,n):
    if n != 0:
        a = 1/2
        b = 3**(1/2)/2
        s = Point( p1.x + (p2.x-p1.x)/3, p1.y + (p2.y-p1.y)/3 )
        t = Point( p1.x + (p2.x-p1.x)*2/3, p1.y + (p2.y-p1.y)*2/3 )
        v = Point( t.x-s.x, t.y-s.y )
        rot_v = Point( v.x*a - v.y*b, b*v.x + a*v.y )
        u = Point( rot_v.x + s.x, rot_v.y + s.y)
        recKF(p1,s,n-1)
        print(s.x,s.y)
        recKF(s,u,n-1)
        print(u.x,u.y)
        recKF(u,t,n-1)
        print(t.x,t.y)
        recKF(t,p2,n-1)
        
from collections import namedtuple
Point = namedtuple("Point", ["x","y"])
p1 = Point(0,0)
p2 = Point(100,0)
n = int(input())
print(p1.x,p1.y)
recKF(p1,p2,n)
print(p2.x,p2.y)

Submission status: Accepted

Solution code:
def koch_curve(n, x1, y1, x2, y2):
    if n == 0:
        return [(x1, y1)]
    s_x = (2 * x1 + x2) / 3
    s_y = (2 * y1 + y2) / 3
    t_x = (x1 + 2 * x2) / 3
    t_y = (y1 + 2 * y2) / 3
    # 回転行列を用いて
    u_x = (t_x - s_x) * .5 - (t_y - s_y) * (3 ** .5 / 2) + s_x
    u_y = (t_x - s_x) * (3 ** .5 / 2) + (t_y - s_y) * .5  + s_y
    ret = koch_curve(n - 1, x1, y1, s_x, s_y) + \
        koch_curve(n - 1, s_x, s_y, u_x, u_y) + \
        koch_curve(n - 1, u_x, u_y, t_x, t_y) + \
        koch_curve(n - 1, t_x, t_y, x2, y2)
    return ret
for x, y in koch_curve(int(input()), 0, 0, 100, 0):
    print('{:.8f} {:.8f}'.format(x, y))
print('{:.8f} {:.8f}'.format(100, 0))

Submission status: Accepted

Solution code:
import math

n = int(input())

def dividing_point(p1, p2, ratio):
    return ( p1[0]*(1-ratio) + p2[0]*ratio, p1[1]*(1-ratio) + p2[1]*ratio)

def rotate_vec_60deg(v):
    return (v[0]/2 - math.sqrt(3)*v[1]/2, math.sqrt(3)*v[0]/2 + v[1]/2)
    

def koch(depth, p_left, p_right):
    if depth == 0:
        return
    
    s = dividing_point(p_left, p_right, 1/3)
    t = dividing_point(p_left, p_right, 2/3)
    tmpvec = rotate_vec_60deg((s[0] - p_left[0], s[1] - p_left[1] ))
    u = (s[0] + tmpvec[0], s[1] + tmpvec[1])
    
    koch(depth - 1, p_left, s)
    print(s[0], s[1])
    koch(depth - 1, s, u)
    print(u[0], u[1])
    koch(depth - 1, u, t)
    print(t[0], t[1])
    koch(depth - 1, t, p_right)
    
    
print(0.0, 0.0)
koch(n, (0,0), (100, 0))
print(100.0, 0)

Submission status: Accepted

Solution code:
import math
class Point:
	def __init__(self,x,y):
		self.x=x
		self.y=y
	def __str__(self):
		return str(self.x)+' '+str(self.y)
	
def koch(n,a,b):
	if n==0: return
	s=Point(0,0)
	t=Point(0,0)
	u=Point(0,0)
	th=math.pi*60/180
	s.x=(2*a.x+1*b.x)/3
	s.y=(2*a.y+1*b.y)/3
	t.x=(1*a.x+2*b.x)/3
	t.y=(1*a.y+2*b.y)/3
	u.x=(t.x-s.x)*math.cos(th)-(t.y-s.y)*math.sin(th)+s.x
	u.y=(t.x-s.x)*math.sin(th)+(t.y-s.y)*math.cos(th)+s.y
	
	koch(n-1,a,s)
	print(s)
	koch(n-1,s,u)
	print(u)
	koch(n-1,u,t)
	print(t)
	koch(n-1,t,b)

n=int(input())
a=Point(0,0)
b=Point(100,0)
print(a)
koch(n,a,b)
print(b)

Submission status: Accepted

Solution code:
import math
sqrt3 = math.sqrt(3.0)

def koch_curve(n, i, pt1, pt2, pt_array):
    if i > n:
        return
    pt1x, pt1y = pt1
    pt2x, pt2y = pt2
    pts = (2/3 * pt1x + 1/3 * pt2x, 2/3 * pt1y + 1/3 * pt2y)
    ptt = (1/3 * pt1x + 2/3 * pt2x, 1/3 * pt1y + 2/3 * pt2y)
    ptx = ptt[0] - pts[0]
    pty = ptt[1] - pts[1]
    ptu = (1/2 * (ptx - sqrt3 * pty) + pts[0], 1/2 * (sqrt3 * ptx + pty) + pts[1])

    koch_curve(n, i+1, pt1, pts, pt_array)
    pt_array.append(pts)
    koch_curve(n, i+1, pts, ptu, pt_array)
    pt_array.append(ptu)
    koch_curve(n, i+1, ptu, ptt, pt_array)
    pt_array.append(ptt)
    koch_curve(n, i+1, ptt, pt2, pt_array)
    return

def main():
    n = int(input())
    pt_array = []
    begin = (0.0, 0.0)
    end = (100.0, 0.0)
    pt_array.append(begin)
    koch_curve(n, 1, begin, end, pt_array)
    pt_array.append(end)
    for pt in pt_array:
        print("{:.6f} {:.6f}".format(pt[0], pt[1]))

main()

Submission status: Accepted

Solution code:
import math

def koch(d, p1, p2):
    if d == 0:
        return

    s_x = (2 * p1[0] + 1 * p2[0])/3
    s_y = (2 * p1[1] + 1 * p2[1])/3
    s = [s_x, s_y]
    t_x = (1 * p1[0] + 2 * p2[0])/3
    t_y = (1 * p1[1] + 2 * p2[1])/3
    t = [t_x, t_y]
    rotated = math.pi * 60 / 180
    u_x = (t_x - s_x) * math.cos(rotated) - (t_y - s_y) * math.sin(rotated) + s_x
    u_y = (t_x - s_x) * math.sin(rotated) + (t_y - s_y) * math.cos(rotated) + s_y
    u = [u_x, u_y]

    koch(d-1, p1, s)
    print(' '.join(map(str, s)))
    koch(d-1, s, u)
    print(' '.join(map(str, u)))
    koch(d-1, u, t)
    print(' '.join(map(str, t)))
    koch(d-1, t, p2)
    

def main():
    n  = int(input())
    p1 = [0.00000000,0.00000000]
    p2 = [100.00000000,0.00000000]
    print(' '.join(map(str, p1)))
    koch(n, p1, p2)
    print(' '.join(map(str, p2)))
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

"""
コッホ曲線
参考：https://www.mynote-jp.com/entry/2016/04/30/201249
　　　https://atarimae.biz/archives/23930
・回転行列を使うと、向きの変わった座標が出せる。
・そのために行列の計算方法も確認。
　→m行n列成分は、「左の行列のm行目」と「右の行列のn列目」の内積
"""

from math import sin, cos, radians

N = int(input())

def dfs(p1x, p1y, p2x, p2y, depth):
    if depth == N:
        return
    sx = p1x + (p2x-p1x)/3
    tx = p1x + (p2x-p1x)/3*2
    sy = p1y + (p2y-p1y)/3
    ty = p1y + (p2y-p1y)/3*2
    # s,tを基準の位置として左回りに60度回転させる
    ux = sx + (tx-sx) * cos(radians(60)) - (ty-sy) * sin(radians(60))
    uy = sy + (tx-sx) * sin(radians(60)) + (ty-sy) * cos(radians(60))
    # 再帰が終了した場所から出力していけば、線分上の各頂点の順番になる
    dfs(p1x, p1y, sx, sy, depth+1)
    print(sx, sy)
    dfs(sx, sy, ux, uy, depth+1)
    print(ux, uy)
    dfs(ux, uy, tx, ty, depth+1)
    print(tx, ty) 
    dfs(tx, ty, p2x, p2y, depth+1)

print(0, 0)
dfs(0, 0, 100, 0, 0)
print(100, 0)

Submission status: Accepted

Solution code:
import math
n = int(input())

class point:
    x = 0
    y = 0

def kock(k, p1, p2):
    s = point()
    t = point()
    u = point()
    if k == 0:
        return
    s.x = (2*p1.x+p2.x)/3
    s.y = (2*p1.y+p2.y)/3
    t.x = (p1.x+2*p2.x)/3
    t.y = (p1.y+2*p2.y)/3
    u.x = (t.x-s.x)*math.cos(math.radians(60))-(t.y-s.y)*math.sin(math.radians(60))+s.x
    u.y = (t.x-s.x)*math.sin(math.radians(60))+(t.y-s.y)*math.cos(math.radians(60))+s.y
    kock(k-1, p1, s)
    print(s.x, s.y)
    kock(k-1, s, u)
    print(u.x, u.y)
    kock(k-1, u, t)
    print(t.x, t.y)
    kock(k-1, t, p2)


a = point()
b = point()
b.x = 100
print(a.x, a.y)
kock(n, a, b)
print(b.x, b.y)

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
p1 = complex(0, 0)
p2 = complex(100, 0)
phi = complex(1/2, sqrt(3)/2)

def makeCurve(p1, p2, cnt):
    if cnt >= n:
        return
    s = p1 + (p2-p1)/3
    t = p1 + (p2-p1)*2/3
    u = (t-s)*phi+s
    
    
    makeCurve(p1, s, cnt + 1)
    print(s.real, s.imag)
    makeCurve(s, u, cnt + 1)
    print(u.real, u.imag)
    makeCurve(u, t, cnt + 1)
    print(t.real, t.imag)
    makeCurve(t, p2, cnt + 1)

print(p1.real, p1.imag)
makeCurve(p1, p2, 0)
print(p2.real, p2.imag)

Submission status: Accepted

Solution code:
import math
import collections

N = int(input())
Point = collections.namedtuple('Point', ['x', 'y'])


def koch(d, p1, p2):
    if d == 0:
        return
    th = math.pi * 60 / 180
    s = Point(x=(2 * p1.x + p2.x) / 3, y=(2 * p1.y + p2.y) / 3)
    t = Point(x=(p1.x + 2 * p2.x) / 3, y=(p1.y + 2 * p2.y) / 3)
    u = Point(x=(t.x - s.x) * math.cos(th) - (t.y - s.y) * math.sin(th) + s.x,
              y=(t.x - s.x) * math.sin(th) + (t.y - s.y) * math.cos(th) + s.y)
    koch(d - 1, p1, s)
    print('{:.8f} {:.8f}'.format(s.x, s.y))
    koch(d - 1, s, u)
    print('{:.8f} {:.8f}'.format(u.x, u.y))
    koch(d - 1, u, t)
    print('{:.8f} {:.8f}'.format(t.x, t.y))
    koch(d - 1, t, p2)


p1 = Point(x=0, y=0)
p2 = Point(x=100, y=0)
print('{:.8f} {:.8f}'.format(p1.x, p1.y))
koch(N, p1, p2)
print('{:.8f} {:.8f}'.format(p2.x, p2.y))

Submission status: Accepted

Solution code:
import math
N = int(input())
ans = [(0, 0)]
def rec(i, lx, ly, rx, ry):
    if i == N:
        return ans
    else:
        x1 = (2 * lx + rx) / 3
        y1 = (2 * ly + ry) / 3
        x3 = (2 * rx + lx) / 3
        y3 = (2 * ry + ly) / 3
        x2 = (x3 - x1) * math.cos(math.pi/3) \
             - (y3 - y1) * math.sin(math.pi/3) + x1
        y2 = (x3 - x1) * math.sin(math.pi/3) \
             + (y3 - y1) * math.cos(math.pi/3) + y1
        

        rec(i+1, lx, ly, x1, y1)
        ans.append((x1, y1))

        rec(i+1, x1, y1, x2, y2)
        ans.append((x2, y2))

        rec(i+1, x2, y2, x3, y3)
        ans.append((x3, y3))

        rec(i+1, x3, y3, rx, ry)

        return ans
        

answer = rec(0, 0, 0, 100, 0)
answer.append((100, 0))
for pair in answer:
    print(*pair)

Submission status: Accepted

Solution code:
l=map(complex,[0,1e2])
for _ in range(input()):
    t,l=l,[0]
    for i,j in zip(t[:-1],t[1:]):
        d=(j-i)/3
        l.extend((i+d,i+d+d*complex(1,3**0.5)/2,i+d*2,j))
for i in l:
    print i.real,i.imag

Submission status: Accepted

Solution code:
def next_koch(n):
    turn60 = 0.5 + 3 ** 0.5 * 0.5j
    n_next = [n[0]]
    for i in range(1, len(n)):
        s = (n[i - 1] * 2 + n[i]) / 3
        t = (n[i - 1] + n[i] * 2) / 3
        u = (t - s) * turn60 + s
        n_next +=[s, u, t, n[i]]
    return n_next

p1 = 0 + 0j
p2 = 100 + 0j
koch = [p1, p2]

n = int(input())
for i in range(n):
    koch = next_koch(koch)

for z in koch:
    print("{:.8f} {:.8f}".format(z.real, z.imag))

Submission status: Accepted

Solution code:
import math
sin60 = math.sin(60*math.pi/180)
def koch(n,p1,p2):
    if n==0:
        return
    s = [(2*p1[0]+1*p2[0])/3., (2*p1[1]+1*p2[1])/3.]
    t = [(1*p1[0]+2*p2[0])/3., (1*p1[1]+2*p2[1])/3.]
    u = [(t[0]-s[0])*0.5 - (t[1]-s[1])*sin60+ s[0],
        (t[0]-s[0])*sin60 + (t[1]-s[1])*0.5 + s[1]]
    koch(n-1,p1,s)
    print s[0],s[1]
    koch(n-1,s,u)
    print u[0],u[1]
    koch(n-1,u,t)
    print t[0],t[1]
    koch(n-1,t,p2)
       
n = int(raw_input())
p1 = (0,0)
p2 = (100,0)
print p1[0],p1[1]
koch(n,p1,p2)
print p2[0],p1[1]

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_C
# Koch Curve
# Result:
import sys
import math

# 60 degree
degree = math.pi / 3

def print_point(point):
    print '%f %f' % (point[0], point[1])

def koch(depth, p1, p2):
    if depth == 0:
        return
    s = ((2 * p1[0] + p2[0]) / 3, (2 * p1[1] + p2[1]) / 3)
    t = ((p1[0] + 2 * p2[0]) / 3, (p1[1] + 2 * p2[1]) / 3)
    u = ((t[0] - s[0]) * math.cos(degree) - (t[1] - s[1]) * math.sin(degree) + s[0],
         (t[0] - s[0]) * math.sin(degree) + (t[1] - s[1]) * math.cos(degree) + s[1])
    depth -= 1
    koch(depth, p1, s)
    print_point(s)
    koch(depth, s, u)
    print_point(u)
    koch(depth, u, t)
    print_point(t)
    koch(depth, t, p2)

n_itr = int(sys.stdin.readline())
start = (0.0, 0.0)
end = (100.0, 0.0)
print_point(start)
koch(n_itr, start, end)
print_point(end)

Submission status: Accepted

Solution code:
import math


def koch(n, p1, p2):
    if n == 0:
        return
    s = [(2 * p1[0] + p2[0]) / 3, (2 * p1[1] + p2[1]) / 3]
    t = [(p1[0] + 2 * p2[0]) / 3, (p1[1] + 2 * p2[1]) / 3]
    u = [(t[0] - s[0]) * math.cos(math.radians(60))
         - (t[1] - s[1]) * math.sin(math.radians(60)) + s[0],
         (t[0] - s[0]) * math.sin(math.radians(60))
         + (t[1] - s[1]) * math.cos(math.radians(60)) + s[1]]

    koch(n - 1, p1, s)
    print(s[0], s[1])
    koch(n - 1, s, u)
    print(u[0], u[1])
    koch(n - 1, u, t)
    print(t[0], t[1])
    koch(n - 1, t, p2)

n = int(input())
p1 = (0, 0)
p2 = (100, 0)
print(p1[0], p1[1])
koch(n, p1, p2)
print(p2[0], p2[1])

Submission status: Accepted

Solution code:
import math
def kochCurve(A):
    B = []
    for i in xrange(len(A)-1):
        p1 = A[i]
        p2 = A[i+1]
        s = ((A[i][0] * 2.0 + A[i+1][0] * 1.0)/3, (A[i][1] * 2.0 + A[i+1][1] * 1.0)/3)
        t = ((A[i][0] * 1.0 + A[i+1][0] * 2.0)/3, (A[i][1] * 1.0 + A[i+1][1] * 2.0)/3)
        x = t[0] - s[0]
        y = t[1] - s[1]
        rad = math.radians(60)
        ux = x * math.cos(rad) - y * math.sin(rad)
        uy = x * math.sin(rad) + y * math.cos(rad)
        u = (ux + s[0], uy + s[1])
        B.extend([p1, s, u, t])
    B.append(A[len(A)-1])
    return B

n = int(raw_input())
A = [(0, 0), (100, 0)]
for i in xrange(n):
    A = kochCurve(A)
for x in A:
    print x[0], x[1]

Submission status: Accepted

Solution code:
import math
class point:
    def __init__(self):
        self.x = 0.0
        self.y = 0.0

def koch(n,a,b):
    if n == 0 :
        return

    rad = 1.0471975511965976 
    s = point()
    t = point()
    u = point()
    tmp = point()

    s.x = (2 * a.x + b.x)/3.0
    s.y = (2 * a.y + b.y)/3.0

    t.x = (2 * b.x + a.x)/3.0
    t.y = (2 * b.y + a.y)/3.0

    tmp.x,tmp.y = s.x-t.x ,s.y-t.y
    u.x = (tmp.x * math.cos(rad) + tmp.y * math.sin(rad)) + t.x
    u.y = (-tmp.x * math.sin(rad) + tmp.y * math.cos(rad)) + t.y


    koch(n-1,a,s)
    print(s.x,s.y)

    koch(n-1,s,u)
    print(u.x,u.y)

    koch(n-1,u,t)
    print(t.x,t.y)

    koch(n-1,t,b)

    return

a = point()
b = point()
a.x,a.y = 0.0,0.0
b.x,b.y = 100.0,0.0
n = int(input())
print(a.x,a.y)
koch(n,a,b)
print(b.x,b.y)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


SIN60 = 0.86602540378
COS60 = 0.5


class KochCurve(object):

    def __init__(self, n, p1=(0.0, 0.0), p2=(100.0, 0.0)):
        self.depth = n
        self.p1_init = p1
        self.p2_init = p2
        self.points = [p1]

    def koch(self, n=None, p1=None, p2=None):
        if n is None:
            n = self.depth
        if p1 is None:
            p1 = self.p1_init
        if p2 is None:
            p2 = self.p2_init
        if n != 0:
            s = ((2 * p1[0] + p2[0]) / 3, (2 * p1[1] + p2[1]) / 3)
            t = ((p1[0] + 2 * p2[0]) / 3, (p1[1] + 2 * p2[1]) / 3)
            ux = (t[0] - s[0]) * COS60 - (t[1] - s[1]) * SIN60 + s[0]
            uy = (t[0] - s[0]) * SIN60 + (t[1] - s[1]) * COS60 + s[1]
            u = (ux, uy)
            self.koch(n - 1, p1, s)
            self.points.append(s)
            self.koch(n - 1, s, u)
            self.points.append(u)
            self.koch(n - 1, u, t)
            self.points.append(t)
            self.koch(n - 1, t, p2)

    def draw_koch(self):
        self.koch()
        self.points.append(self.p2_init)


def main():
    k = KochCurve(n=int(input()))
    k.draw_koch()
    for p in k.points:
        print("{x:.8f} {y:.8f}".format(x=p[0], y=p[1]))


if __name__ == "__main__":
    main()