编程之美格格取数(为什么一直是WA,求测试)

描述

给你一个m x n (1 <= m, n <= 100)的矩阵A (0<=aij<=10000),要求在矩阵中选择一些数,要求每一行,每一列都至少选到了一个数,使得选出的数的和尽量的小。


输入

多组测试数据。首先是数据组数T

对于每组测试数据,第1行是两个正整数m, n,分别表示矩阵的行数和列数。

接下来的m行,每行n个整数,之间用一个空格分隔,表示矩阵A的元素。


输出

每组数据输出一行,表示选出的数的和的最小值。


数据范围

小数据:1 <= m, n <= 5

大数据:1 <= m, n <= 100



样例输入
2
3 3
1 2 3
3 1 2
2 3 1
5 5
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
样例输出
Case 1: 3
Case 2: 5

我的代码:

#include<iostream>

#include<vector>
using namespace std;
struct Node{
vector<int> rows;
int line;
int different_rows;
int sum;
Node() :line(-1), different_rows(0), sum(0){}
Node(int l, int d, int s) :line(l), different_rows(d), sum(s){ }
};
int main()
{
int T;
cin >> T;
vector<int **> store;
vector<pair<int, int> > index;
while (T > 0)
{
int m,n;
cin >> m >> n;
if (m <= 0 || n <= 0)
{
return 0;
}
if(m>=n)index.push_back(pair<int, int>(m, n));
else index.push_back(pair<int, int>(n, m));
int **temp=new int*[m];
for (int i = 0; i < m; i++)
{
*(temp + i) = new int[n];
int *row = new int[n];
for (int j = 0; j < n; j++)
cin >> *(row + j);
*(temp + i) = row;


}
if(m>=n)store.push_back(temp);
else
{
int **temp1 = new int*[n];
for (int i = 0; i < n; i++)
{
*(temp1 + i) = new int[m];
int *row = new int[m];
for (int j = 0; j < m; j++)
*(row + j) = temp[j][i];
*(temp1 + i) = row;
}
store.push_back(temp1);
}
T--;
}



vector<int> result;
vector<int **>::iterator iter = store.begin();
vector<pair<int, int> >::iterator iter_index = index.begin();

while (iter != store.end())
{

vector<Node> stack;
int min;
bool first_sum = true;
int arrayLine = (*iter_index).first;
int arrayRow = (*iter_index).second;
for (int i = 0; i < arrayRow; i++)
{
Node tempNode(0,0,(*iter)[0][i]);
tempNode.rows.push_back(i);
stack.push_back(tempNode);
}

while (!stack.empty())
{
Node temp = *(--stack.end());
stack.pop_back();
if (temp.line == arrayLine - 1 && temp.different_rows == arrayRow - 1)
{

vector<int>::iterator temp_iter = temp.rows.begin();

if (first_sum)
{

min = temp.sum;
cout << min << endl;
first_sum = false;
}
else if (temp.sum < min)min = temp.sum;
}
else if (arrayLine - 1 - temp.line < arrayRow - 1 - temp.different_rows)
continue;
else
{

for (int i = 0; i < arrayRow; i++)
{
bool has = false;
vector<int> tempV;
vector<int>::iterator temp_iter = temp.rows.begin();
while (temp_iter != temp.rows.end())
{

if (*temp_iter == i)
{
has = true;

}
tempV.push_back(*temp_iter);
temp_iter++;
}

int k;
if (!has) k = temp.different_rows + 1;
else k = temp.different_rows;


tempV.push_back(i);

int sum = temp.sum + (*iter)[temp.line + 1][i];


Node tempNode(temp.line + 1, k, sum);
vector<int>::iterator iter0 = tempV.begin();
while (iter0 != tempV.end())
{
tempNode.rows.push_back(*iter0);
iter0++;
}
stack.push_back(tempNode);
}
}
}
result.push_back(min);
iter++;
iter_index++;

}


vector<int>::iterator iter_result = result.begin();
int  num = 1;
while (iter_result != result.end())
{
cout<<"Case "<<num++<<": " << *iter_result << endl;
iter_result++;
}
return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值