Right turn

 Right turn

Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
 
frog is trapped in a maze. The maze is infinitely large and divided into grids. It also consists of n obstacles, where the i-th obstacle lies in grid (xi,yi).
 
frog is initially in grid (0,0), heading grid (1,0). She moves according to The Law of Right Turn: she keeps moving forward, and turns right encountering a obstacle.
 
The maze is so large that frog has no chance to escape. Help her find out the number of turns she will make.
 

Input

The input consists of multiple tests. For each test:
 
The first line contains 1 integer n (0n103). Each of the following n lines contains 2 integers xi,yi. (|xi|,|yi|109,(xi,yi)(0,0), all (xi,yi) are distinct)
 

Output

For each test, write 1 integer which denotes the number of turns, or -1′′ if she makes infinite turns.
 

Sample Input

2
1 0
0 -1
1
0 1
4
1 0
0 1
0 -1
-1 0

Sample Output

2
0
-1
4种情况的dfs:
    只要把所有情况用代码表达清楚就OK了。这里有几点较为关键:dfs()该传递什么值,如何判断重复,如何找到运动时遇到的第一个障碍物。
    首先,dfs()传递的a[].x,a[].y是障碍物的位置,而物体实际的位置,应该是(a[].x,a[].y-1),(a[].x-1,a[].y),(a[].x,a[].y+1),(a[].x+1,a[].y)这四个不同的状态;所以在判断turn%4后,要先处理一下x和y;所以最初放入dfs()的并不应该是(0,0),而是(0,1)。
    撞到同一个障碍物只有4种方向,并且这4种直接可以用turn%4来表示,判断dis[][]如若有相同,则必有重复。
    turn right 的要求 不过是 遇到一个 同x(or y)的 比当前位置y(or x)  大(or 小) 的最小(or 最大) 值,找到则turn++,dfs();否则结束。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
const int INF = 0x3f3f3f3f;
using namespace std;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};
int dis[1005][4];
int sign;
int turn;
int n;
struct node{
    int x,y;
}a[1005];
void dfs(int cnt){
    if(sign)return;
    int tu = turn%4;
    for(int i = 0; i < 4; i++){
        if(dis[cnt][i] == tu){
            sign = 2;return;
        }
        if(dis[cnt][i] == -1){
            dis[cnt][i] = tu;
            break;
        }
    }
    int j = -1;
    if(dy[tu] == 0){
        if(dx[tu] == 1){
            int x = a[cnt].x;
            int y = a[cnt].y-1;
            int xx = INF;
            for(int i = 1; i <= n; i++){
                if(a[i].x > x && a[i].x < xx && a[i].y == y){
                    xx = a[i].x;
                    j = i;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
        else {
            int x = a[cnt].x;
            int y = a[cnt].y+1;
            int xx = -INF;
            for(int i = 1; i <= n; i++){
                if(a[i].x < x && a[i].x > xx && a[i].y == y){
                    xx = a[i].x;
                    j = i;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
    }
    else {
        if(dy[tu] == -1){
            int x = a[cnt].x-1;
            int y = a[cnt].y;
            int yy = -INF;
            for(int i = 1; i <= n; i++){
                if(a[i].y < y && a[i].y > yy && a[i].x == x){
                    j = i;
                    yy = a[i].y;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
        else {
            int x = a[cnt].x+1;
            int y = a[cnt].y;
            int yy = INF;
            for(int i = 1; i <= n; i++){
                if(a[i].y > y && a[i].y < yy && a[i].x == x){
                    yy = a[i].y;
                    j = i;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
    }
    return;
}
int main(){
    while(~scanf("%d",&n)){
        for(int i = 1; i <= n; i++){
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        memset(dis,-1,sizeof(dis));
        sign = 0;
        turn = 0;
        a[0].x = 0,a[0].y = 1;
        dfs(0);
        if(sign == 2)puts("-1");
        else printf("%d\n",turn);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/ACMessi/p/4852278.html

#include "ti_msp_dl_config.h" #include "main.h" char str[16]; int16_t AR_Speed, AR_Location; int16_t AL_Speed, AL_Location; float relative_yaw, best_yaw, yaw_offset; // 转向控制变量 enum { MODE_STRAIGHT, MODE_TURN_LEFT, MODE_TURN_RIGHT } drive_mode = MODE_STRAIGHT; uint32_t turn_start_time = 0; const uint32_t TURN_DURATION = 800; // 转向持续时间 (ms) const float TURN_ANGLE = 90.0f; // 转向角度 (度) // 状态跟踪变量 static uint8_t action_index = 0; // 当前动作索引 static uint32_t straight_start_time = 0; // 直行开始时间 static uint8_t left_turn_count = 0; // 已执行左转次数 static uint8_t right_turn_count = 0; // 已执行右转次数 // 定义任意顺序的动作序列 #define ACTION_COUNT 10 // 8个动作:4次转向 + 4次直行 typedef enum { ACTION_STRAIGHT, ACTION_LEFT_TURN, ACTION_RIGHT_TURN } ActionType; // 自定义序列: 左-直-右-直-右-直-左-直 const ActionType custom_sequence[ACTION_COUNT] = { ACTION_LEFT_TURN, // 左转 ACTION_STRAIGHT, // 直行 ACTION_RIGHT_TURN, // 右转 ACTION_STRAIGHT, // 直行 ACTION_RIGHT_TURN, // 右转 ACTION_STRAIGHT, // 直行 ACTION_LEFT_TURN, // 左转 ACTION_STRAIGHT, // 直行 ACTION_LEFT_TURN, // 左转 ACTION_STRAIGHT // 直行 }; // 每个动作的等待时间 (ms) const uint32_t WAIT_TIMES[ACTION_COUNT] = { 1200, // 左转1前等待 0, // 直行1持续时间 800, // 右转1前等待 0, // 直行2持续时间 800, // 右转2前等待 0, // 直行3持续时间 800, // 左转2前等待 0, // 直行4持续时间 800, // 左转2前等待 0 // 直行4持续时间 }; // 动作描述(用于显示) const char* ACTION_DESCRIPTIONS[3] = { "STRAIGHT", "LEFT TURN", "RIGHT TURN" }; PID_t AR_Inner = { .Kp = 0.85, .Ki = 0.1, .Kd = 0, .OutMax = 100, .OutMin = -100, }; PID_t AL_Inner = { .Kp = 0.85, .Ki = 0.1, .Kd = 0, .OutMax = 100, .OutMin = -100, }; PID_t angle = { .Kp = 0.15, .Ki = 0, .Kd = 0, .OutMax = 60, .OutMin = -60, }; // 启动左转 void start_left_turn(void) { drive_mode = MODE_TURN_LEFT; turn_start_time = tick_ms; left_turn_count++; } // 启动右转 void start_right_turn(void) { drive_mode = MODE_TURN_RIGHT; turn_start_time = tick_ms; right_turn_count++; } // 进入直行模式 void enter_straight_mode(void) { drive_mode = MODE_STRAIGHT; straight_start_time = tick_ms; angle.Target = 0; // 重置角度目标 } // 执行当前序列动作 void execute_current_action(void) { ActionType current_action = custom_sequence[action_index]; switch (current_action) { case ACTION_LEFT_TURN: start_left_turn(); break; case ACTION_RIGHT_TURN: start_right_turn(); break; case ACTION_STRAIGHT: enter_straight_mode(); break; } // 更新到下一个动作 action_index = (action_index + 1) % ACTION_COUNT; } int main(void) { SYSCFG_DL_init(); SysTick_Init(); WIT_Init(); lcd_init(); PID_Init(); encoder_Init(); // 初始状态为直行 enter_straight_mode(); while (1) { // 检查是否应该执行下一个动作 if (drive_mode == MODE_STRAIGHT) { // 检查是否达到当前动作的等待时间 if (tick_ms - straight_start_time > WAIT_TIMES[action_index]) { execute_current_action(); } } // 显示当前模式 const char* mode_str; switch (drive_mode) { case MODE_STRAIGHT: mode_str = "STRAIGHT"; break; case MODE_TURN_LEFT: mode_str = "LEFT TURN"; break; case MODE_TURN_RIGHT: mode_str = "RIGHT TURN"; break; default: mode_str = "UNKNOWN"; break; } sprintf(str, "Mode: %s", mode_str); LCD_ShowString(0, 48, (unsigned char *)str, RED, WHITE, 16, 0); // 显示当前动作序列 ActionType current_action = custom_sequence[action_index]; sprintf(str, "Action: %s", ACTION_DESCRIPTIONS[current_action]); LCD_ShowString(0, 64, (unsigned char *)str, RED, WHITE, 16, 0); // 显示当前状态持续时间 uint32_t elapsed = 0; uint32_t total_duration = 0; const char* time_label = ""; if (drive_mode == MODE_STRAIGHT) { elapsed = tick_ms - straight_start_time; total_duration = WAIT_TIMES[action_index]; time_label = "Straight:"; } else { elapsed = tick_ms - turn_start_time; total_duration = TURN_DURATION; time_label = "Turn:"; } sprintf(str, "%s %d/%d", time_label, elapsed, total_duration); LCD_ShowString(0, 80, (unsigned char *)str, RED, WHITE, 16, 0); // 显示动作序列索引 sprintf(str, "Index: %u/%d", action_index, ACTION_COUNT-1); LCD_ShowString(0, 96, (unsigned char *)str, RED, WHITE, 16, 0); // 显示转向计数 sprintf(str, "Turns: L:%u R:%u", left_turn_count, right_turn_count); LCD_ShowString(0, 112, (unsigned char *)str, RED, WHITE, 16, 0); // 显示tick_ms sprintf(str, "tick: %lu", tick_ms); LCD_ShowString(0, 128, (unsigned char *)str, RED, WHITE, 16, 0); // 原始显示代码保持不变 sprintf(str, "AR_Target:%+04.0f", AR_Inner.Target); LCD_ShowString(200, 0, (unsigned char *)str, RED, WHITE, 16, 0); sprintf(str, "AR_Out:%+04.0f", AR_Inner.Out); LCD_ShowString(200, 16, (unsigned char *)str, RED, WHITE, 16, 0); sprintf(str, "AR_Actual:%+04.0f", AR_Inner.Actual); LCD_ShowString(200, 32, (unsigned char *)str, RED, WHITE, 16, 0); sprintf(str, "AL_Target:%+04.0f", AL_Inner.Target); LCD_ShowString(0, 0, (unsigned char *)str, RED, WHITE, 16, 0); sprintf(str, "AL_Out:%+04.0f", AL_Inner.Out); LCD_ShowString(0, 16, (unsigned char *)str, RED, WHITE, 16, 0); sprintf(str, "AL_Actual:%+04.0f", AL_Inner.Actual); LCD_ShowString(0, 32, (unsigned char *)str, RED, WHITE, 16, 0); sprintf(str, "yaw:%-6.1f", relative_yaw); LCD_ShowString(0, 144, (unsigned char *)str, RED, WHITE, 16, 0); } } void TIMER_0_INST_IRQHandler(void) { switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST)) { case DL_TIMER_IIDX_ZERO: encoder_value_up(); // 转向控制逻辑 float turn_control = 0; if (drive_mode == MODE_TURN_LEFT || drive_mode == MODE_TURN_RIGHT) { // 检查转向是否完成 if (tick_ms - turn_start_time > TURN_DURATION) { // 转向完成,进入下一个动作 execute_current_action(); } else { // 设置转向控制量(左转为正,右转为负) turn_control = (drive_mode == MODE_TURN_LEFT) ? TURN_ANGLE : -TURN_ANGLE; } } // 角度控制 angle.Target = turn_control; angle.Actual = relative_yaw; PID_Update(&angle); // 速度控制 float forward_speed = 40; AR_Inner.Target = forward_speed + angle.Out; AL_Inner.Target = forward_speed - angle.Out; AR_Speed = AR_encoder(); AL_Speed = AL_encoder(); AR_Location += AR_Speed; AL_Location += AL_Speed; AR_Inner.Actual = AR_Speed; AL_Inner.Actual = AL_Speed; PID_Update(&AR_Inner); PID_Update(&AL_Inner); AR_MOTOR(AR_Inner.Out); AL_MOTOR(AL_Inner.Out); best_yaw = InfiniteYaw(wit_data.yaw); static int first_time = 1; if (first_time) { yaw_offset = best_yaw; first_time = 0; } relative_yaw = best_yaw - yaw_offset; break; default: break; } }为什么我的代码无法实现第三次左转
08-01
一、数据采集层:多源人脸数据获取 该层负责从不同设备 / 渠道采集人脸原始数据,为后续模型训练与识别提供基础样本,核心功能包括: 1. 多设备适配采集 实时摄像头采集: 调用计算机内置摄像头(或外接 USB 摄像头),通过OpenCV的VideoCapture接口实时捕获视频流,支持手动触发 “拍照”(按指定快捷键如Space)或自动定时采集(如每 2 秒采集 1 张),采集时自动框选人脸区域(通过Haar级联分类器初步定位),确保样本聚焦人脸。 支持采集参数配置:可设置采集分辨率(如 640×480、1280×720)、图像格式(JPG/PNG)、单用户采集数量(如默认采集 20 张,确保样本多样性),采集过程中实时显示 “已采集数量 / 目标数量”,避免样本不足。 本地图像 / 视频导入: 支持批量导入本地人脸图像文件(支持 JPG、PNG、BMP 格式),自动过滤非图像文件;导入视频文件(MP4、AVI 格式)时,可按 “固定帧间隔”(如每 10 帧提取 1 张图像)或 “手动选择帧” 提取人脸样本,适用于无实时摄像头场景。 数据集对接: 支持接入公开人脸数据集(如 LFW、ORL),通过预设脚本自动读取数据集目录结构(按 “用户 ID - 样本图像” 分类),快速构建训练样本库,无需手动采集,降低系统开发与测试成本。 2. 采集过程辅助功能 人脸有效性校验:采集时通过OpenCV的Haar级联分类器(或MTCNN轻量级模型)实时检测图像中是否包含人脸,若未检测到人脸(如遮挡、侧脸角度过大),则弹窗提示 “未识别到人脸,请调整姿态”,避免无效样本存入。 样本标签管理:采集时需为每个样本绑定 “用户标签”(如姓名、ID 号),支持手动输入标签或从 Excel 名单批量导入标签(按 “标签 - 采集数量” 对应),采集完成后自动按 “标签 - 序号” 命名文件(如 “张三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值