“俄罗斯套娃问题”程序(能够处理稀疏矩阵)

这是一个关于俄罗斯套娃奖品问题的程序实现,使用C++编写,通过Matrix类处理稀疏矩阵。程序包括了矩阵的初始化、寻找最大收益路径、输出路径等关键功能,支持对文件中的矩阵数据进行读取和处理。
摘要由CSDN通过智能技术生成

//=========================================================================

// File Name: Matrix.h
// Desp       :  俄罗斯套娃奖品问题,Matrix类
// Author     : 905A组

// Last Update: 2010-05-27 22:35

//=========================================================================

#include <list>
#include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;

struct eInMat
{
 int x;
 int y;
 int operator == (const eInMat& right) const
 {
  return x==right.x ? (y==right.y ? 1:0) :0;
 }
};

class Matrix
{
public:
 Matrix(int countRow,int countCol)
 {
  nRows=countRow;
  nCols=countCol;
  int size=countRow*countCol*sizeof(int);
  elements=(int*)malloc(size);
  padVal=(int*)malloc(size);
  memset(padVal,-2,size);
 };
 ~Matrix()
 {
  free(elements);
  free(padVal);
 };

 int seekMaxGain(eInMat curE,list<eInMat> &vPath);
 void outPutPath(list<eInMat> &vPath);
 
 inline int& element(int x)
 {
  return *(elements+x); 
 }
 
 inline int& element(eInMat e)
 {
  return *(elements+nCols*e.x+e.y);
 }
 
private:
 int nRows;
 int nCols;
 int* elements;               //用来记录矩阵元素
 int* padVal;                 //用来记录矩阵元素为0时的填充值
 int validEMat(eInMat curE);   //判断当前元素是否合法,即:是否超出矩阵的边界
 int seekMat(eInMat curE,list<eInMat> &vPath);

 //判断下一个要遍历的元素是否已在路径vPath中<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值