这道题我太傻逼了,我入队的时候,++ hh 写成 hh ++ 了。。
原题链接:https://www.acwing.com/problem/content/description/22/
class Solution {
public:
int get_sum(int x, int y)
{
int sum = 0;
while(x)
{
sum += x % 10;
x /= 10;
}
while(y)
{
sum += y % 10;
y /= 10;
}
return sum;
}
int movingCount(int threshold, int rows, int cols)
{
if (!rows || !cols) return 0;
int sum = 0;
bool st[rows][cols];
memset(st, 0, sizeof st);
pair<int, int> q[2510];
int hh = 0;
q[hh] = {0, 0};
st[0][0] = true;
sum ++;
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
// int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while(hh != -1)
{
int tx = q[hh].first, ty = q[hh].second;
hh --;
// printf("出栈的是%d, %d\n", tx, ty);
for(int i = 0; i < 4; i ++)
{
int x = tx + dx[i], y = ty + dy[i];
if(x >= 0 && x < rows && y >= 0 &&
y < cols && get_sum(x, y) <= threshold && !st[x][y])
{
st[x][y] = true;
// 我是真的傻逼,这里++hh写成hh++了。
q[++hh] = {x, y};
// printf("%d %d\n", x, y);
sum ++;
}
}
}
// cout << get_sum(14, 10) << endl;
return sum;
}
};
from collections import deque
class Solution(object):
def movingCount(self, threshold, rows, cols):
"""
:type threshold: int
:type rows: int
:type cols: int
:rtype: int
"""
queue = deque()
queue.append((0, 0))
st = [[False] * cols for _ in range(rows)]
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
res = 0
while queue:
x, y = queue.popleft()
if x >= 0 and x < rows and y >= 0 \
and y < cols and not st[x][y] and \
self.getSum(x, y) <= threshold:
res += 1
st[x][y] = True
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
queue.append((nx, ny))
return res
def getSum(self, x, y):
sum = 0
while x:
sum += x % 10
x //= 10
while y:
sum += y % 10
y //= 10
return sum