1 条题解
-
0
方法思路
- 我们需要模拟每一秒史莱姆的移动,然后统计没有史莱姆的格子数量
- 对于每个格子,我们可以确定在第i秒时,哪些初始位置的史莱姆会移动到这个格子
- 如果史莱姆向右移动(ai=1),那么第i秒它会在位置i+1
- 如果史莱姆向左移动(ai=0),那么第i秒它会在位置i-1
- 对于每一秒,我们计算每个格子是否有史莱姆,然后统计没有史莱姆的格子数量
代码实现
Java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] directions = new int[n]; for (int i = 0; i < n; i++) { directions[i] = sc.nextInt(); } int[] result = countEmptyCells(n, directions); for (int i = 0; i < n; i++) { System.out.print(result[i] + (i == n - 1 ? "" : " ")); } } public static int[] countEmptyCells(int n, int[] directions) { int[] result = new int[n]; for (int second = 1; second <= n; second++) { boolean[] hasSlime = new boolean[n + 1]; // 1-indexed // 检查每个初始位置的史莱姆在当前秒的位置 for (int initialPos = 1; initialPos <= n; initialPos++) { int currentPos; if (directions[initialPos - 1] == 0) { // 向左移动 currentPos = initialPos - second; } else { // 向右移动 currentPos = initialPos + second; } // 如果史莱姆还在地图上,标记该位置有史莱姆 if (currentPos >= 1 && currentPos <= n) { hasSlime[currentPos] = true; } } // 统计没有史莱姆的格子数量 int emptyCells = 0; for (int pos = 1; pos <= n; pos++) { if (!hasSlime[pos]) { emptyCells++; } } result[second - 1] = emptyCells; } return result; } }
Python
def count_empty_cells(n, directions): result = [] for second in range(1, n + 1): has_slime = [False] * (n + 1) # 1-indexed # 检查每个初始位置的史莱姆在当前秒的位置 for initial_pos in range(1, n + 1): if directions[initial_pos - 1] == 0: # 向左移动 current_pos = initial_pos - second else: # 向右移动 current_pos = initial_pos + second # 如果史莱姆还在地图上,标记该位置有史莱姆 if 1 <= current_pos <= n: has_slime[current_pos] = True # 统计没有史莱姆的格子数量 empty_cells = sum(1 for pos in range(1, n + 1) if not has_slime[pos]) result.append(empty_cells) return result n = int(input()) directions = list(map(int, input().split())) result = count_empty_cells(n, directions) print(" ".join(map(str, result)))
C++
#include <iostream> #include <vector> using namespace std; vector<int> countEmptyCells(int n, vector<int>& directions) { vector<int> result(n); for (int second = 1; second <= n; second++) { vector<bool> hasSlime(n + 1, false); // 1-indexed // 检查每个初始位置的史莱姆在当前秒的位置 for (int initialPos = 1; initialPos <= n; initialPos++) { int currentPos; if (directions[initialPos - 1] == 0) { // 向左移动 currentPos = initialPos - second; } else { // 向右移动 currentPos = initialPos + second; } // 如果史莱姆还在地图上,标记该位置有史莱姆 if (currentPos >= 1 && currentPos <= n) { hasSlime[currentPos] = true; } } // 统计没有史莱姆的格子数量 int emptyCells = 0; for (int pos = 1; pos <= n; pos++) { if (!hasSlime[pos]) { emptyCells++; } } result[second - 1] = emptyCells; } return result; } int main() { int n; cin >> n; vector<int> directions(n); for (int i = 0; i < n; i++) { cin >> directions[i]; } vector<int> result = countEmptyCells(n, directions); for (int i = 0; i < n; i++) { cout << result[i]; if (i < n - 1) cout << " "; } cout << endl; return 0; }
- 1
信息
- ID
- 67
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 3
- 已通过
- 1
- 上传者