2 条题解

  • 1
    @ 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
      @ 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
      标签
      递交数
      290
      已通过
      67
      上传者