图的联通块问题

xiaoxiao2021-02-28  21

链接:https://www.nowcoder.com/acm/contest/76/A来源:牛客网

随着海上运输石油泄漏的问题,一个新的有利可图的行业正在诞生,那就是撇油行业。如今,在墨西哥湾漂浮的大量石油,吸引了许多商人的目光。这些商人们有一种特殊的飞机,可以一瓢略过整个海面20米乘10米这么大的长方形。(上下相邻或者左右相邻的格子,不能斜着来)当然,这要求一瓢撇过去的全部是油,如果一瓢里面有油有水的话,那就毫无意义了,资源完全无法利用。现在,商人想要知道,在这片区域中,他可以最多得到多少瓢油。

地图是一个N×N的网络,每个格子表示10m×10m的正方形区域,每个区域都被标示上了是油还是水

输入描述:

测试输入包含多条测试数据 测试数据的第一行给出了测试数据的数目T(T<75) 每个测试样例都用数字N(N<50)来表示地图区域的大小,接下来N行,每行都有N个字符,其中符号’.’表示海面、符号’#’表示油面。

输出描述:

输出格式如下“Case X: M”(X从1开始),M是商人可以最多得到的油量。 #include<stdio.h> #include<iostream> #include<map> #include<algorithm> #include<string.h> using namespace std; char mp[100][100]; int n; pair<int,int> getans(int y, int x) { mp[y][x] = '.'; //奇数,偶数 pair<int, int>ss; if ((x + y) & 1) { ss.first = 1; } else { ss.second = 1; } if (y + 1 < n && mp[y + 1][x] == '#') { pair<int, int>tmp = getans(y + 1, x); ss.second += tmp.second; ss.first += tmp.first; } if (y - 1 >= 0 && mp[y - 1][x] == '#') { pair<int, int>tmp = getans(y - 1, x); ss.second += tmp.second; ss.first += tmp.first; } if (x + 1 < n && mp[y][x + 1] == '#') { pair<int, int>tmp = getans(y, x + 1); ss.second += tmp.second; ss.first += tmp.first; } if (x - 1 >= 0 && mp[y][x - 1] == '#') { pair<int, int>tmp = getans(y, x - 1); ss.second += tmp.second; ss.first += tmp.first; } return ss; } int main() { int t; cin >> t; int cnt = 1; while (cnt<=t) { cin >> n; for (int i = 0;i < n;i++) { scanf("%s", mp[i]); } int ans = 0; for (int i = 0;i < n;i++) { for (int j = 0;j < n;j++) { if (mp[i][j] == '#') { pair<int, int>ss = getans(i, j); ans += min(ss.first, ss.second); } } } printf("Case %d: %d\n", cnt++, ans); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2625762.html

最新回复(0)