50 years, 50 colors

Description

On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it's so nice, isn't it? To celebrate this meaningful day, the ACM team of HDU hold some fuuny games. Especially, there will be a game named "crashing color balloons".       

There will be a n*n matrix board on the ground, and each grid will have a color balloon in it.And the color of the ballon will be in the range of [1, 50].After the referee shouts "go!",you can begin to crash the balloons.Every time you can only choose one kind of balloon to crash, we define that the two balloons with the same color belong to the same kind.What's more, each time you can only choose a single row or column of balloon, and crash the balloons that with the color you had chosen. Of course, a lot of students are waiting to play this game, so we just give every student k times to crash the balloons.       

Here comes the problem: which kind of balloon is impossible to be all crashed by a student in k times.       


              

Input

There will be multiple input cases.Each test case begins with two integers n, k. n is the number of rows and columns of the balloons (1 <= n <= 100), and k is the times that ginving to each student(0 < k <= n).Follow a matrix A of n*n, where Aij denote the color of the ballon in the i row, j column.Input ends with n = k = 0.       
              

Output

For each test case, print in ascending order all the colors of which are impossible to be crashed by a student in k times. If there is no choice, print "-1".       
              

Sample Input

    
    
1 1 1 2 1 1 1 1 2 2 1 1 2 2 2 5 4 1 2 3 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 5 1 2 3 4 3 3 50 50 50 50 50 50 50 50 50 0 0
              

Sample Output

    
    
-1 1 2 1 2 3 4 5 -1

 

题意:一个n*n矩阵,每个格子放一个气球,气球有颜色。一个人一次可以选择一种颜色的气球,再选择一行或者一列,把该种颜色的气球踩破;

你有k次机会,看否把某种颜色的气球全部踩破。若有些颜色的气球不能被踩破,按从小大的顺序输出这些气球。否则,输出-1.

在二分图中求最少的点,让每条边都至少和其中的一个点关联,这就是二分图的“最小顶点覆盖”。

二分图的最小顶点覆盖数 =二分图的最大匹配数

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int vist[1100];
int map[1100][1100],color[1100],col[1100];
int flag[1100],flag1[1000];
int n,k,m,l;
void init()
{
    memset(vist,-1,sizeof(vist));
    memset(map,0,sizeof(map));
    memset(flag1,0,sizeof(flag1));
}
int find(int i,int ol)
{
    int j;
    for(j=1; j<=n; j++)
    {
        if(!flag[j]&&map[i][j]==ol)
        {
            flag[j]=1;
            if(vist[j]==-1||find(vist[j],ol))
            {
                vist[j]=i;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&k)&&n&&k)
    {
        init();
        m=0,l=0;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf("%d",&map[i][j]);
                if(!flag1[map[i][j]])
                {
                    color[m++]=map[i][j];//记录所有的颜色
                    flag1[map[i][j]]=1;
                }
            }
        }
        sort(color,color+m);//因为最后输出是按顺序的
        int sum;
        for(i=0; i<m; i++)//对每个颜色进行判断
        {
            sum=0;
            memset(vist,-1,sizeof(vist));
            for(j=1; j<=n; j++)
            {
                memset(flag,0,sizeof(flag));
                if(find(j,color[i]))
                    sum++;
            }
            if(sum>k)
            {
                col[l++]=color[i];
            }
        }
        if(l==0)
            printf("-1\n");
        else
        {
            for(i=0; i<l; i++)
            {
                if(i==l-1)
                    printf("%d\n",col[i]);
                else
                    printf("%d ",col[i]);
            }
        }
    }
    return 0;
}


 

<think>嗯,用户遇到了一个错误:“'ylim'值不能是无限的”。这个问题出现在他们使用R语言中的plotrix包绘制堆叠柱状图时。我需要先理解这个错误的原因,然后找出解决办法。 首先,用户提供的代码中,在创建空画布时使用了plot.window函数,并设置了xlim和ylim参数。错误提示说'ylim'的值是无限的,说明在设置ylim时,传入的值可能包含NA、NaN或者没有正确计算最大值,导致范围无法确定。 检查用户提供的代码,发现ylim的设置是c(0, max(aggregate(Value ~ years, data_long, sum)$Value))。这里可能存在两种情况:一是数据中的Value列有缺失值(NA),导致max函数返回NA;二是aggregate的结果可能为空,导致max无法计算。 接下来需要验证这些可能性。比如,如果data_long中的Value列有NA,那么aggregate的结果中对应的总和也会是NA,max就会返回NA,从而导致ylim变成c(0, NA),即上限为NA,这样ylim就无效了,进而引发错误。 另外,用户的数据转换步骤是否正确?使用pivot_longer将数据转换为长格式后,是否正确生成了Value列?如果原始数据中存在缺失值,转换后的数据也会有NA,这时候需要处理缺失值,比如用na.rm=TRUE来忽略。 解决方案可能包括:在计算max时使用na.rm=TRUE参数,确保排除缺失值;检查数据是否存在缺失,并进行适当处理;确保aggregate函数正确计算每年的总和,避免结果为NULL或空。 此外,用户的数据结构是否正确?例如,years是否为连续且存在的年份,data_long中的分组是否正确。如果数据中存在某些年份没有数据,也可能导致计算最大值时出现问题。 可能还需要确认用户的数据是否正确加载,变量名是否匹配,比如mydataexcel1是否存在,列名是否正确。如果数据没有正确加载,data_long可能为空,从而导致后续计算失败。 总结可能的解决步骤: 1. 检查数据是否存在缺失值,确保aggregate计算的总和有效。 2. 在max函数中使用na.rm=TRUE参数,避免因NA导致的问题。 3. 验证数据转换后的结构是否正确,Value列是否为数值型。 4. 确保数据中存在足够的有效数据点来计算ylim的范围。 最后,在代码中调整max函数的参数,添加na.rm=TRUE,并建议用户检查数据完整性,处理缺失值,确保所有Value都是有效数值,应该可以解决这个错误。</think>当出现`'ylim'值不能是无限的`错误时,通常是由于绘图时指定的`ylim`参数包含非数值(如`NA`或`Inf`)或取值范围计算错误。以下是逐步排查和解决方案: --- ### **错误原因分析** 1. **数据缺失值**:`data_long$Value`列存在`NA`,导致`max()`计算结果为`NA` 2. **空数据集**:`data_long`转换后没有有效数据 3. **变量名错误**:原始数据列名与代码中`pivot_longer`使用的名称不匹配 4. **数值类型错误**:`Value`列包含非数值型数据 --- ### **解决方案与优化代码** #### **步骤1:检查数据完整性** ```r # 查看数据摘要 summary(data_long) # 检查是否有NA sum(is.na(data_long$Value)) # 检查数据范围 range(data_long$Value, na.rm = TRUE) ``` #### **步骤2:修复绘图代码** ```r # 加载包 library(plotrix) library(tidyr) library(dplyr) # 生成有效高度(添加na.rm处理) max_height <- data_long %>% group_by(years) %>% summarise(Total = sum(Value, na.rm = TRUE)) %>% pull(Total) %>% max(na.rm = TRUE) # 双重保险 # 确保max_height有效 if (!is.finite(max_height)) { stop("无法计算有效高度,请检查数据完整性") } # 创建绘图窗口(修复ylim) par(mar = c(5,4,4,8), xpd = TRUE) plot.new() plot.window( xlim = c(0.5, length(unique(data$years)) + 0.5), ylim = c(0, max_height * 1.05), # 留出5%空白 xaxs = "i", yaxs = "i" ) ``` --- ### **完整修正代码** ```r # 数据准备(确保变量名正确) data <- data.frame( years = mydataexcel1$year, rice = mydataexcel1$rice, rice1 = mydataexcel1$rice1, wheat = mydataexcel1$wheat, wheat1 = mydataexcel1$wheat1 ) # 转换长格式(处理可能的NA) data_long <- data %>% pivot_longer( cols = c(rice, rice1, wheat, wheat1), names_to = "Type", values_to = "Value" ) %>% mutate(Value = as.numeric(Value)) %>% # 强制转换为数值 filter(!is.na(Value)) # 移除NA # 生成颜色渐变(同原方案) create_gradient <- function(base_col, n = 100) { ramp1 <- colorRampPalette(c(base_col, "white")) ramp2 <- colorRampPalette(c("white", base_col)) c(ramp1(n/2), ramp2(n/2)[-1]) } type_colors <- list( rice = create_gradient("#1B9E77"), rice1 = create_gradient("#D95F02"), wheat = create_gradient("#7570B3"), wheat1 = create_gradient("#E7298A") ) # 安全绘图 tryCatch({ par(mar = c(5,4,4,8), xpd = TRUE) plot.new() # 计算安全范围 valid_years <- unique(data$years) max_height <- aggregate(Value ~ years, data_long, sum)$Value %>% max(na.rm = TRUE) plot.window( xlim = c(0.5, length(valid_years) + 0.5), ylim = c(0, ifelse(is.finite(max_height), max_height, 1)), # 防崩溃 xaxs = "i", yaxs = "i" ) # 仅在有数据时绘图 if (nrow(data_long) > 0) { stackpoly( matrix(match(data$years, valid_years), ncol = 1), as.matrix(data[,-1]), col = unlist(type_colors), border = "gray30", axis4 = FALSE, stack = TRUE ) axis(1, at = seq_along(valid_years), labels = valid_years) axis(2) legend("topright", inset = c(-0.3,0), legend = names(type_colors), fill = sapply(type_colors, `[`, 1), title = "作物类型", bty = "n") title(main = "作物产量堆叠柱状图", xlab = "年份", ylab = "总产量") } else { warning("没有有效数据可供绘图") } }, error = function(e) { message("绘图失败:", e$message) }) ``` --- ### **关键改进点** 1. **数据清洗**:添加`filter(!is.na(Value))`移除缺失值 2. **类型强制转换**:`mutate(Value = as.numeric(Value))`确保数值类型 3. **安全高度计算**:双重`na.rm=TRUE`和`is.finite()`检查 4. **错误处理**:使用`tryCatch`防止完全崩溃 5. **动态范围**:`length(valid_years)`替代硬编码长度 --- ### **常见问题排查表** | 现象 | 检查方法 | 解决方案 | |------|---------|---------| | 空白图 | `nrow(data_long)`是否为0 | 检查原始数据是否存在有效数值 | | 颜色不匹配 | `names(type_colors)`是否与`cols`参数一致 | 核对列名与颜色命名 | | 坐标轴偏移 | `match(data$years, valid_years)`是否返回正确索引 | 确保years变量为连续数值 | | 渐变断裂 | `create_gradient`的`n`参数是否足够大 | 尝试增大`n=200` | 通过以上改进,可有效避免`ylim`错误,并提升代码健壮性。如果仍有问题,建议先用`View(data_long)`检查数据结构。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值