2 条题解

  • 6
    @ 2022-7-23 20:04:20

    学霸题

    属实看到这题先想到这个了,于是就有了这篇题解

    这题能做出来不是一般悬,好像有一篇题解了,我看不懂,但我大受震撼()

    我应该是属于遍历吧,学霸题一般来说分为两步

    1.头顶标数法
    2.从上往下数
    

    所以就分两步来解决这个问题

    第一步先统计俯视图能看到的正方体数量(不止一种解法)

    我的方法就是先统计俯视图状态下的'+'号正下方一定有一个' '同时这个空格右面的两个字符也一定会是空格

        if( a[ i ][ j ] == '+' )
        {
            if( a[ i + 1 ][ j ] == ' ' and a[ i + 1 ][ j + 1 ] == ' ' and a[ i + 1 ][ j + 2 ] == ' ' )
            {
                //内容
            }
        }
    

    第二步就是统计每个正方体所处层数

    我的方法就是如果是在外层那么'+'号下方一定会有'|'这个符号,并且一旦发现这个符号就可以把层数+1了,此时有两种情况,因为我们一直在统计相对于左边的正方体的角(也就是+号),所以此时如果下面是|那么说明这个+号是这个正方体的俯视图的左下角,可以很方便地统计层数,而下面还有可能是空格(和第一步相同)此时我们看样例可以发现这个左上角的+下面2行左边2列就是这个正方体俯视图左下角,如果没有找到'|'就一直左2下2就可以找到最外层的|就可以统计层数了

        int cs = 0 ;
        int nowx = i , nowy = j ;
        while( nowx >= 1 and nowy <= m )
        {
          if( nowx > n or nowy < 1 ) break ;
          if( a[ nowx + 1 ][ nowy ] == '|' )
          {
            cs ++ ;
            nowx = nowx + 3 ;
          }
          else 
          {
            nowy = nowy - 2 ;
            nowx = nowx + 2 ;
          }
        }
        ans += cs ;
    

    最后还有几个问题

    数据范围,我没看明白数据范围,但是我看到了50,所以我把范围开到1000肯定没问题(

    循环时的数组越界,只要你确定了这是一个正方体俯视图的左上角,那么你怎么写还会越界吗((

    代码贴了

        #include<bits/stdc++.h>
        using namespace std;
        int m , n , ans , ans2 ;
        char a[ 1001 ][ 1001 ] ;
        string test ;
        int main()
        {
            ios::sync_with_stdio( 0 ) ;
            cin.tie( 0 ) ;
            cin >> n >> m ;
            for( int i = 1 ; i <= n ; i ++ )
            {
                getline( cin , test ) ;
                for( int j = 1 ; j <= m ; j ++ )
                {
                    a[ i ][ j ] = test[ j - 1 ] ;
                }
            }
            for( int i = 1 ; i <= n ; i ++ )
            {
                for( int j = 1 ; j <= m ; j ++ )
                {
                    if( a[ i ][ j ] == '+' )
                    {
                        if( a[ i + 1 ][ j ] == ' ' and a[ i + 1 ][ j + 1 ] == ' ' and a[ i + 1 ][ j + 2 ] == ' ' )
                        {
                            int cs = 0 ;
                            int nowx = i , nowy = j ;
                            while( nowx >= 1 and nowy <= m )
                            {
                                if( nowx > n or nowy < 1 ) break ;
                                if( a[ nowx + 1 ][ nowy ] == '|' )
                                {
                                    cs ++ ;
                                    nowx = nowx + 3 ;
                                }
                                else 
                                {
                                    nowy = nowy - 2 ;
                                    nowx = nowx + 2 ;
                                }
                            }
                            ans += cs ;
                        }
                    }
                }
            }
            cout << ans ;
            return 0;
        }
    
  • 1
    @ 2022-7-21 10:15:17

    好像关于图的题都离不开bfs 上次的钻石也是 以+为基准bfs 思路很明确 重点! 横着要+4 竖着+3 斜着+2 这个要数对了

    #include <bits/stdc++.h>
    using namespace std;
    char a[505][505];
    bool vis[505][505];
    int n,m,sum;
    struct P{
    	int x,y,d;
    };
    queue<P> q;
    void bfs(int c,int b,int ans)
    {
    	q.push((P){c,b,ans});
    	vis[c][b] = 1;
    	while(!q.empty())
    	{
    		P t = q.front();
    		q.pop();
    		int p = 0;
    		if(t.x < 3)
    		{
    			continue;
    		}
    		if(a[t.x-3][t.y] == '+' && a[t.x-3][t.y+4] == '+' && a[t.x][t.y+4] == '+')
    		{
    			sum += t.d;//利用长高平面的主视图搜索,找到一个正方形就乘以宽
    		}
    		if(!vis[t.x-3][t.y] && a[t.x-3][t.y] == '+' )
    		{
    			vis[t.x-3][t.y] = 1;
    			q.push((P){t.x-3,t.y,t.d});
    		}
    		if(!vis[t.x][t.y+4] && a[t.x][t.y+4]=='+')
    		{
    			vis[t.x][t.y+4] = 1;
    			q.push((P){t.x,t.y+4,t.d});
    		}
    		if(!vis[t.x-3][t.y+4] && a[t.x-3][t.y+4] == '+')
    		{
    			vis[t.x-3][t.y+4] = 1;
    			q.push((P){t.x-3,t.y+4,t.d});
    		}
    		if(!vis[t.x-2][t.y+2] && a[t.x-2][t.y+2] == '+')
    		{
    			vis[t.x-2][t.y+2] = 1;
    			q.push((P){t.x-2,t.y+2,t.d-1});
    		}
    	}
    }
    int main()
    {
    	cin >> n >> m;
    	getchar();
    	for(int i=1; i <= n ;i++)
    	{
    		string s;
    		getline(cin,s);
    		for(int j=0; j < m ;j++)
    		{
    			a[i][j+1] = s[j];
    		}
    	}
        int	k = n, z = 1,ans=-1;
    	while(a[k][z] == '+')
    	{
    		if(a[k-3][z] == '+')
    		{
    			k-=3;
    		}
    		else
    		{
    			ans++;
    			k-=2;
    			z+=2;
    		}
    	}// ans 先把宽找到
     	bfs(n,1,ans);
     	cout << sum;
     	return 0;
    }
    
    • 1

    信息

    ID
    1945
    时间
    1000ms
    内存
    256MiB
    难度
    1
    标签
    (无)
    递交数
    54
    已通过
    37
    上传者