把生活写成首诗 2020-06-04 00:28 采纳率: 83.3%
浏览 89
已采纳

c# 匈牙利算法数组越界问题

                        static int M, N;            //M, N分别表示左、右侧集合的元素数量
            static int[][] Map = new int[100][]; //邻接矩阵存图
            static int[] p = new int[100];         //记录当前右侧元素所对应的左侧元素
            static bool[] vis = new bool[100];      //记录右侧元素是否已被访问过

            static bool match(int i)
            {
                for (int j = 1; j <= N; ++j)
                    if (Map[i][j] != 0 && !vis[j]) //有边且未访问
                    {
                        vis[j] = true;                 //记录状态为访问过
                        if (p[j] == 0 || match(p[j])) //如果暂无匹配,或者原来匹配的左侧元素可以找到新的匹配
                        {
                            p[j] = i;    //当前左侧元素成为当前右侧元素的新匹配
                            return true; //返回匹配成功
                        }
                    }
                return false; //循环结束,仍未找到匹配,返回匹配失败
            }

            static void Hungarian()
            {
                int cnt = 0;
                for (int i = 1; i <= M; ++i)
                {
                    vis = vis.Select(x => false).ToArray();
                    if (match(i))
                        cnt++;
                }

            }

         public void ds()
         {
             Map = Map.Select(x => new int[100]).ToArray();

             SqlConnection sqlconn = new SqlConnection();
             sqlconn.ConnectionString = "server=.;database=DCS;uid=sa;pwd=111";
             sqlconn.Open();
             string cmdtxt1 = "select * from volunteer";
             string cmdtxt2 = "select * from tvolunteer";
             SqlDataAdapter dap1 = new SqlDataAdapter(cmdtxt1, sqlconn);
             SqlDataAdapter dap2 = new SqlDataAdapter(cmdtxt2, sqlconn);
             DataSet dataset1 = new DataSet();
             DataSet dataset2 = new DataSet();
             dap1.Fill(dataset1);
             dap2.Fill(dataset2); 
             string t1 = "select count(*) from volunteer";
             string t2 = "select count(*) from tvolunteer";
             int x1 = (int)SqlDBHelper.ExecuteScalar(t1);
             int x2 = (int)SqlDBHelper.ExecuteScalar(t2);
             #endregion

             int[,] leader = new int[x1, 3]; //x1个一维数组 ,每个一维数组有3个元素
             for(int i = 0;i<dataset1.Tables[0].Rows.Count;i++) 
             {
                // int[] a = new int[3];

                 for (int x = 1; x < 4;x++ )
                 {
                     //a[x-1] = Convert.ToInt32(dataset.Tables[0].Rows[i][x]); 
                     leader[i, x - 1] = Convert.ToInt32(dataset1.Tables[0].Rows[i][x]); { }
                 }

             }

             int[,] teacher = new int[x2, 2]; //x2个一维数组 ,每个一位数组有3个元素
             for(int i = 0;i<dataset2.Tables[0].Rows.Count;i++) 
             {
                // int[] a = new int[3];                 
                 for (int x = 1; x < 3;x++ ) 
                 {                    
                     //a[x-1] = Convert.ToInt32(dataset.Tables[0].Rows[i][x]); 
                     teacher[i, x - 1] = Convert.ToInt32(dataset2.Tables[0].Rows[i][x]); 
                 }

             }

             M = leader.GetLength(0);
             N = teacher.GetLength(0);

             for (int i = 0; i <leader.GetLength(0); i++) //行数
                 for (int j = 0; j <leader.GetLength(1); j++) //列数
                 {
                     Map[i+1][leader[i, j]]++;
                 }
             for (int i = 0; i < teacher.GetLength(0); i++)
                 for (int j = 0; j <teacher.GetLength(1); j++)
                 {
                     Map[teacher[i, j]][i+1]++;
                 }
             Map = Map.Select(x => x.Select(y => y == 2 ? 1 : 0).ToArray()).ToArray();
             Hungarian();

             for (int r = 0; r < M; r++) 
             {
                 int i = p[r];

}

图片说明

图片说明

算法代码是某大佬写的,我改了输入数据后出现了越界问题,输入数据的值在截图中,自己水平不够找不出是哪里的问题,求个大佬解惑

  • 写回答

2条回答 默认 最新

  • gentelwind 2020-06-15 13:08
    关注

    teacher变量中,teacher[0,0]=729,teacher[0,1]=777
    那么你代码运行到Map[teacher[i, j]][i + 1]++;这一行时,表达式为
    Map[teacher[0, 0]][0 + 1]++; -> Map[729][1]
    而你Map定义为:static int[][] Map = new int[100][];即100个元素的数据,数组索引值越界时必然的了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e