墙与门

https://leetcode-cn.com/problems/walls-and-gates/

  1. typedef struct{
  2. int x;
  3. int y;
  4. } Position;
  5. Position p[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  6. void wallsAndGates(int** rooms, int roomsSize, int* roomsColSize){
  7. int total = roomsSize * (*roomsColSize);
  8. Position * queue = malloc(sizeof(Position) * total);
  9. int front = 0, rear = 0, num = 0;
  10. for(int i = 0; i < roomsSize; i++){
  11. for(int j = 0; j < *roomsColSize; j++){
  12. if(rooms[i][j] == 0){
  13. queue[rear].x = i;
  14. queue[rear].y = j;
  15. rear = (rear + 1) % total;
  16. num++;
  17. }
  18. }
  19. }
  20. while(num != 0){
  21. Position cur = queue[front];
  22. num--;
  23. front = (front + 1) % total;
  24. for(int i = 0; i < 4; i++){
  25. int x = cur.x + p[i].x;
  26. int y = cur.y + p[i].y;
  27. if(x >= 0 && x < roomsSize && y >= 0 && y < *roomsColSize && rooms[x][y] == 2147483647){
  28. rooms[x][y] = rooms[cur.x][cur.y] + 1;
  29. queue[rear].x = x;
  30. queue[rear].y = y;
  31. rear = (rear + 1) % total;
  32. num++;
  33. }
  34. }
  35. }
  36. }
  1. class Solution {
  2. Queue<int[]> q = new LinkedList<int[]>();
  3. int[][] directions = {{1, 0},{-1, 0},{0, 1},{0, -1}};
  4. int x;
  5. int y;
  6. public void wallsAndGates(int[][] rooms) {
  7. if(rooms == null)
  8. return;
  9. x = rooms.length;
  10. y = rooms[0].length;
  11. for(int i = 0; i < x; i++){
  12. for(int j = 0; j < y; j++){
  13. if(rooms[i][j] == 0){
  14. q.add(new int[]{i, j});
  15. }
  16. }
  17. }
  18. bfs(rooms);
  19. }
  20. public void bfs(int[][] rooms, int){
  21. int nextX;
  22. int nextY;
  23. while(!q.isEmpty()){
  24. int[] cur = q.poll();
  25. for(int[] direction: directions){
  26. nextX = cur[0] + direction[0];
  27. nextY = cur[1] + direction[1];
  28. if(nextX >= x || nextX < 0 || nextY < 0 || nextY >= y || rooms[nextX][nextY] != 2147483647)
  29. continue;
  30. rooms[nextX][nextY] = rooms[cur[0]][cur[1]] + 1;
  31. q.add(new int[]{nextX, nextY});
  32. }
  33. }
  34. }
  35. }

岛屿数量

https://leetcode-cn.com/problems/number-of-islands/

  1. typedef struct{
  2. int x;
  3. int y;
  4. } Position;
  5. Position p[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  6. int numIslands(char** grid, int gridSize, int* gridColSize){
  7. int total = gridSize * (*gridColSize);
  8. Position * queue = (Position *)malloc(sizeof(Position) * total);
  9. int rear = 0, front = 0, num = 0, count = 0;
  10. for(int i = 0; i < gridSize; i++){
  11. for(int j = 0 ; j < *gridColSize; j++){
  12. if(grid[i][j] == '1'){
  13. grid[i][j] = '0';
  14. count++;
  15. queue[rear].x = i;
  16. queue[rear].y = j;
  17. num++;
  18. rear = (rear + 1) % total;
  19. }
  20. //遍历每一座岛
  21. while(num != 0){
  22. Position cur = queue[front];
  23. num--;
  24. front = (front + 1) % total;
  25. for(int m = 0; m < 4; m++){
  26. int x = p[m].x + cur.x;
  27. int y = p[m].y + cur.y;
  28. if(x >= 0 && x < gridSize && y >= 0 && y < *gridColSize && grid[x][y] == '1'){
  29. grid[x][y] = '0';
  30. queue[rear].x = x;
  31. queue[rear].y = y;
  32. num++;
  33. rear = (rear + 1) % total;
  34. }
  35. }
  36. }
  37. }
  38. }
  39. return count;
  40. }

打开转盘锁

https://leetcode-cn.com/problems/open-the-lock/