Skip to content

Commit

Permalink
feat: update lc-1391
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Sep 7, 2023
1 parent 3daede5 commit 1f59327
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions algorithm/array/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ list(APPEND leetcode_order 944 977 985 986 989)
list(APPEND leetcode_order 999 1010 1013 1030 1051)
list(APPEND leetcode_order 1089 1108 1170 1184 1200)
list(APPEND leetcode_order 1217 1329 1360 1365 1371)
list(APPEND leetcode_order 1391)
LIST(TRANSFORM leetcode_order PREPEND leetcode_)

set(dependencies ${dependencies} ${leetcode_order})
Expand Down
156 changes: 156 additions & 0 deletions algorithm/array/leetcode_1391.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2023 nanoseeds
*/
#include "leetcode_1391_test.hpp"

namespace leetcode_1371 {

enum direction {
left_to_right = 1,
up_to_down = 2,
left_to_down = 3,
down_to_right = 4,
left_to_up = 5,
up_to_right = 6,
};

// 他得有一个额外的变量记录方向, 从而决定下一步的方向.
bool leetcode_1371::hasValidPath(vector<vector<int32_t>> &grid) {
const auto row = grid.size();
if (row == 0) {
return true;
}
const auto col = grid[0].size();
if (col == 0) {
return true;
}
if (grid[0][0] == direction::left_to_up) {
return false;
} else if (grid[0][0] == direction::down_to_right) {
grid[0][0] = direction::left_to_right;
const auto v1 = hasValidPath(grid);
if (v1) {
return true;
}
grid[0][0] = direction::up_to_down;
return hasValidPath(grid);
}
size_t i{0}, j{0}, direction{0};//0 to right, 1 to down, 2 to left,3 to up
switch (grid[0][0]) {
case direction::left_to_right: {
direction = 0;
break;
}
case direction::up_to_down: {
direction = 1;
break;
}
case direction::left_to_down: {
direction = 1;
break;
}
case direction::up_to_right: {
direction = 0;
break;
}
}
vector<vector<uint8_t>> vec(row, vector<uint8_t>(col, 0));
while (i < row && j < col) {
if (i == row - 1 && j == col - 1) {
return true;
}
if (vec[i][j] == 1) {
return false;
}
vec[i][j] = 1;
switch (direction) {
case 0: {
if (j + 1 < col) {
if (grid[i][j + 1] == direction::left_to_right) {
j += 1;
continue;
} else if (grid[i][j + 1] == direction::left_to_down) {
j += 1;
direction = 1;
continue;
} else if (grid[i][j + 1] == direction::left_to_up) {
j += 1;
direction = 3;
continue;
} else {
return false;
}
} else {
return false;
}
}
case 1: {
if (i + 1 < row) {
if (grid[i + 1][j] == direction::up_to_down) {
i += 1;
continue;
} else if (grid[i + 1][j] == direction::left_to_up) {
i += 1;
direction = 2;
continue;
} else if (grid[i + 1][j] == direction::up_to_right) {
i += 1;
direction = 0;
continue;
} else {
return false;
}
} else {
return false;
}
}
case 2: {
if (j > 0) {
if (grid[i][j - 1] == direction::left_to_right) {
j -= 1;
continue;
} else if (grid[i][j - 1] == direction::down_to_right) {
j -= 1;
direction = 1;
continue;
} else if (grid[i][j - 1] == direction::up_to_right) {
j -= 1;
direction = 3;
continue;
} else {
return false;
}
} else {
return false;
}
}
case 3: {
if (i > 0) {
if (grid[i - 1][j] == direction::up_to_down) {
i -= 1;
continue;
} else if (grid[i - 1][j] == direction::left_to_down) {
i -= 1;
direction = 2;
continue;
} else if (grid[i - 1][j] == direction::down_to_right) {
i -= 1;
direction = 0;
continue;
} else {
return false;
}
} else {
return false;
}
}
}
}
return false;
}

}
35 changes: 35 additions & 0 deletions algorithm/array/leetcode_1391_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2023 nanoseeds
*/
//@Tag array
//@Tag 数组
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1391_TEST_HPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1391_TEST_HPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <vector>
#include <string>

namespace leetcode_1371 {
using std::vector;

namespace leetcode_1371 {
bool hasValidPath(vector<vector<int32_t>> &grid);
}

TEST_CASE("test case 1-1 {test_1371}", "{test_1371}") {
vector<vector<int32_t>> input{
{2, 4, 3},
{6, 5, 2},
};
CHECK(leetcode_1371::hasValidPath(input));
}

}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1391_TEST_HPP

0 comments on commit 1f59327

Please sign in to comment.