2 条题解

  • 0
    @ 2025-4-18 17:48:58

    一道连通块型dfs,精髓在于0,0一定是不是内环,所以在0,0 dfs,剩余被包住的就涂色

    #include <bits/stdc++.h>
    using namespace std;
    int a[50][50];
    bool vis[50][50];
    int n;
    int fx[4] = {0,0,1,-1};
    int fy[4] = {1,-1,0,0};
    void dfs(int x,int y)
    {
    	vis[x][y] = true;
    	int tx,ty;
    	for(int i=0; i<4; i++)
    	{
    		tx = x + fx[i];
    		ty = y + fy[i];
    		if(tx>=0&&tx<=n+1&&ty>=0&&ty<=n+1
    		&&vis[tx][ty]==false&&a[tx][ty]==0)
    		{
    			dfs(tx,ty);
    		}
    	}
    } 
    int main()
    {
    	cin>>n;
    	for(int i=1; i<=n; i++)
    	{
    		for(int j=1; j<=n; j++)
    		{
    			cin>>a[i][j];
    		}
    	}
    	dfs(0,0);
    	for(int i=1; i<=n; i++)
    	{
    		for(int j=1; j<=n; j++)
    		{
    			if(vis[i][j]==true||a[i][j]==1)
    			{
    				cout<<a[i][j];
    			}
    			else
    			{
    				cout<<"2";
    			}
    			cout<<" ";
    		}
    		cout<<endl;
    	}
    	
    	return 0;
     }
    
    • 0
      @ 2025-3-7 16:51:19
      #include <iostream>
      #include <vector>
      #include <queue>
      
      using namespace std;
      
      // 定义方向数组,表示上下左右四个方向
      const int dx[] = {-1, 1, 0, 0};
      const int dy[] = {0, 0, -1, 1};
      
      void bfs(int x, int y, vector<vector<int>>& grid, int n) {
          queue<pair<int, int>> q;
          q.push({x, y});
          grid[x][y] = -1; // 标记为与边界相连的 0
      
          while (!q.empty()) {
              auto [cx, cy] = q.front();
              q.pop();
      
              // 遍历四个方向
              for (int i = 0; i < 4; ++i) {
                  int nx = cx + dx[i];
                  int ny = cy + dy[i];
      
                  // 检查是否在方阵范围内且为 0
                  if (nx >= 0 && nx < n && ny >= 0 && ny < n && grid[nx][ny] == 0) {
                      grid[nx][ny] = -1; // 标记为与边界相连的 0
                      q.push({nx, ny});
                  }
              }
          }
      }
      
      int main() {
          int n;
          cin >> n;
          vector<vector<int>> grid(n, vector<int>(n));
      
          // 输入方阵
          for (int i = 0; i < n; ++i) {
              for (int j = 0; j < n; ++j) {
                  cin >> grid[i][j];
              }
          }
      
          // 遍历边界,找到所有边界上的 0,并标记为 -1
          for (int i = 0; i < n; ++i) {
              if (grid[i][0] == 0) bfs(i, 0, grid, n); // 左边界
              if (grid[i][n - 1] == 0) bfs(i, n - 1, grid, n); // 右边界
          }
          for (int j = 0; j < n; ++j) {
              if (grid[0][j] == 0) bfs(0, j, grid, n); // 上边界
              if (grid[n - 1][j] == 0) bfs(n - 1, j, grid, n); // 下边界
          }
      
          // 遍历整个方阵,填充被 1 包围的 0 为 2
          for (int i = 0; i < n; ++i) {
              for (int j = 0; j < n; ++j) {
                  if (grid[i][j] == 0) {
                      grid[i][j] = 2; // 填充为 2
                  } else if (grid[i][j] == -1) {
                      grid[i][j] = 0; // 恢复为 0
                  }
              }
          }
      
          // 输出结果
          for (int i = 0; i < n; ++i) {
              for (int j = 0; j < n; ++j) {
                  cout << grid[i][j] << " ";
              }
              cout << endl;
          }
      
          return 0;
      }
      
      
      • 1

      信息

      ID
      5220
      时间
      1000ms
      内存
      125MiB
      难度
      2
      标签
      递交数
      54
      已通过
      29
      上传者