ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022.3.20

2022-03-20 20:03:33  阅读:133  来源: 互联网

标签:cnt 20 int ++ yy vis xx 2022.3


蓝书

AcWing 194. 涂满它!

比完赛后补题解

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
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;
}

标签:cnt,20,int,++,yy,vis,xx,2022.3
来源: https://www.cnblogs.com/menitrust/p/16031245.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有