代码提交前的测试模板
准备工作:
创建 input.txt 放测试数据
创建 expect.txt 放预期结果
例题:https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix/
input.txt
3 3
9 9 4
6 6 8
2 1 1
expect.txt
4
编译命令
g++ main.cpp -std=c++17 -Wall -DTEST
输出结果
几个常见问题
1、 怎么改成 ide 调试
* 将// #define TEST的注释去掉
* 将 dir 改成绝对路径
2、输入输出的时候
用户 out 代替 cout, in 代替 cin
3、writeToFile 函数使用
* 作用是将输出写入到 output.txt 文件
* 支持内置对象 写单行
* 支持stl 容器, 每个容器的内容会写入单行并用单个空格隔间最后换行,比如
1. vector<int> dd{1, 2, 3}
写入的格式为:1 2 3\n
2. vector<vector<int>> dd{{1, 2, 3}, {4, 5, 6},{7, 8, 9}}
写入格式的为:1 2 3 4 5 6 7 8 9
#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <fstream>
using namespace std;
// 直接调试去掉下面的注释, 然后把 dir改成绝对路径
// #define TEST
#ifdef TEST
// 修改这里可以改成绝对路径用 ide 直接调试
string dir = "./";
string inputFile = dir + "input.txt";
string outputFile = dir + "output.txt";
string expectFile = dir + "expect.txt";
ifstream in(inputFile);
ofstream out(outputFile);
ifstream output(outputFile);
ifstream expect(expectFile);
string emptyStr = "nullptr";
template<typename T, typename ...Args>
auto has_begin(int) -> decltype(declval<T>().begin(declval<Args>...), true_type{});
template<typename T, typename ...Args>
false_type has_begin(...);
template<typename T, typename ...Args>
using beginable = typename std::is_same<decltype(has_begin<T>(0)), true_type>::type;
// 用于实现了 operator<<的对象
template<typename Obj, typename = typename enable_if<
!is_same<
true_type,
beginable<
typename decay<Obj>::type
>
>::value
>::type
>
auto writeToFile(Obj &&r, bool end = true) -> decltype(void(out.operator<<(r))) {
out << r << " ";
if (end) {
out << endl;
}
}
// 容器都有 begin 方法
template<typename Container>
auto writeToFile(Container &&c, bool end = true) -> decltype(void(c.begin())) {
for (auto&& v : c) {
writeToFile(v, false);
}
if (end) {
out << endl;
}
}
// 用于数值退化成指针的情况
template<typename Arr, size_t N>
void writeToFile(Arr &&r, int n) {
for (int i = 0; i < n; ++i) {
out << r[n] << " ";
}
out << endl;
}
#else
istream &in = cin;
ostream &out = cout;
// 加速
ios::sync_with_stdio(false);
in.tie(0);
out.tie(0);
#endif
class Prepare {
public:
Prepare() {
prepare();
}
static void prepare() {
#ifdef TEST
if (!in.is_open()) {
cout << "empty input file" << endl;
exit(-1);
}
if (!expect.is_open()) {
cout << "empty expect file" << endl;
exit(-1);
}
#endif
}
};
class Check {
public:
~Check() {
// compare
#ifdef TEST
for (int i = 1; !output.eof(); ++i) {
if (output.eof()) {
break;
}
string l, r;
getline(output, l);
getline(expect, r);
while(!l.empty() && l.back() == ' ') {
l.pop_back();
}
while(!r.empty() && r.back() == ' ') {
r.pop_back();
}
if (l.empty() && r.empty()) {
break;
}
if (l == emptyStr) {
break;
}
if (l == r) {
cout << "case: " << i << " pass" << endl;
} else if (l != r) {
cout << "case: " << i << "wrong " << "output[" << l << "], while expect[" << r << "]" << endl;
continue;
}
}
#endif
}
};
static Prepare pre;
static Check check;
/// ===============提交的业务代码=============================
/*
* @lc app=leetcode.cn id=329 lang=cpp
*
* [329] 矩阵中的最长递增路径
*/
// @lc code=start
class Solution {
public:
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
int dfs(int x, int y, vector<vector<int>>& dp, const vector<vector<int>>& matrix) {
if (dp[x][y]) {
return dp[x][y];
}
int res = 1;
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || nx >= dp.size() || ny < 0 || ny >= dp[nx].size() || matrix[x][y] <= matrix[nx][ny]) {
continue;
}
int now = dfs(nx, ny, dp, matrix);
res = max(res, now + 1);
}
return dp[x][y] = res;
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
vector<vector<int>> dp(matrix.size(), vector<int>(matrix[0].size()));
int res = 0;
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[i].size(); ++j) {
if (!dp[i][j]) {
res = max(res, dfs(i, j, dp, matrix));
}
}
}
return res;
}
};
// @lc code=end
#ifdef TEST
/// ===============测试代码=============================
int main() {
// 自定义读取输入 规定输入边界
int m, n;
while (in >> m >> n) {
vector<vector<int>> matrix(m, vector<int>(n));
for(int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
in >> matrix[i][j];
}
}
//
auto ans = Solution{}.longestIncreasingPath(matrix);
// 输出结果到 out
// 如
writeToFile(ans);
}
}
#endif