classSolution { public: voiddfs(vector<vector<int>>& grid, int i, int j){ if (i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size() || grid[i][j] != 1) return; grid[i][j] = 2; dfs(grid, i - 1, j); dfs(grid, i + 1, j); dfs(grid, i, j - 1); dfs(grid, i, j + 1); }
vector<vector<int>> floodIslands(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i * j == 0 || i == m - 1 || j == n - 1 || grid[i][j] == 0) dfs(grid, i, j); } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { grid[i][j] = grid[i][j] == 2 ? 1 : 0; } } return grid; } };
intmain(){ int T; cin >> T; while (T--) { int M, N; cin >> M >> N; vector<vector<int>> grid(M, vector<int>(N)); for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { cin >> grid[i][j]; } } Solution solution; grid = solution.floodIslands(grid); for (constauto& row : grid) { for (int val : row) { cout << val; } cout << endl; } } return0; }
classSolution { public: boolcanJump(vector<int>& nums){ int n = nums.size(); int rightmost = 0; for (int i = 0; i < n; ++i) { if (i <= rightmost) { rightmost = max(rightmost, i + nums[i]); if (rightmost >= n - 1) { returntrue; } } } returnfalse; } };
intmain(){ int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; ++i) { cin >> nums[i]; } Solution solution; cout << (solution.canJump(nums) ? "TRUE" : "FALSE") << endl; return0; }