1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| class Solution { public: typedef struct pos_count { int m_x; int m_y; int m_rem_count; pos_count(int x, int y, int rem_count) : m_x(x), m_y(y), m_rem_count(rem_count) {} } pos_count_t;
int edgeCount(int m, int n, int x, int y) { int tmp = 0; tmp += x == 0 ? 1 : 0; tmp += x == m - 1 ? 1 : 0; tmp += y == 0 ? 1 : 0; tmp += y == n - 1 ? 1 : 0; return tmp; }
int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { if (maxMove <= 0) { return 0; }
if (m == 1 && n == 1) { return 4; } queue<pos_count_t> Q; long long ans = 0; long long mod = 1e9 + 7; int xf[] = {0, 0, 1, -1}; int yf[] = {1, -1, 0, 0}; int fi, se, remMove; Q.push({startRow, startColumn, maxMove}); while (!Q.empty()) { pos_count_t tmp = Q.front(); Q.pop(); fi = tmp.m_x; se = tmp.m_y; remMove = tmp.m_rem_count - 1; ans = (ans + edgeCount(m, n, fi, se)) % mod;
if (remMove == 0) { continue; } for (int i = 0; i < 4; i++) { int x = fi + xf[i]; int y = se + yf[i]; if (x < 0 || x > m - 1 || y < 0 || y > n - 1) { continue; } Q.push({x, y, remMove}); } }
return ans; } };
|