和大伙伴做题-gym C. Figures

2024-06-01 21:32
文章标签 gym 做题 伙伴 figures

本文主要是介绍和大伙伴做题-gym C. Figures,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C. Figures
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a map divided into n × m square fields which are colored. We call a subset of fields of the same color a Figure, if it is:

  • in one piece: for every two fields in the Figure, there is a path between them composed of side-adjacent fields belonging to the Figure,
  • maximal: every field from outside the Figure that is adjacent to it has a different color.
Figures A and B are considered identical (they might be colored differently) if you can move A onto B so that every field of B is covered by a field of A and every field of A covers a field from B, without turning the Figure. Calculate the number of different Figures on the given map.

Input

In the first line of input there is an integer z - the number of test cases. Then, z test cases follow.

First line of a test case contains two integers, n and m (1 ≤ n, m ≤ 1000) - number of rows and columns of the map. In next n lines you are given the description of rows on the map: m integers which are nonnegative and no greater than 109. These integers are the colors of fields in the map.

Output

For each test case, print one line with one integer: the number of different figures.

Sample test(s)
input
1
5 5
1 1 2 2 2
3 1 3 3 4
3 4 4 3 4
5 5 5 2 2
1 1 1 6 2
output
5

题意:给定一个图,求出不同形状的块有几种。

思路:我自己想用二维数组记录下拼图形状在插入到set,可是好像set不太支持哦。。。然后大伙伴说可以利用pair保存点,用set保存点集的方法来保存形状,大伙伴实在机智得我整个人都不行了。f@ck。

代码:

#include <stdio.h>
#include <string.h>
#include <set>
using namespace std;
const int d[4][2] = {{0, 1}, {-1, 0}, {1, 0}, {0, -1}};int t, n, m, g[1005][1005], vis[1005][1005];typedef pair<int ,int> pi;
set<pi> aa;
set<set<pi> > bb;void dfs(int x, int y, int num, int a, int b) {vis[x][y] = 1;aa.insert(make_pair(x - a, y - b));for (int i = 0; i < 4; i++) {int xx = x + d[i][0];int yy = y + d[i][1];if (num == g[xx][yy] && !vis[xx][yy] && xx >= 0 && xx < n && yy >= 0 && yy < m) {dfs(xx, yy, num, a, b);}}
}
int main() {scanf("%d", &t);while (t --) {memset(vis, 0, sizeof(vis));bb.clear();int count = 0;scanf("%d%d", &n, &m);for (int i = 0; i < n; i ++)for (int j = 0; j < m; j ++)scanf("%d", &g[i][j]);for (int i = 0; i < n; i ++)for (int j = 0; j < m; j ++) {if (!vis[i][j]) {aa.clear();dfs(i, j, g[i][j], i, j);if (bb.find(aa) == bb.end()) {bb.insert(aa);count ++;}}}printf("%d\n", count);}return 0;
}


这篇关于和大伙伴做题-gym C. Figures的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1022163

相关文章

Creating OpenAI Gym Environment from Map Data

题意:从地图数据创建 OpenAI Gym 环境 问题背景: I am just starting out with reinforcement learning and trying to create a custom environment with OpenAI gym. However, I am stumped with trying to create an enviro

小杨做题c++

题目描述 为了准备考试,小杨每天都要做题。第1天,小杨做了a道题;第2天,小杨做了b道题;从第3天起,小杨每天做的题目数量是前两天的总和。 此外,小杨还规定,当自己某一天做了大于或等于m题时,接下来的所有日子里,他就再也不做题了。 请问,到了第N天,小杨总共做了多少题呢? 输入 总共4行。第一行一个整数a,第二行一个整数b,第三行一个整数m,第四行一个整数N。 保证0≤a,b≤10; a,b

【codeforces】gym 101137 K - Knights of the Old Republic【用最小生成树对图做集合dp】

题目链接:【codeforces】gym 101137 K - Knights of the Old Republic 考虑对图集合dp,一个连通块的dp值为两个连通块的值的和或者强制加一条新边后的最小值,取个最小值(边从小到大枚举,则强制加一条最大的边会导致连通块内较小的边一定都选,则会构成一个生成树)。用kruskal实现这个dp过程即可。 #include <bits/stdc++.h>

【codeforces】gym 101138 K. The World of Trains【前缀和优化dp】

题目链接:K. The World of Trains 记录一个横着的前缀dp和以及斜着的前缀dp,复杂度 O(n2) O(n^2) #include <bits/stdc++.h>using namespace std ;typedef pair < int , int > pii ;typedef long long LL ;#define clr( a , x ) memset (

How to user “Discrete“ object in openai-gym environments?

题意:怎样在 OpenAI Gym 环境中使用 “Discrete” 对象 问题背景: I am trying to create a Q-Learning agent for a openai-gym "Blackjack-v0" environment. I am trying to get the size of the observation space but its in

OpenAI Gym custom environment: Discrete observation space with real values

题意:OpenAI Gym 自定义环境:具有实数值的离散观测空间 问题背景: I would like to create custom openai gym environment that has discrete state space, but with float values. To be more precise, it should be a range of valu

Pyqt5高级技巧2:Tab顺序、伙伴快捷键、各类常用控件的事件(含基础Demo)

一、编辑Tab顺序         点击下面这个按钮后,按控件调整tab的顺序,设置好后,鼠标聚焦在输入框1中,按一下tab鼠标聚焦会跳到下一个输入框中         编辑tab结束后,按下面这个按钮重新返回页面布局   二、编辑伙伴  (删除伙伴的方法:框选-右键选择全部-删除) 三、设置快捷键(仅MainWindow可用)         例如我菜单(MainWind

浪潮信息携区域ISP伙伴,共创AI应用新生态

2024年8月12日,浪潮信息成功举办区域ISP伙伴战略签约盛会,携手全国33家顶尖的区域ISP伙伴,共同签署战略合作协议。此次合作横跨13个省区,深度覆盖互联网、电信、金融及制造等前沿行业,依托浪潮信息强大的“元脑企智”EPAI大模型开发平台,双方旨在加速AI技术的区域应用落地,共创智能产业新未来。 区域ISP伙伴:架起AI技术与应用创新的桥梁 区域ISP伙伴作为深化中国AI应用发展的中

Marscode:程序员的智能伙伴,2024 活动震撼来袭

在程序员的世界里,高效的开发工具如同手中的利器,能让我们在代码的海洋中披荆斩棘。今天,我要向大家隆重介绍一款强大的智能开发工具——Marscode,以及它带来的精彩 2024 活动。 一、Marscode:智能开发的新势力 Marscode 是由字节跳动基于豆包大模型打造的面向开发者群体的工具,它拥有两种产品形态:编程助手和 CloudIDE。无论你是在 Windows、macOS 还是 Li

SAT数学考试做题时需要用到的数学公式

SAT数学做题时可能会用到的公式:   (做题时会遇到的相关概念将于下篇出现,这里只是单独的公式集锦   1.抛物线:y = a(x^2 + bx + c   (y等于ax 的平方加上 bx再加上 c   a > 0时开口向上   a 0   5. 椭圆(很少用到,知道就可以了   1周长公式:L=2πb+4(a-b   椭圆周长定理:椭圆的周长等于该椭圆短半轴长为半径的圆