Multiplayer Moo

时间限制: 2 Sec  内存限制: 256 MB
提交: 97  解决: 15
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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.

来源/分类

USACO 2018 US Open Contest, Silver

题目大意:给出一个矩阵,矩阵里边的编号代表一个牛的id,第一行让输出一个最大的联通块,第二行让两个牛组成一个团队,然后再输出最大的块是多少个。

本来我将每个块的大小排序,每次抽出来两个块合并之后搜。但是这样应该属于无脑爆搜。

结束之后听了同学和学长讲题后才明白。正确的剪枝。

正解:将每个数字(id范围0-1e6)的出现次数统计一下,然后从大到小排序。每次我们尽量合并块数较多的id,一个很好的剪枝就是如果当前两个数字的个数小于当前的暂时最优解就直接break;(因为当前块之后的块数肯定更小)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define N 100005
#define inf 0x3f3f3f3f
#define pb(x) push_back(x)
#define LL long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define maxn 1000005
struct node
{int num,k;node(){num=0,k=0;}friend bool operator < (node xx,node yy){return xx.k>yy.k;}
}s[maxn];int ans=0;
int vis[350][350];
int a[350][350];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
int n;
void dfs(int x,int y,int step,int last1,int last2)
{//cout<<x<<" "<<y<<" "<<step<<" "<<last1<<" "<<last2<<endl;vis[x][y]=1;ans=max(ans,step)+1;rep(j,0,3){int xx=x+dx[j];int yy=y+dy[j];if(xx<=0||x>n)continue;if(yy<=0||yy>n)continue;if(vis[xx][yy])continue;if(a[xx][yy]!=last1&&a[xx][yy]!=last2)continue;dfs(xx,yy,step+1,last1,last2);}
}void init()
{for(int i=0;i<=maxn;i++){s[i].k=0;}
}int solve(int x,int y)
{int ask=0;rep(i,1,n){rep(j,1,n){if(vis[i][j])continue;if(a[i][j]==x||a[i][j]==y){dfs(i,j,0,x,y);ask=max(ask,ans);// cout<<x<<" "<<y<<" "<<ask<<endl;ans=0;}}}return ask;
}
int main()
{while(cin>>n){init();memset(vis,0,sizeof(vis));rep(i,1,n){rep(j,1,n){scanf("%d",&a[i][j]);}}int m=0;rep(i,1,n){rep(j,1,n){if(vis[i][j])continue;dfs(i,j,0,a[i][j],a[i][j]);int tmp=a[i][j];s[tmp].k+=ans;s[tmp].num=tmp;m=max(m,ans);ans=0;}}cout<<m<<endl;sort(s,s+maxn);int ans2=m;rep(i,0,maxn){rep(j,i+1,maxn){if(s[i].k+s[j].k<=ans2)break;memset(vis,0,sizeof(vis));int last=solve(s[i].num,s[j].num);//cout<<"**************"<<endl;ans2=max(ans2,last);ans=0;}}cout<<ans2<<endl;}
}

Multiplayer Moo相关推荐

  1. Multiplayer Moo[ [ 并查集+dfs连通块 ] / [ dfs ] ]

    题目链接 题目描述: The cows have come up with a creative new game, surprisingly giving it the least creative ...

  2. USACO2018 OPEN TEST - Silver

    USACO2018 OPEN TEST - Silver 1. Lemonade Line 题目描述 这是农场上一个炎热的夏日,Farmer John要给他的 NNN 头奶牛发柠檬汽水了!所有的 NN ...

  3. Unity中创建本地多人游戏完整案例视频教程 Learn To Create A Local Multiplayer Game In Unity

    Unity中创建本地多人游戏完整案例视频教程 Learn To Create A Local Multiplayer Game In Unity MP4 |视频:h264,1280x720 |音频:A ...

  4. unity镜像_通过镜像学习Unity Multiplayer Basics

    unity镜像 Unity is one of the most well-known and established engines for game development, and Mirror ...

  5. POJ 2231 Moo Volume(递推、前缀和)

    题外话: POJ 2231 Moo Volume 题意: 解题过程: AC代码: 题外话: emm--第三套题好像综合了其他OJ的题目蛤,那么我就把题目分开了发了蛤蛤-- POJ 2231 Moo V ...

  6. Moo.fx 超级轻量级的 javascript 特效库

    Moo.fx是 一个超级轻量级的 javascript 特效库(7k),能够与 prototype.js 或mootools 框架一起使用.它非常快.易于使用.跨浏览器.符合标准,提供控制和修改任何 ...

  7. ActionScript for Multiplayer Games and Virtual Worlds 下载。

    感谢NN~的努力, 一本新书<ActionScript for Multiplayer Games and Virtual Worlds>国内的朋友可以很快免费看到了. 下载地址: htt ...

  8. bzoj1679[Usaco2005 Jan]Moo Volume 牛的呼声*

    bzoj1679[Usaco2005 Jan]Moo Volume 牛的呼声 题意: N只牛,每只牛都与其他N-1只牛聊着天.一个对话的进行,需要两只牛都按照和她们间距离等大的音量吼叫,计算音量和.N ...

  9. 【BZOJ】1679: [Usaco2005 Jan]Moo Volume 牛的呼声(数学)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1679 水题没啥好说的..自己用笔画画就懂了 将点排序,然后每一次的点到后边点的声音距离和==(n-i ...

最新文章

  1. 微软为什么从 C/C++ 转向了 Rust?
  2. Silverlight Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)
  3. spring的Aop使用问题
  4. mxnet基础到提高(27)-Dense
  5. 杭电1262--寻找素数对(打表)
  6. I AM NOTHING vs I AM SOMETHING
  7. JS遍历对象或者数组
  8. 房价必须涨,不涨不正常!因为妈妈又印钱了
  9. sqlserver空间数据 + c# 实现查询附近的设备
  10. 智能硬件(4)---NB-IoT的DRX、eDRX、PSM三个模式怎么用?
  11. 实践总结 - 不可错过的Angular应用技巧
  12. kb3042553显示不适用计算机,KB3042553补丁打不上,怎么办?
  13. go 1.5 国内下载地址
  14. 用友nc系统服务器端口号,用友NC数据库服务器参数配置说明
  15. easyui datagrid 点击其它 单元格,不让头列 checkbook 选中
  16. 分享 100 道基础的前端面试题(附答案)
  17. 青少年编程教育平台后台—登录注册(界面设计)
  18. 利用一般分配律降低时间复杂度
  19. 上海计算机5年制大专学校,上海五年一贯制大专学校有哪些
  20. ubuntu 安装软件 tar.gz deb

热门文章

  1. matlab:inv,pinv逆与伪逆
  2. MSP430待机功耗问题
  3. 为docker设置国内镜像
  4. SRS流媒体服务器——服务器读取RTMP推流数据
  5. ajax返回报错html,Jquery AJAX POST调用返回200状态确定,但错误
  6. android原生见缝插针游戏自定义控件源码
  7. STC89C52RC的P4口的应用问题
  8. 我的博客园博客设计更新记录
  9. 山东女子学院计算机专业分数线,山东女子学院历年录取分数线汇总、
  10. IEEE 期刊双栏模板引用文献问题