upc 6352 Multiplayer Moo

                                                 6352: Multiplayer Moo

                                                                时间限制: 2 Sec  内存限制: 256 MB
 

题目描述

The cows have come up with a creative new game, surprisingly giving it the least creative name possible: "Moo".
The game of Moo is played on an N×N grid of square cells, where a cow claims a grid cell by yelling "moo!" and writing her numeric ID number in the cell.

At the end of the game, every cell contains a number. At this point, a cow wins the game if she has created a region of connected cells as least as large as any other region. A "region" is defined as a group of cells all with the same ID number, where every cell in the region is directly adjacent to some other cell in the same region either above, below, left, or to the right (diagonals don't count).

Since it is a bit boring to play as individuals, the cows are also interested in pairing up to play as teams. A team of two cows can create a region as before, but now the cells in the region can belong to either of the two cows on the team.

Given the final state of the game board, please help the cows compute the number of cells belonging to the largest region that any one cow owns, and the number of cells belonging to the largest region that can be claimed by a two-cow team. A region claimed by a two-cow team only counts if it contains the ID numbers of both cows on the team, not just one of the cows.

输入

The first line of input contains N (1≤N≤250). The next N lines each contain N integers (each in the range 0…106), describing the final state of the game board. At least two distinct ID numbers will be present in the board.

输出

The first line of output should describe the largest region size claimed by any single cow, and the second line of output should describe the largest region size claimed by any team of two cows.

样例输入

4
2 3 9 3
4 9 9 1
9 9 1 7
2 1 1 9

样例输出

5
10

提示

In this example, the largest region for a single cow consists of five 9s. If cows with IDs 1 and 9 team up, they can form a region of size 10.

题目大意:

N × N 的矩阵, 每个方格中都填有一个数字。 问题一,相同数字上下左右连通个数的最大值;问题二, 若把两个数看做一组上下左右连通个数的最大值。

分析:

第一问,dfs求连通块即可。 第二问,在第一问的过程中统计,每个数字出现的次数,按从大到小排序, 暴力枚举两个数,把两个数看做一起再dfs即可。 稍微剪一下枝就ok了。

AC代码:

#include <bits/stdc++.h>
#define mset(a, x) memset(a, x, sizeof(a))
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int dir[4][2] = {1, 0, 0 ,1, -1, 0, 0, -1};
int n, sum;
int mp[505][505];
int vis[505][505];
struct node {
    int num, sum;
}a[N];
bool judge(int x, int y) {
    if (x <= 0 || y <= 0 || x > n || y > n) {
        return false;
    }
    return true;
}
bool cmp(node x, node y) {
    return x.sum > y.sum;
}
void dfs(int x, int y, int num, int num2 = -1) {
    for (int i = 0; i < 4; i++) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if (judge(tx, ty) && !vis[tx][ty] && (mp[tx][ty] == num || mp[tx][ty] == num2)) {
            vis[tx][ty] = 1;
            sum++;
            dfs(tx, ty, num, num2);
        }
    }
}
int solve(int x, int y) {
    int ss = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (!vis[i][j]) {
                if (mp[i][j] == x || mp[i][j] == y) {
                    sum = 1;
                    vis[i][j] = 1;
                    dfs(i, j, x, y); 
                    ss = max(ss, sum);
                }
            }
        }
    }
//  cout << ss << "    " << x << "    " << y  << endl;
    return ss;
}
int main() {
    std::ios::sync_with_stdio(false);
    mset(a, 0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> mp[i][j];
        }
    }
    int maxn1 = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (!vis[i][j]) {
                vis[i][j] = 1;
                sum = 1;
                dfs(i, j,mp[i][j]);
                maxn1 = max(maxn1, sum);
                a[mp[i][j]].num = mp[i][j];
                a[mp[i][j]].sum += sum;
            }
        }
    }
    sort(a, a + N, cmp);
    int maxn2 = maxn1;
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            if (a[i].sum + a[j].sum <= maxn2) break;
            mset(vis, 0);
            maxn2 = max(maxn2, solve(a[i].num, a[j].num));
        }
    }
    cout << maxn1 << endl << maxn2 << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mm__1997/article/details/81274628
UPC