ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/1485
package main
import (
"bufio"
"fmt"
"math"
"os"
"sort"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var t int
fmt.Fscanln(reader, &t)
for i := 0; i < t; i++ {
var coordinates []coordinateFormat
for j := 0; j < 4; j++ {
var coordinate = coordinateFormat{}
fmt.Fscanln(reader, &coordinate.x, &coordinate.y)
coordinates = append(coordinates, coordinate)
}
sort.Slice(coordinates, func(i, j int) bool {
if coordinates[i].x < coordinates[j].x {
return true
} else if coordinates[i].x == coordinates[j].x {
return coordinates[i].y < coordinates[j].y
}
return false
})
fmt.Fprintln(writer, checkSquare(coordinates))
}
}
type coordinateFormat struct {
x float64
y float64
}
// if square return 1 else return 0
func checkSquare(c []coordinateFormat) int {
sideOne := math.Pow(c[0].x-c[1].x, 2.0) + math.Pow(c[0].y-c[1].y, 2.0)
sideTwo := math.Pow(c[1].x-c[3].x, 2.0) + math.Pow(c[1].y-c[3].y, 2.0)
if sideOne != sideTwo {
return 0
}
sideThree := math.Pow(c[3].x-c[2].x, 2.0) + math.Pow(c[3].y-c[2].y, 2.0)
if sideTwo != sideThree {
return 0
}
sideFour := math.Pow(c[2].x-c[0].x, 2.0) + math.Pow(c[2].y-c[0].y, 2.0)
if sideThree != sideFour {
return 0
}
diagonalOne := math.Pow(c[0].x-c[3].x, 2.0) + math.Pow(c[0].y-c[3].y, 2.0)
diagonalTwo := math.Pow(c[2].x-c[1].x, 2.0) + math.Pow(c[2].y-c[1].y, 2.0)
if diagonalOne != diagonalTwo || sideOne+sideTwo != diagonalOne {
return 0
}
return 1
}
๐จ Python3
# https://www.acmicpc.net/problem/1485
import sys
def check_square(c):
side_one = (c[0][0] - c[1][0])**2 + (c[0][1] - c[1][1])**2
side_two = (c[1][0] - c[3][0])**2 + (c[1][1] - c[3][1])**2
if side_one != side_two:
return 0
side_three = (c[3][0] - c[2][0])**2 + (c[3][1] - c[2][1])**2
if side_two != side_three:
return 0
side_four = (c[2][0] - c[0][0])**2 + (c[2][1] - c[0][1])**2
if side_three != side_four:
return 0
diagonal_one = (c[0][0] - c[3][0])**2 + (c[0][1] - c[3][1])**2
diagonal_two = (c[2][0] - c[1][0])**2 + (c[2][1] - c[1][1])**2
if diagonal_one != diagonal_two or side_one + side_two != diagonal_one:
return 0
return 1
if __name__ == "__main__":
t = int(sys.stdin.readline())
for i in range(t):
coordinates = []
for j in range(4):
x, y = list(map(int,sys.stdin.readline().split()))
coordinates.append((x, y))
coordinates.sort(key=lambda a: (a[0], a[1]))
print(check_square(coordinates))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 2670๋ฒ / ์ฐ์๋ถ๋ถ์ต๋๊ณฑ [Go][Python3] (0) | 2021.02.12 |
---|---|
BOJ / 1543๋ฒ / ๋ฌธ์ ๊ฒ์ [Go][Python3] (0) | 2021.02.11 |
BOJ / 2217๋ฒ / ๋กํ [Go][Python3] (0) | 2021.02.09 |
BOJ / 4659๋ฒ / ๋น๋ฐ๋ฒํธ ๋ฐ์ํ๊ธฐ [Go][Python3] (0) | 2021.02.08 |
BOJ / 11656๋ฒ / ์ ๋ฏธ์ฌ ๋ฐฐ์ด [Go][Python3] (0) | 2021.02.07 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- Algorithm
- go
- ์๊ณ ๋ฆฌ์ฆ
- python3
- ์ด๋ถํ์
- ํด์๋งต
- ๋งฅ๋ถํ๋ก
- ๋ฐ๋
- Golang
- ์๊ฐ๊ต์ฒด
- Macbook pro 2012 mid 13
- ๋ธ๋ฃจํธํฌ์ค
- BOJ
- dfs
- ์คํ
- dp
- ๋ชฝ๊ณ ๋๋น
- ํ๋ก์ด๋์์ฌ
- BFS
- MongoDB
- AWS
- ์๋ฐ
- ๋ถํ ์ ๋ณต
- ํ
- ๋งฅ๋ถ
- ballet
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ฐฑ์ค
- baekjoon
- java
- Total
- Today
- Yesterday