uva_112_Tree Summing

本文介绍了一种通过输入表达式构建二叉树并搜索特定路径的方法。利用递归方式生成节点,并通过前序遍历判断是否存在从根节点到叶子节点的路径,使得路径上节点值之和等于给定值。此算法适用于解决计算机科学中树结构相关的问题。
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAXN    1001

typedef struct _BTREENODE {
        int data;
        struct _BTREENODE *left_child;
        struct _BTREENODE *right_child;
}BTREENODE;

char ch, num[MAXN/100];
int stack[MAXN], s_pos, flag;

void get_node_data(BTREENODE **node)
{
        if( !s_pos ) {
                (*node) = NULL; return;
        }
        int idx(0), mark(0), val;
        while( ~scanf("%c", &ch) && s_pos ) {
                if( '(' == ch ) {
                        stack[s_pos ++] = ch; mark ++;
                }
                else if( ')' == ch ) {
                        s_pos --;
                }
                else if( '-' == ch || isdigit(ch) ) {
                        num[idx ++] = ch;
                }
                if( mark ) {
                        if( idx ) {
                                num[idx] = '\0'; sscanf(num, "%d", &val);
                        }
                        break;
                }
        }
        if( !idx ) {
                (*node) = NULL; return;
        }
        (*node) = new BTREENODE; (*node)->data = val;
        get_node_data(&(*node)->left_child);  get_node_data(&(*node)->right_child);
}

void pre_order(BTREENODE *node, int cur_sum, const int &search_key)
{
        if( NULL == node || flag ) {
                return;
        }
        if( NULL == node->left_child && NULL == node->right_child && 
            cur_sum+node->data == search_key ) {
                flag = 1; return;
        }
        pre_order(node->left_child, cur_sum+node->data, search_key);
        pre_order(node->right_child, cur_sum+node->data, search_key);
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int search_key;
        BTREENODE *root;
        while( ~scanf("%d", &search_key) ) {
                while( scanf("%c", &ch) && '(' != ch ) {}
                s_pos = 0; stack[s_pos ++] = ch; flag = 0;
                get_node_data(&root); pre_order(root, 0, search_key);
                if( flag ) {
                        printf("yes\n"); continue;
                }
                printf("no\n");
        }
        return 0;
}

### Rotated Bounding Box Single IoU Implementation and Explanation In computer vision tasks involving object detection, especially when dealing with rotated objects such as in aerial imagery or specific orientations of items within an image, traditional axis-aligned bounding boxes may not provide accurate representations. For this reason, calculating Intersection over Union (IoU) for rotated bounding boxes becomes crucial. #### Definition of Rotated Bounding Boxes A rotated bounding box is defined by its center coordinates \((cx, cy)\), width \(w\), height \(h\), and rotation angle \(\theta\) relative to the horizontal axis[^1]. This representation allows more precise fitting around non-axis-aligned objects compared to standard rectangular bounds. #### Calculation Methodology To compute IoU between two rotated rectangles, one approach involves converting each rectangle into a polygon format using their respective parameters. The intersection area can then be calculated through geometric operations on these polygons while union areas are derived from subtracting intersections from summed individual areas: \[ IOU = \frac{Area_{Intersection}}{Area_{Union}} \] Where: - \( Area_{Intersection} \): Calculated via clipping algorithms like Sutherland-Hodgman. - \( Area_{Union} \): Derived from summing up both shapes' areas minus overlapping section. This process requires careful handling due to potential complexities introduced by rotations which might lead to self-intersecting cases during conversion steps. For practical implementations, libraries such as `shapely` offer built-in functions that simplify working with geometrical entities including computing intersections efficiently without manually implementing low-level geometry processing routines. ```python from shapely.geometry import Polygon def calculate_rotated_iou(box_a_params, box_b_params): """ Calculate IoU given parameter sets describing two rotated bounding boxes Parameters: box_a_params ((float,float,float,float,float)): Tuple containing cx,cy,w,h,angle for first bbox box_b_params ((float,float,float,float,float)): Same structure but for second bbox Returns: float: Value representing computed IoU score ranging [0,1] """ # Convert parametric form into Polygons poly_a = create_polygon_from_box(*box_a_params) poly_b = create_polygon_from_box(*box_b_params) inter_area = poly_a.intersection(poly_b).area union_area = poly_a.union(poly_b).area return inter_area / union_area if union_area != 0 else 0 def create_polygon_from_box(cx, cy, w, h, theta): """Helper function creating Shapely Polygon instances.""" corners = [ (-w/2,-h/2), (+w/2,-h/2), (+w/2,+h/2), (-w/2,+h/2)] transformed_corners = [(corner[0]*cos(theta)-corner[1]*sin(theta)+cx, corner[0]*sin(theta)+corner[1]*cos(theta)+cy) for corner in corners] return Polygon(transformed_corners) ``` The provided code snippet demonstrates how to implement IoU calculation specifically tailored towards scenarios where bounding boxes have arbitrary angles associated with them. Utilizing external packages helps streamline development efforts allowing focus on higher level logic rather than intricate details involved in manipulating spatial data structures directly. --related questions-- 1. How do different types of transformations affect accuracy metrics used in evaluating models? 2. What challenges arise when applying conventional evaluation measures to specialized datasets? 3. Can you explain alternative methods besides IoU for assessing performance in object detection systems? 4. In what ways could pre-processing techniques improve alignment between predicted and ground truth annotations?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值