2022.3.20

蓝书

AcWing 194. 涂满它!

这次终于写对估计函数了,这次把除了a[0][0]的颜色的个数当做估计函数。我们可以枚举5种颜色,同时判断那些是与左上角的点联通的并且颜色相同的点,把他们认定为待更新的点,并将它们更新(类似bfs),在不断搜索的时候时刻更新当前的层数和估计函数的和是否大于限定深度,如果大于就退出。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 10, INF = 1e8;
int n, a[10][10], vis[10][10];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int check()
{
    int cnt = 0, tot[6] = {0};
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (!tot[a[i][j]] && vis[i][j] != 1)
            {
                tot[a[i][j]] = 1;
                cnt++;
            }
        }
    }
    return cnt;
}
void paint(int x, int y, int color)
{
    vis[x][y] = 1;
    for (int i = 0; i < 4; i++)
    {
        int xx = x + dx[i], yy = y + dy[i];
        if (xx < 0 || xx >= n || yy < 0 || yy >= n || vis[xx][yy] == 1)
            continue;
        vis[xx][yy] = 2;
        if (a[xx][yy] == color)
            paint(xx, yy, color);
    }
}
int change(int color)
{
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            if (a[i][j] == color && vis[i][j] == 2)
            {
                cnt++;
                paint(i, j, color);
            }
    }
    return cnt;
}
bool dfs(int dep, int cnt)
{
    if (cnt + check()> dep)
    {
        return 0;
    }
    if (!check())
        return 1;
    int back[10][10];
    for (int i = 0; i < 6; i++)
    {
        memcpy(back, vis, sizeof vis);
        if (change(i) && dfs(dep, cnt + 1))
            return 1;
        memcpy(vis, back, sizeof vis);
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while (cin >> n && n)
    {
        memset(vis, 0, sizeof vis);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                cin >> a[i][j];
        int dep = 0;
        paint(0, 0, a[0][0]);
        while (dep <= 50)
        {
            if (dfs(dep, 0))
                break;
            dep++;
        }
        cout << dep << '\n';
    }

    return 0;
}

AtCoder Beginner Contest 244

A - Last Letter

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    string s;
    cin >> s;
    cout << s[n-1] << '\n';
    return 0;
}

B - Go Straight and Turn Right

万万没想到是被自己给搞了,明明s[i]'R'给打成s[i]"R",打成双引号愣是没看出来,白白浪费十几分钟。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin >> s;
    int x = 0, y = 0;
    int d = 1;
    for (int i = 0; i < n;i++)
    {
        if(s[i]=='S')
        {
            if(d==1)
                x++;
            if(d==2)
                y--;
            if(d==3)
                x--;
            if(d==4)
                y++;
        }
        if(s[i]=='R')
        {
            if(d==1)
                d = 2;
            else if(d==2)
                d = 3;
            else if(d==3)
                d = 4;
            else if(d==4)
                d = 1;
        }
    }
    cout << x << ' ' << y;
    return 0;
}

C - Yamanote Line Game

需要刷新标准输出,还去查了一下这个该咋弄,就是在输出后加fflush(stdout),不然会超时.结果加了还是tm超时。后面发现是不是和cin关闭流同步有关系,把关闭流同步注释掉之后就过了。
刚刚看完别人的代码发现只要开启流同步就可以了。。endl的是时候会释放缓冲区,不用fflush(stdout)。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e4+10,INF=1e8;
bool vis[N];
int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int n;
    cin >> n;
    int x;
    cout << 1 << endl;
    vis[1] = 1;
    int mmin = 1,t=0;
    while(1)
    {
        cin >> x;
        if(x==0)
            break;
        vis[x] = 1;
        t = mmin;
        while(x%t==0||vis[t])
        {
            t++;
        }
        if(t==mmin)
            mmin++;
        cout << t << endl;
        vis[t] = 1;
        fflush(stdout);
    }
    return 0;
}

D - Swap Hats

一开始模拟的时候把自己绕进去了,以为全是输出yes,没想到还能过一半的样例,于是就重新推了一遍,可以知道如果s串和t串3个位置的字母都不相同的话,我们可以通过2次转换使得它变成t串,因为1e18是偶数,所以这是一定可以的,当有2个位置不相同的时候,经过推导需要3次也就是奇数次才能实现,所以这是不可能的,然后注意一下两个串相等的情况。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
ll n = 1e18;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    char a, b, c;
    char a1, b1, c1;
    cin >> a >> b >> c;
    cin >> a1 >> b1 >> c1;
    int cnt = 0;
    if(a!=a1)
        cnt++;
    if(b!=b1)
        cnt++;
    if(c!=c1)
        cnt++;
    if(cnt==2)
        cout << "No" << '\n';
    if(cnt==3||cnt==0)
        cout << "Yes" << '\n';
    return 0;
}
posted @ 2022-03-20 19:58  menitrust  阅读(22)  评论(0编辑  收藏  举报