F - Catch That Cow

本问题描述了 Farmer John 如何利用步行和瞬移两种方式,在最短时间内追上静止不动的逃逸奶牛。通过合理规划路径,实现快速追赶。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F - Catch That Cow
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers:  N and  K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

Description The cow bicycling team consists of N (1 <= N <= 20) cyclists. They wish to determine a race strategy which will get one of them across the finish line as fast as possible. Like everyone else, cows race bicycles in packs because that's the most efficient way to beat the wind. While travelling at x laps/minute (x is always an integer), the head of the pack expends x*x energy/minute while the rest of pack drafts behind him using only x energy/minute. Switching leaders requires no time though can only happen after an integer number of minutes. Of course, cows can drop out of the race at any time. The cows have entered a race D (1 <= D <= 100) laps long. Each cow has the same initial energy, E (1 <= E <= 100). What is the fastest possible finishing time? Only one cow has to cross the line. The finish time is an integer. Overshooting the line during some minute is no different than barely reaching it at the beginning of the next minute (though the cow must have the energy left to cycle the entire minute). N, D, and E are integers. Input A single line with three integers: N, E, and D Output A single line with the integer that is the fastest possible finishing time for the fastest possible cow. Output 0 if the cows are not strong enough to finish the race. Sample Input 3 30 20 Sample Output 7 Hint as shown in this chart: leader E pack total used this time---leader--speed--dist---minute 1------1-------5------5------25 2------1-------2------7-------4 3------2*------4------11-----16 4------2-------2------13------4 5------3*------3------16------9 6------3-------2------18------4 7------3-------2------20------4 * = leader switch Source USACO 2002 February
//============================================================================== // WARNING!! This file is overwritten by the Block UI Styler while generating // the automation code. Any modifications to this file will be lost after // generating the code again. // // Filename: D:\Tool\Application\dzbk.cpp // // This file was generated by the NX Block UI Styler // Created by: liugang0213 // Version: NX 8.5 // Date: 06-13-2025 (Format: mm-dd-yyyy) // Time: 09:46 (Format: hh-mm) // //============================================================================== //============================================================================== // Purpose: This TEMPLATE file contains C++ source to guide you in the // construction of your Block application dialog. The generation of your // dialog file (.dlx extension) is the first step towards dialog construction // within NX. You must now create a NX Open application that // utilizes this file (.dlx). // // The information in this file provides you with the following: // // 1. Help on how to load and display your Block UI Styler dialog in NX // using APIs provided in NXOpen.BlockStyler namespace // 2. The empty callback methods (stubs) associated with your dialog items // have also been placed in this file. These empty methods have been // created simply to start you along with your coding requirements. // The method name, argument list and possible return values have already // been provided for you. //============================================================================== //------------------------------------------------------------------------------ //These includes are needed for the following template code //------------------------------------------------------------------------------ #include "dzbk.hpp" using namespace NXOpen; using namespace NXOpen::BlockStyler; //------------------------------------------------------------------------------ // Initialize static variables //------------------------------------------------------------------------------ Session *(dzbk::theSession) = NULL; UI *(dzbk::theUI) = NULL; //------------------------------------------------------------------------------ // Constructor for NX Styler class //------------------------------------------------------------------------------ dzbk::dzbk() { try { // Initialize the NX Open C++ API environment dzbk::theSession = NXOpen::Session::GetSession(); dzbk::theUI = UI::GetUI(); theDlxFileName = "dzbk.dlx"; theDialog = dzbk::theUI->CreateDialog(theDlxFileName); // Registration of callback functions theDialog->AddApplyHandler(make_callback(this, &dzbk::apply_cb)); theDialog->AddOkHandler(make_callback(this, &dzbk::ok_cb)); theDialog->AddUpdateHandler(make_callback(this, &dzbk::update_cb)); theDialog->AddInitializeHandler(make_callback(this, &dzbk::initialize_cb)); theDialog->AddDialogShownHandler(make_callback(this, &dzbk::dialogShown_cb)); } catch(exception& ex) { //---- Enter your exception handling code here ----- throw; } } //------------------------------------------------------------------------------ // Destructor for NX Styler class //------------------------------------------------------------------------------ dzbk::~dzbk() { if (theDialog != NULL) { delete theDialog; theDialog = NULL; } } //------------------------------- DIALOG LAUNCHING --------------------------------- // // Before invoking this application one needs to open any part/empty part in NX // because of the behavior of the blocks. // // Make sure the dlx file is in one of the following locations: // 1.) From where NX session is launched // 2.) $UGII_USER_DIR/application // 3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly // recommended. This variable is set to a full directory path to a file // containing a list of root directories for all custom applications. // e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIR\menus\custom_dirs.dat // // You can create the dialog using one of the following way: // // 1. USER EXIT // // 1) Create the Shared Library -- Refer "Block UI Styler programmer's guide" // 2) Invoke the Shared Library through File->Execute->NX Open menu. // //------------------------------------------------------------------------------ extern "C" DllExport void ufusr(char *param, int *retcod, int param_len) { UF_initialize(); dzbk *thedzbk = NULL; try { thedzbk = new dzbk(); // The following method shows the dialog immediately thedzbk->Show(); } catch(exception& ex) { //---- Enter your exception handling code here ----- dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } if(thedzbk != NULL) { delete thedzbk; thedzbk = NULL; } } //------------------------------------------------------------------------------ // This method specifies how a shared image is unloaded from memory // within NX. This method gives you the capability to unload an // internal NX Open application or user exit from NX. Specify any // one of the three constants as a return value to determine the type // of unload to perform: // // // Immediately : unload the library as soon as the automation program has completed // Explicitly : unload the library from the "Unload Shared Image" dialog // AtTermination : unload the library when the NX session terminates // // // NOTE: A program which associates NX Open applications with the menubar // MUST NOT use this option since it will UNLOAD your NX Open application image // from the menubar. //------------------------------------------------------------------------------ extern "C" DllExport int ufusr_ask_unload() { //return (int)Session::LibraryUnloadOptionExplicitly; return (int)Session::LibraryUnloadOptionImmediately; //return (int)Session::LibraryUnloadOptionAtTermination; } //------------------------------------------------------------------------------ // Following method cleanup any housekeeping chores that may be needed. // This method is automatically called by NX. //------------------------------------------------------------------------------ extern "C" DllExport void ufusr_cleanup(void) { try { //---- Enter your callback code here ----- } catch(exception& ex) { //---- Enter your exception handling code here ----- dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } } int dzbk::Show() { try { theDialog->Show(); } catch(exception& ex) { //---- Enter your exception handling code here ----- dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return 0; } //------------------------------------------------------------------------------ //---------------------Block UI Styler Callback Functions-------------------------- //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //Callback Name: initialize_cb //------------------------------------------------------------------------------ void dzbk::initialize_cb() { try { qiucha = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("qiucha")); mubiao = dynamic_cast<NXOpen::BlockStyler::BodyCollector*>(theDialog->TopBlock()->FindBlock("mubiao")); group = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group")); gongju = dynamic_cast<NXOpen::BlockStyler::BodyCollector*>(theDialog->TopBlock()->FindBlock("gongju")); separator0 = dynamic_cast<NXOpen::BlockStyler::Separator*>(theDialog->TopBlock()->FindBlock("separator0")); separator01 = dynamic_cast<NXOpen::BlockStyler::Separator*>(theDialog->TopBlock()->FindBlock("separator01")); juli = dynamic_cast<NXOpen::BlockStyler::DoubleBlock*>(theDialog->TopBlock()->FindBlock("juli")); vector<NXOpen::TaggedObject *> mbt = mubiao->GetSelectedObjects(); for(int a = 0; a < mbt.size(); a++) { tag_t mbttag = mbt[a]->Tag(); UF_DISP_set_highlight(mbttag,1); } } catch(exception& ex) { //---- Enter your exception handling code here ----- dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } } //------------------------------------------------------------------------------ //Callback Name: dialogShown_cb //This callback is executed just before the dialog launch. Thus any value set //here will take precedence and dialog will be launched showing that value. //------------------------------------------------------------------------------ void dzbk::dialogShown_cb() { try { //---- Enter your callback code here ----- } catch(exception& ex) { //---- Enter your exception handling code here ----- dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } } //------------------------------------------------------------------------------ //Callback Name: apply_cb //------------------------------------------------------------------------------ int dzbk::apply_cb() { int errorCode = 0; try { double pyl = juli->Value(); // 圆柱面偏置量(用户输入) double chamferOffset = -0.03; // 沉头平面固定偏置-0.03 vector<NXOpen::TaggedObject *> gjt = gongju->GetSelectedObjects(); vector<NXOpen::TaggedObject *> mbt = mubiao->GetSelectedObjects(); // 存储原始目标体的所有面(求差前的面) std::set<tag_t> originalFaces; for(int a = 0; a < mbt.size(); a++) { tag_t mbttag = mbt[a]->Tag(); UF_DISP_set_highlight(mbttag,1); uf_list_p_t originalFaceList = NULL; UF_MODL_ask_body_faces(mbttag, &originalFaceList); int originalCount = 0; UF_MODL_ask_list_count(originalFaceList, &originalCount); for(int i = 0; i < originalCount; i++) { tag_t faceTag = NULL_TAG; UF_MODL_ask_list_item(originalFaceList, i, &faceTag); originalFaces.insert(faceTag); } UF_MODL_delete_list(&originalFaceList); } // 存储已经偏置过的面(避免重复偏置) std::set<tag_t> offsetFaces; for(int g = 0; g < gjt.size(); g++) { tag_t gjtTag = gjt[g]->Tag(); for(int a = 0; a < mbt.size(); a++) { tag_t mbttag = mbt[a]->Tag(); tag_t frec_eid = NULL_TAG; UF_MODL_subtract_bodies_with_retained_options(mbttag, gjtTag, 0, 1, &frec_eid); // 求差,保留工具体 UF_DISP_set_highlight(mbttag,0); // 获取求差后的面 uf_list_p_t faceList = NULL; UF_MODL_ask_body_faces(mbttag, &faceList); int listCount = 0; UF_MODL_ask_list_count(faceList, &listCount); for (int i = 0; i < listCount; i++) { tag_t faceTag = NULL_TAG; UF_MODL_ask_list_item(faceList, i, &faceTag); // 如果面是原始面或已经偏置过,则跳过 if(originalFaces.find(faceTag)!=originalFaces.end()||offsetFaces.find(faceTag)!=offsetFaces.end()) { continue; } int faceSubtype = 0; UF_MODL_ask_face_type(faceTag, &faceSubtype); // (1) 如果是圆柱面,按输入值偏置 if (faceSubtype == 16) // 16=圆柱面 { char offsetStr[32]; sprintf(offsetStr, "%f", pyl); uf_list_p_t faces = NULL; UF_MODL_create_list(&faces); UF_MODL_put_list_item(faces, faceTag); tag_t feature_obj_id = NULL_TAG; UF_MODL_create_face_offset(offsetStr, faces, &feature_obj_id); UF_MODL_delete_list(&faces); offsetFaces.insert(faceTag); // 标记为已偏置 } // (2) 如果是平面(沉头面),额外偏置-0.03 else if (faceSubtype == 22) // 22=平面 { char offsetStr[32]; sprintf(offsetStr, "%f", chamferOffset); uf_list_p_t faces = NULL; UF_MODL_create_list(&faces); UF_MODL_put_list_item(faces, faceTag); tag_t feature_obj_id = NULL_TAG; UF_MODL_create_face_offset(offsetStr, faces, &feature_obj_id); UF_MODL_delete_list(&faces); offsetFaces.insert(faceTag); // 标记为已偏置 } } UF_MODL_delete_list(&faceList); } } } catch(exception& ex) { //---- Enter your exception handling code here ----- errorCode = 1; dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return errorCode; } //------------------------------------------------------------------------------ //Callback Name: update_cb //------------------------------------------------------------------------------ int dzbk::update_cb(NXOpen::BlockStyler::UIBlock* block) { try { if(block == mubiao) { //---------Enter your code here----------- } else if(block == gongju) { //---------Enter your code here----------- } else if(block == separator0) { //---------Enter your code here----------- } else if(block == separator01) { //---------Enter your code here----------- } else if(block == juli) { //---------Enter your code here----------- } } catch(exception& ex) { //---- Enter your exception handling code here ----- dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return 0; } //------------------------------------------------------------------------------ //Callback Name: ok_cb //------------------------------------------------------------------------------ int dzbk::ok_cb() { int errorCode = 0; try { errorCode = apply_cb(); } catch(exception& ex) { //---- Enter your exception handling code here ----- errorCode = 1; dzbk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return errorCode; UF_terminate(); } //------------------------------------------------------------------------------ //Function Name: GetBlockProperties //Description: Returns the propertylist of the specified BlockID //------------------------------------------------------------------------------ PropertyList* dzbk::GetBlockProperties(const char *blockID) { return theDialog->GetBlockProperties(blockID); } 这是我的程序 将这个功能加进去
最新发布
06-17
### 在现有C++ NX Open程序中添加新功能的实现方法 要在现有的C++ NX Open程序中添加新功能,可以按照以下逻辑进行扩展。以下内容详细描述了如何在现有代码框架下集成新功能。 #### 1. 确保程序结构清晰 在NX二次开发中,通常使用`NXOpen::Session`和`NXOpen::Part`对象来管理会话和部件。确保现有程序中已经正确初始化了这些对象[^2]。 ```cpp NXOpen::Session* theSession = NXOpen::Session::GetSession(); NXOpen::Part* workPart = theSession->Parts()->Work; ``` #### 2. 添加新功能模块 假设需要添加一个布尔求差功能模块,可以按照以下步骤实现: ##### (1) 创建布尔构建器 通过`NXOpen::Features::BooleanBuilder`创建布尔操作构建器,并设置目标体和工具体[^1]。 ```cpp NXOpen::Features::BooleanBuilder* booleanBuilder = workPart->Features()->CreateBooleanBuilder(NXOpen::Features::BooleanType::Subtract); booleanBuilder->TargetBodies()->Add(targetBody); for (auto toolBody : toolBodies) { booleanBuilder->ToolBodies()->Add(toolBody); } ``` ##### (2) 提交布尔操作 完成参数设置后,调用`Commit()`方法执行布尔操作,并清理构建器资源。 ```cpp booleanBuilder->Commit(); booleanBuilder->Destroy(); ``` #### 3. 处理异常 为了增强程序的健壮性,应捕获并处理可能发生的异常。可以参考异常处理逻辑[^3]。 ```cpp try { // 执行布尔操作 booleanBuilder->Commit(); } catch (const NXException& ex) { // 输出异常信息并转换编码 uc1601(UTF8ToGB(ex.what()), 1); } catch (const std::exception& ex) { // 处理标准C++异常 uc1601(UTF8ToGB(ex.what()), 1); } ``` #### 4. 动态更新选择控件 如果需要动态更新选择控件的内容,可以通过过滤用户选择的对象来实现。例如,在选择工具体时排除目标体[^2]。 ```cpp std::vector<NXOpen::TaggedObject*> selectToolBodies(NXOpen::TaggedObject* targetBody) { std::vector<NXOpen::TaggedObject*> toolBodies; std::vector<NXOpen::TaggedObject*> selectedObjects = edgeSelect0->GetSelectedObjects(); for (auto obj : selectedObjects) { if (obj != targetBody) { // 排除目标体 toolBodies.push_back(obj); } } edgeSelect0->SetSelectedObjects(toolBodies); // 更新选择对象 return toolBodies; } ``` #### 5. 集成新功能到主程序 将上述模块集成到现有程序中,确保新功能与现有功能协调工作。例如,在主程序的菜单或按钮事件中调用布尔求差功能。 ```cpp void onBooleanOperation() { NXOpen::TaggedObject* targetBody = selectTargetBody(); std::vector<NXOpen::TaggedObject*> toolBodies = selectToolBodies(targetBody); performBooleanSubtract(targetBody, toolBodies); } ``` --- ### 注意事项 - 确保目标体和工具体的类型一致,例如都为实体(Solid Body)。 - 如果需要保留原始对象,可以在布尔操作中设置相关参数。 - 跨装配布尔运算可能需要额外处理链接体。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值