5 条题解

  • 3
    @ 2024-12-1 21:32:50
    #include <bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int N = 20 + 10;
    const int INF = 0x3f3f3f3f;
    char a [N] [N];
    int m , n , k , q , w;
    int xx [4] = {-1 , 1 , 0 , 0};
    int yy [4] = {0 , 0 , 1 , -1};
    bool f (int c , int d)
    {
    	if (a [c] [d] == '.' && c >= 1 && c <= n && d >= 1 && d <= m)
    	{
    		return 1;
    	}
    	return 0;
    }
    void dfs (int x , int y)
    {
    	/*if (a [x] [y] == "#")
    	    return;*/
    	a [x] [y] = '#';
    	for (int i = 0; i < 4; i++)
    	{
    		int nx = x + xx [i];
    		int ny = y + yy [i];
    		if (f (nx , ny)) 
    		{
    			k++;
    		    dfs (nx , ny);
    		}
    	}
    }
    int main()
    {
    	    while (cin >> m >> n)
    	    {
    	    	if (m == 0 && n == 0)
    	    	{
    	    		break;
    			}
    			for (int i = 1; i <= n; i++)
    	        {
    	    	    for (int j = 1; j <= m; j++)
    			    {
    				    cin >> a [i] [j];
    				    if (a [i] [j] == '@')    
    			        {
    					    q = i;
    				        w = j;
    				    }
    			    } 
    		    }
    		    k = 1;
                dfs (q , w);
    		    cout << k << endl;
    		}
    	    return 0;
    }
    
    • 1
      @ 2025-3-17 13:25:16
      #include<iostream>
      //#include<iomanip>
      //#include<math.h>
      using namespace std;
      
      // var
      int w,h,ax,ay,cnt = 1;
      int dx[] = {0,-1,0,1};
      int dy[] = {-1,0,1,0};
      char a[55][55];
      
      // func
      bool check(int x,int y){
          return ( x <= h && x >= 1 && y <= w && y >= 1 && a[x][y] == '.' );
      }
      
      void dfs(int x,int y){
          // locked
          a[x][y] = '#';
      
          // diffusion
          for(int i=0;i<=3;i++){
              if(check(x + dx[i],y + dy[i])){
                  cnt ++;
                  dfs(x + dx[i],y + dy[i]);
              }
          }
      }
      
      int main(){
          // in 
          while(cin >> w >> h){
              // break_cond
              if(w == 0 && h == 0){
                  break;
              }
      
              // input_array
              for(int i=1;i<=h;i++){
                  for(int j=1;j<=w;j++){
                      cin >> a[i][j];
      
                      if(a[i][j] == '@') ax = i , ay = j; // ???
                  }
              }
      
              // call_func
              dfs(ax,ay);
      
              // out
              cout << cnt << endl;
              cnt = 1;
          }
          return 0;
      }
      
      • 0
        @ 2025-3-22 16:07:56

        #include<bits/stdc++.h>//导入输出工具库 using namespace std;//规定一个范围空间 叫std

        int n , m , d , e , cnt; char a[1005][1005]; bool vis[1005][1005]; struct node{ int x, y; };

        bool chicken(int x , int y){ if(x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] == '.') return 1; else return 0; } node dxy[4] = {{1 , 0} , {0 , 1} , {-1 , 0} , {0 , -1}};

        int bfs(int x , int y){; queue q; node t1; t1.x = x; t1.y = y; vis[t1.x ][t1.y ] = '#'; q.push(t1); cnt++; while(!q.empty()){ node t2 = q.front(); q.pop(); for(int i = 0;i < 4;i++){ node t3; t3.x = t2.x + dxy[i].x ; t3.y = t2.y + dxy[i].y ; if(chicken(t3.x , t3.y )){ a[t3.x ][t3.y ] = '#'; q.push(t3); cnt++; } }

        }
        return cnt;
        

        } int main()//主程序的入口 { cin >> m >> n; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ cin >> a[i][j]; if(a[i][j] == '@'){ d = i; e = j; } } } cout << bfs(d , e); return 0; }

        • 0
          @ 2025-3-16 18:09:31
          #include<bits/stdc++.h>
          using namespace std;
          
          const int N=1e4+10;
          char n[N][N];
          int w,h,cnt,ans,a,b,k=1;
          int xx[4]={-1,1,0,0};
          int yy[4]={0,0,1,-1};
          bool f(int x,int y){
          	if(n[x][y]=='.'&&x>=1&&x<=h&&y>=1&&y<=w){
          		return 1;
          	}
          	return 0;
          }
          
          void dfs(int x,int y){
          	n[x][y]='#';
          	for(int i=0;i<4;i++){
          		int nx=x+xx[i];
          		int ny=y+yy[i];
          		if(f(nx,ny)) 
          		{
          			k++;
          		    dfs(nx,ny);
          		}
          	}
          }
          
          int main(){
          	while(cin>>w>>h){
          		if(w==0&&h==0){
          			break;
          		}
          		for(int i=1;i<=h;i++){
          			for(int j=1;j<=w;j++){
          				cin>>n[i][j];
          			    if(n[i][j]=='@'){
          			    	a=i;b=j;
          				}
          			}
          		}
          		k=1;
          		dfs(a,b);
          		cout<<k<<endl;
          	}
          	return 0;
          }
          
          
          
          • -2
            @ 2024-12-8 15:59:25
            #include <bits/stdc++.h>
            using namespace std;
            const int N = 1e2 + 10;
            char a [N] [N];
            int m , n , k , q , w;
            int xx [4] = {-1 , 1 , 0 , 0};
            int yy [4] = {0 , 0 , 1 , -1};
            bool f (int c , int d)
            {
            	if (a [c] [d] == '.' && c >= 1 && c <= n && d >= 1 && d <= m)
            	{
            		return 1;
            	}
            	return 0;
            }
            void dfs (int x , int y)
            {
            
            	a [x] [y] = '#';
            	for (int i = 0; i < 4; i++)
            	{
            		int nx = x + xx [i];
            		int ny = y + yy [i];
            		if (f (nx , ny)) 
            		{
            			k++;
            		    dfs (nx , ny);
            		}
            	}
            }
            int main()
            {
            	    while (cin >> m >> n)
            	    {
            	    	if (m == 0 && n == 0)
            	    	{
            	    		break;
            			}
            			for (int i = 1; i <= n; i++)
            	        {
            	    	    for (int j = 1; j <= m; j++)
            			    {
            				    cin >> a [i] [j];
            				    if (a [i] [j] == '@')    
            			        {
            					    q = i;
            				        w = j;
            				    }
            			    } 
            		    }
            		    k = 1;
                       dfs(q,w);
            		    cout << k << endl;
            		}
            	    return 0;
            }
            
            
            • 1

            信息

            ID
            3004
            时间
            1000ms
            内存
            256MiB
            难度
            3
            标签
            递交数
            446
            已通过
            104
            上传者