1 条题解

  • 2
    @ 2022-12-30 15:00:34
    #include <bits/stdc++.h> //万能头文件
    using namespace std;
    int n,m,k,ans=0,xs,ys,dy[6]={0,0,1,-1,0,0},dh[6]={1,-1,0,0,0,0};
    int dx[6]={0,0,0,0,1,-1}; //方向数组
    char a[15][15][15];
    void dfs(int h,int x,int y)
    {
    	ans++; //更新答案
    	a[h][x][y]='#'; //标记一下
    	for(int i=0;i<6;i++)
    	{
    		int hh=h+dh[i],xx=x+dx[i],yy=y+dy[i];
    		if(hh<=k&&hh>=1&&xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[hh][xx][yy]=='.') //不能超界,且走得通
    			dfs(hh,xx,yy);
    	}
    }
    int main()
    {
    	cin>>k>>n>>m;
    	for(int i=1;i<=k;i++)
    	    for(int j=1;j<=n;j++)
    	        for(int k=1;k<=m;k++)
    		        cin>>a[i][j][k]; //输入
    	cin>>xs>>ys;
    	dfs(1,xs,ys); //从起始点开始
    	cout << ans; //输出答案
    	return 0;
    }
    
    • @ 2023-10-3 21:06:11
      #include <bits/stdc++.h>
      using namespace std;
      int n ,m ,k ,ans = 0 ,xs ,ys;
      int dy[6] = {0 ,0 ,1 ,-1 ,0 ,0};
      int dh[6] = {1 ,-1 ,0 ,0 ,0 ,0};
      int dx[6] = {0 ,0 ,0 ,0 ,1 ,-1};
      char a[15][15][15];
      void dfs(int h ,int x,int y)
      {
      ans++;
      a[h][x][y] = '#';
      for (int i = 0;i < 6;i++)
      {
      int hh = h + dh[i];
      int xx = x + dx[i];
      int yy = y + dy[i];
      if(hh <= k && hh >= 1 && xx >= 1 && xx <= n && yy >= 1 && yy <= m && a[hh][xx][yy] == '.')
      {
      dfs(hh,xx,yy);
      }
      }
      }
      int main()
      {
      cin >> k >> n >> m;
      for (int i = 1;i <= k;i++)
      {
      for (int j = 1;j <= n;j++)
      {
      for  (int k = 1;k <= m;k++)
      {
      cin >> a[i][j][k];
      }
      }
      }
      cin >> xs >> ys;
      dfs(1 ,xs ,ys);
      cout << ans;
      return 0;
      }
      
  • 1

信息

ID
1922
时间
1000ms
内存
256MiB
难度
3
标签
递交数
79
已通过
43
上传者