D. Train Problem I

本文介绍了一个火车调度模拟问题,通过栈和队列的数据结构解决火车进出站顺序的问题。文章提供了完整的代码实现,并详细解释了解题思路。

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

                                                                         D. Train Problem I

1000ms
1000ms
32768KB
64-bit integer IO format: %I64d      Java class name: Main
Font Size:
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

Input

The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.

Output

The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

Sample Input

3 123 321
3 123 312

Sample Output

Yes.
in
in
in
out
out
out
FINISH
No.
FINISH


 
Hint
Hint
For the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1. In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3. Now we can let train 3 leave. But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment. So we output "No.".

解题思路:该题为模拟题,知识点为栈和队列的应用,可以用数组模拟。

最多9辆火车,编号为1,2,3....9,进出站方式入图(先进后出)。给出一个n,表示要进出站得火车数,给出火车进站顺序表,给出火车出站顺序表,问按给出入站表入站的火车能否以出站表顺序出站,如果可以,输出“YES.”以及出入情况信息(in,out),否则输出“NO.”,每组测试案例输出的最后一行以“FINISH”结束。

用3个队列及1个站来模拟,分别存储,火车进站顺序,火车出站顺序,站内火车情况(出入顺序要求情况)。输入火车进、出站顺序表,用字符数组做中介存储,后存入到对应队列。依题意得,火车进出站的次数最多为2*n次(每辆火车进站一次,出站一次),开始就循环结束判断条件纠结了很久,后面才想到。因为火车最多9辆,所以,这个地方不会导致程序运行超时。每次循环,判断出来,如果站内有火车可以按照给定的顺序出站,则让其出站(用队列保存火车出入情况),否则,若有火车未入栈,则让火车入站。最后如果火车进出站情况存储队列中有2*n个数据(所有的火车都进过站并且出来了,按题目给定的顺序)则输出“YES.”和火车出入情况及顺序(直接输出出入情况存储队列),否则输出“NO.”,其余按题意输出即可。


#include<stdio.h>
#include<string.h>    //使用String串
#include<iostream>    //使用c++输出流
#include<stack>    //使用栈
#include<queue>    //使用队列
using namespace std;
int main()
{
    int n,x,i;
    string st;  //输出时内容暂存
    while(scanf("%d",&n)!=EOF)
    {
        queue<int>  in,out;   //存储需要入站,出站的火车编号
        queue<string> sign;    //存储火车存储站的状态(in,out,有序)
        stack<int > s;   //存储现在车站中火车进出顺序
        char a[20];   //存储输入时,火车进、出站顺序,后面会转存到队列
        scanf("%s",a);   //读入火车入站顺序
        for(i=0;i<n;i++)in.push(a[i]-48);    //将火车入站顺序转存到入站队列
        scanf("%s",a);    
        for(i=0;i<n;i++)out.push(a[i]-48);
        x=in.front();   //读取第一辆进站火车的车号
        s.push(x);    //放入栈(站中)
        in.pop();
        sign.push("in");    //记录有一辆火车进站
        int m=2*n-1;    //最多处理2*n次(进n次,出n次)
        while(m--)
        {
            if(!out.empty()&&!s.empty()&&out.front()==s.top())     //若站(栈)内有火车,出站队列中有火车序号,且要处理的为同一辆火车,则处理这辆火车(出战)
            {
                out.pop();   //该火车可以出站
                s.pop();     //该火车出站
                sign.push("out");    //记录有火车出站
            }
            else if(!in.empty())    //若处于出站口的火车不能出站,且还有火车没有进站,则让下一辆火车进站
            {
                x=in.front();   //提取即将进站火车信息
                in.pop();    //该火车将入站
                s.push(x);   //火车入站
                sign.push("in");     //记录有1辆火车入站
            }
        }
        if(sign.size()==n*2)   //若火车出入站的次数为2*n次(每辆火车进一次站,出一次站)
        {
            printf("Yes.\n");    //火车以给出顺序进站,能以给出顺序出站
            for(i=0;i<2*n;i++)
            {
                st=sign.front();
                cout<<st<<endl;
                sign.pop();
            }
        }
        else printf("No.\n");   //火车以给出顺序进站,不能以给出顺序出站
        printf("FINISH\n");
    }
    return 0;
}


<think>We are given a RuntimeError: "No valid images found in D:\datasets\yiquan\dataset\train\labels.cache. Images with incorrectly formatted labels are ignored." This error typically occurs in the context of training object detection models (like YOLO) when the dataset loader cannot find any valid images or the labels are incorrectly formatted. Steps to debug and solve the issue: 1. **Check the dataset paths**: - Ensure that the images and labels are in the correct directories (usually `images` and `labels` subdirectories under `train` and `val`). - The structure should be: D:\datasets\yiquan\dataset\train\images\ [contains images] D:\datasets\yiquan\dataset\train\labels\ [contains label files] 2. **Check the label files**: - Each image should have a corresponding label file with the same name (e.g., `image.jpg` -> `image.txt`). - The label files should be in the format: `class_id center_x center_y width height` (normalized to [0,1]). - There should be no spaces at the beginning or end of lines and each object per line. 3. **Check the .cache file**: - The error mentions `labels.cache`. This file is generated by the dataset to speed up loading. Sometimes it can be corrupted. - Delete the `labels.cache` file and run again. It will be regenerated. 4. **Check the data YAML file**: - The YAML file (usually `data.yaml`) should have the correct paths for `train` and `val`. - Example content of `data.yaml`: train: D:/datasets/yiquan/dataset/train val: D:/datasets/yiquan/dataset/val nc: 10 # number of classes names: ['class1', 'class2', ...] # class names 5. **Check for images with no labels**: - Some implementations skip images that have no corresponding label file. If you have images without labels, you must either provide an empty label file or remove the image. 6. **Check image formats**: - Ensure that the images are in supported formats (like .jpg, .png, etc.) and are not corrupted. 7. **Check the code that loads the dataset**: - If you are using a framework like Ultralytics YOLO, ensure you are using the correct version and that the dataset is being loaded correctly. 8. **Verify the labels**: - You can write a small script to check a few label files to ensure they are correctly formatted. Example of a correct label file (for one object in the image): ``` 0 0.5 0.5 0.1 0.1 ``` 9. **Check for hidden file extensions**: - Sometimes, files might have double extensions (e.g., `image.jpg.txt`). Make sure the label files have the correct extension (`.txt`). 10. **Check the image and label paths in the cache**: - The `labels.cache` file contains a dictionary with image paths and their corresponding label paths. If the paths are incorrect (e.g., absolute paths when relative are expected, or vice versa), it can cause this error. How to regenerate the cache without running the training script? - Simply delete the `labels.cache` file and run the training again. The cache will be rebuilt. If the problem persists, we can write a script to validate the dataset. Here is a Python script to validate the YOLO dataset structure and label formats: Steps for the script: 1. Read the `data.yaml` to get the paths for train and val, and the class names. 2. For each image in the train and val directories: - Check if the corresponding label file exists in the labels directory. - Check if the label file is non-empty (unless you allow empty label files, which are sometimes used for negative samples). - Check the format of each line in the label file. However, note that the error says "Images with incorrectly formatted labels are ignored", so we should focus on the label files. Let's write a validation script: ```python import os import yaml def validate_yolo_dataset(data_yaml_path): with open(data_yaml_path, 'r') as file: data = yaml.safe_load(file) # Get the paths train_path = data['train'] val_path = data['val'] nc = data['nc'] names = data['names'] # We assume the structure: # train_path: .../train # and inside: images/ and labels/ for dataset in [train_path, val_path]: if not dataset: continue print(f"Validating {dataset}...") # Get the images directory and labels directory images_dir = os.path.join(dataset, 'images') labels_dir = os.path.join(dataset, 'labels') # Check if directories exist if not os.path.exists(images_dir): print(f"Images directory {images_dir} does not exist!") continue if not os.path.exists(labels_dir): print(f"Labels directory {labels_dir} does not exist!") continue # Iterate over images image_extensions = ['.jpg', '.jpeg', '.png', '.bmp'] image_files = [f for f in os.listdir(images_dir) if os.path.splitext(f)[1].lower() in image_extensions] for image_file in image_files: base_name = os.path.splitext(image_file)[0] label_file = os.path.join(labels_dir, base_name + '.txt') # Check if label file exists if not os.path.exists(label_file): print(f"Label file for {image_file} does not exist: {label_file}") continue # Check label file content with open(label_file, 'r') as f: lines = f.readlines() if len(lines) == 0: print(f"Label file {label_file} is empty. If intentional, that's okay. Otherwise, check.") for line in lines: parts = line.split() if len(parts) != 5: print(f"Label file {label_file} has incorrect number of elements in line: {line.strip()}") else: # Check the class id class_id = int(parts[0]) if class_id < 0 or class_id >= nc: print(f"Label file {label_file} has class id {class_id} which is out of range [0, {nc-1}]") # Check the coordinates for coord in parts[1:]: try: val = float(coord) if val < 0 or val > 1: print(f"Label file {label_file} has coordinate {coord} which is not in [0,1]") except ValueError: print(f"Label file {label_file} has non-float coordinate: {coord}") print("Validation complete.") # Example usage: data_yaml_path = "D:/datasets/yiquan/dataset/data.yaml" validate_yolo_dataset(data_yaml_path) ``` Note: This script checks for: - Existence of label files for each image. - Non-empty label files (if empty, it prints a message but doesn't fail, because empty labels are allowed for negative samples in some cases). - Each line in the label file has exactly 5 numbers. - The class id is within the range [0, nc-1]. - The coordinates are floats and in [0,1]. If you find any issues with the script above, you can adjust accordingly. After fixing the dataset, delete the `labels.cache` file and rerun the training. If the problem persists, check the framework's issue tracker or documentation for similar errors. Let me know if you need further assistance. §§相关问题§§: 1. 如何解决YOLO训练时遇到的 "No labels found" 错误? 2. YOLO数据集的标准结构是怎样的? 3. 如何将标注数据转换为YOLO格式? 4. 在目标检测中,如何处理类别不平衡问题?</think>### 解决 RuntimeError: No valid images found in labels.cache 这个错误通常发生在使用 YOLO 系列模型(如 Ultralytics YOLOv5/v8)训练时,表明数据集加载器无法找到有效图像,主要原因是标签文件格式错误或路径配置问题。以下是完整的解决方案: --- ### 解决方案步骤 #### 1. **验证数据集结构** 确保你的数据集目录结构符合 YOLO 标准格式: ```bash D:\datasets\yiquan\dataset\ ├── train\ │ ├── images\ # 存放所有训练图片 │ │ ├── image1.jpg │ │ └── ... │ └── labels\ # 存放所有标签文件(.txt) │ ├── image1.txt │ └── ... └── data.yaml # 数据集配置文件 ``` #### 2. **检查标签文件格式** 每个标签文件(如 `image1.txt`)必须是以下格式: ```txt <class_id> <center_x> <center_y> <width> <height> ``` - 所有值需归一化到 `[0, 1]` 区间 - 每行一个目标,例如: ```txt 0 0.45 0.64 0.32 0.43 1 0.12 0.53 0.11 0.15 ``` #### 3. **修复 data.yaml 配置** 确保 `data.yaml` 内容正确(**绝对路径或正确相对路径**): ```yaml train: D:/datasets/yiquan/dataset/train val: D:/datasets/yiquan/dataset/val # 或同 train(无验证集时) nc: 2 # 类别数量(根据实际修改) names: ['class1', 'class2'] # 类别名称 ``` #### 4. **删除损坏的缓存文件** 删除以下文件强制重新生成缓存: ```bash rm D:\datasets\yiquan\dataset\train\labels.cache ``` #### 5. **验证脚本(Python)** 运行此脚本检查数据集完整性: ```python import os import yaml def validate_dataset(dataset_path): # 读取 data.yaml with open(os.path.join(dataset_path, 'data.yaml'), 'r') as f: data = yaml.safe_load(f) # 检查训练集 train_dir = data['train'] print(f"Validating: {train_dir}") # 验证图片和标签匹配 img_dir = os.path.join(train_dir, 'images') label_dir = os.path.join(train_dir, 'labels') for img_file in os.listdir(img_dir): if img_file.lower().endswith(('.png', '.jpg', '.jpeg')): base_name = os.path.splitext(img_file)[0] label_file = os.path.join(label_dir, f"{base_name}.txt") # 检查标签是否存在 if not os.path.exists(label_file): print(f"❌ Missing label: {label_file}") continue # 检查标签内容格式 with open(label_file, 'r') as lbl: lines = lbl.readlines() for i, line in enumerate(lines): parts = line.strip().split() if len(parts) != 5: print(f"❌ Invalid line in {label_file}, line {i+1}: {line.strip()}") elif not all(0 <= float(x) <= 1 for x in parts[1:]): print(f"❌ Coordinates out of range in {label_file}, line {i+1}") print("✅ Validation complete") # 使用示例 validate_dataset("D:/datasets/yiquan/dataset") ``` #### 6. **常见修复场景** - **标签文件为空** → 删除空标签文件或添加有效标注 - **图片格式不支持** → 转换图片为 `.jpg/.png` - **路径大小写问题** → Windows 路径用 `/` 替代 `\` - **特殊字符问题** → 移除路径中的中文/空格(如 `D:\数据集` → `D:\dataset`) --- ### 错误原因说明 - **labels.cache** 是 YOLO 生成的缓存文件,记录图片和标签的映射关系。 - 当标签文件格式错误(如坐标越界、缺少字段)或图片不存在时,会导致缓存为空。 - 删除缓存后,YOLO 会重新解析原始数据并重建缓存。 --- ### 后续建议 1. 使用官方工具验证数据集: ```bash python -m yolov5.utils.autoanchor --data D:/datasets/yiquan/dataset/data.yaml ``` 2. 小批量测试训练: ```bash python train.py --img 640 --batch 16 --epochs 10 --data data.yaml --weights yolov5s.pt ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值