完美矩形: N 个小矩形可以完美拼接成一个大矩形,不缺,不重
重点在于如何保证不缺和不重复
- 先对所有的输入进行遍历找到 最终的左下角,右上角
- 检查如果有超越,左右点的边界的 则舍弃
- 检查是否为完美矩形(除了标记,每个端点必然完全重合,而且是两次)
package main import ( "fmt" "os" "bufio" "strconv" "strings" ) type node struct { x1 int y1 int x2 int y2 int } func main(){ var n int fmt.Scanf("%d",&n) arr := make([]node,n) scanner := bufio.NewScanner(os.Stdin) scanner.Split(bufio.ScanWords) for i:=0;i<n;i++{ if scanner.Scan(){ arr[i].x1 ,_ = strconv.Atoi(scanner.Text()) } if scanner.Scan(){ arr[i].y1 ,_ = strconv.Atoi(scanner.Text()) } if scanner.Scan(){ arr[i].x2 ,_ = strconv.Atoi(scanner.Text()) } if scanner.Scan(){ arr[i].y2 ,_ = strconv.Atoi(scanner.Text()) } } bOk := helper(arr,n) if bOk == true { fmt.Println("Yes") }else{ fmt.Println("No") } return } func helper(arr []node ,n int) bool{ index1,index2 := 0,0 //求出边界 for i:=0;i<n;i++{ //找打了更小的左下角了 if arr[index1].x1 >= arr[i].x1 && arr[index1].y1 >= arr[i].y1{ index1 = i } //找到了更大的右上角了 if arr[index2].x2 <= arr[i].x2 && arr[index2].y2 <=arr[i].y2 { index2 = i } } //可以用或的关系先干掉一波 for i:=0;i<n;i++{ bOk := arr[i].x1 <arr[index1].x1 || arr[i].y1 < arr[index1].y1 || arr[i].x2 > arr[index2].x2 || arr[i].y2 > arr[index2].y2 if bOk == true { return false } } //将每个四边形的四个点都进行一下统计 tmpmap := make(map[string]int ) for i:=0;i<n;i++{ A := strconv.Itoa(arr[i].x1) + "-" + strconv.Itoa(arr[i].y1) B := fmt.Sprintf("%d-%d",arr[i].x1,arr[i].y2) C := fmt.Sprintf("%d-%d",arr[i].x2,arr[i].y2) D := fmt.Sprintf("%d-%d",arr[i].x2,arr[i].y1) tmpmap[A]++ tmpmap[B]++ tmpmap[C]++ tmpmap[D]++ } //对统计结果进行计算 for key,val := range tmpmap { //有偶数个重合的可以直接跳过 if val % 2 == 0 { continue } //将key 进行拆分 tmp := strings.Split(key,"-") //fmt.Println(tmp,key) x1,_ := strconv.Atoi(tmp[0]) y1,_ := strconv.Atoi(tmp[1]) if x1 == arr[index1].x1 || x1== arr[index2].x1 || y1==arr[index1].y1 || y1== arr[index2].y2{ continue; } return false } return true }