4.10 周四晚训练 -- 河西301 总结

· · 个人记录

总结

T1 已AC

T2

错误代码

#include<bits/stdc++.h>
using namespace std;
char c[1005][1005];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            if(c[i][j-3]=='h' and c[i][j-2]=='e' and c[i][j-1]=='h' and c[i][j]=='e')
            {
                cnt++;
            }
        }
    }
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            if(c[i][j+3]=='e' and c[i][j+2]=='h' and c[i][j+1]=='e' and c[i][j]=='h')
            {
                cnt++;
            }
        }
    }
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            if(c[i-3][j]=='h' and c[i-2][j]=='e' and c[i-1][j]=='h' and c[i][j]=='e')
            {
                cnt++;
            }
        }
    }
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            if(c[i+3][j]=='e' and c[i+2][j]=='h' and c[i+1][j]=='e' and c[i][j]=='h')
            {
                cnt++;
            }
        }
    }
    cout<<cnt;
    return 0;
}

错误原因

考试代码可能会发生越界和少判断的情况,所以需要进行以下修改。

正确思路

枚举当前矩阵,将可能越界的含有减法公式改成之后需要减的公式改成现在的加法的公式,同时,判断的条件应该反过来,因为是之后往前找的。最后统计答案即可。

T3T4已AC

T5

错误代码

#include<bits/stdc++.h>
using namespace std;
char a[1005][1005];
char b[1005][1005];
char c[1005][1005];
int n;
bool check()
{
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            if(c[i][j]!=b[i][j])
            {
                return 0;
            }
        }
    }
    return 1;
}
void one()
{
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            c[j][n-i+1]=a[i][j];
        }
    }
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            a[i][j]=c[i][j];
        }
    }
}
void twe()
{
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            c[n-i+1][j]=a[i][j];
        }
    }
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            a[i][j]=c[i][j];
        }
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            cin>>b[i][j];
        }
    }
    one();
    if(check())
    {
        cout<<1;
        return 0;
    }
    one();
    if(check())
    {
        cout<<2;
        return 0;
    }
    one();
    if(check())
    {
        cout<<3;
        return 0;
    }
    one();
    twe();
    if(check())
    {
        cout<<4;
        return 0;
    }
    one();
    if(check())
    {
        cout<<5;
        return 0;
    }
    one();
    if(check())
    {
        cout<<5;
        return 0;
    }
    one();
    if(check())
    {
        cout<<5;
        return 0;
    }
    one();
    twe();
    if(check())
    {
        cout<<6;
        return 0;
    }
    cout<<7;
    return 0;
}

错误原因

考试时me不断询问:"水平方向翻转是上下还是左右?",得到了某达智肯定的回答,"上下翻转!",最后......

正确思路

我们只需要三个函数:check,翻转,水平翻转。check函数就是判断a数组和ans数字是否相等,翻转数组就是顺时针翻转90°,详情请看2.27晚 训练课 -- 河西301 T4,最后是水平翻转,可以参考下图:

(1,4) (1,3) (1,2) (1,1)
(2,4) (2,3) (2,2) (2,1)
(3,4) (3,3) (3,2) (3,1)
(4,4) (4,3) (4,2) (4,1)

不难看出,转换后的数组b[i][j]=a[i][n-j+1]。得到三个函数之后,经过简单的组合之后,就能达到题目给出的各个操作,按题目判断即可。