3 条题解

  • 2
    @ 2023-8-13 16:31:47

    #include <bits/stdc++.h> using namespace std; int w = 1,h = 1,a,b,sun = 0; char z[25][25];

    void afd(int x,int y){ sun++; z[x][y] = '/';

    if(z[x+1][y] == '.'){
    	afd(x+1,y);
    }
    if(z[x-1][y] == '.'){
    	afd(x-1,y);
    }
    if(z[x][y+1] == '.'){
    	afd(x,y+1);	
    }
    if(z[x][y-1] == '.'){
    	afd(x,y-1);
    
    }
    

    }

    int main(){ while(cin >> h >> w&&(w!=0||h!=0)){ sun = 0; for(int i = 1;i<=20;i++){ for(int j = 1;j<=20;j++){ z[i][j] = ' '; } } for(int i = 1;i<=w;i++){ for(int j = 1;j<=h;j++){ cin >> z[i][j]; if(z[i][j] == '@'){ a = i; b = j;

    			}
    
    		}
    	}
    	afd(a,b);	
    	cout << sun << endl;	
    }
    

    }

    • 1
      @ 2023-8-13 17:02:33

      这道题,非常困难,写不出来👍


      #include <iostream>
      #include <cstring>
      using namespace std;
      char mp[21][21];
      int n,m,x,y,ans = 1,vis[21][21];//vis数组记录,以防重复搜索
      int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0};
      //四个方向
      bool check(int a,int b)
      {
         if(a > 0 && a <= m && b > 0 && b <= n) return true;
         return false;
      }
      //判断是否越界
      void dfs(int a,int b)
      {
         for(int i = 0;i < 4;i ++)//向四个方向进攻!
         {
            int xx = a + dx[i],yy = b + dy[i];
            if(check(xx,yy) && vis[xx][yy] == 0 && mp[xx][yy] == '.')
            {
               ans ++;
               vis[xx][yy] = 1;
               dfs(xx,yy);
            }
         }
      }
      int main()
      {
         ios::sync_with_stdio(0);
         cin.tie(0),cout.tie(0);
         //无法编译
         cin >> n >> m;
         do
         {
            ans = 1;
            memset(vis,0,sizeof(vis));
            memset(mp,0,sizeof(mp));
            for(int i = 1;i <= m;i ++)
            {
               for(int j = 1;j <= n;j ++)
               {
                  cin >> mp[i][j];
                  if(mp[i][j] == '@') 
                  {
                     vis[i][j] = 1;
                     x = i,y = j;
                  }
                  //记录起点
               }
            }
            dfs(x,y);
            cout << ans << "\n";
            cin >> n >> m;
         }while(n && m);
      }
      
      • 1
        @ 2023-8-13 16:56:36

        66666666666666666666666666666666666666666666666666666666

        • 1

        信息

        ID
        216
        时间
        1000ms
        内存
        256MiB
        难度
        8
        标签
        递交数
        62
        已通过
        10
        上传者