UVa 11094 - Continents

题目:有一些岛屿在湖中,地图用两种字符表示,当前处在位置是一个岛屿,求除了当前岛屿外的最大岛屿。

分析:图论,floodfill。直接利用dfs求联通部分的面积即可,然后取出最大。

说明:横线没有边界,初始的岛屿不算在内。╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

char maps[22][22];

int dxy[4][2] = {0,1,1,0,-1,0,0,-1};
int dfs(int x, int y, int m, int n, char c)
{
	if (x < 0 || x >= m) return 0;
	if (maps[x][y] != c) return 0;
	maps[x][y] = 0;
	int sum = 1;
	for (int i = 0; i < 4; ++ i)
		sum += dfs(x+dxy[i][0], (y+n+dxy[i][1])%n, m, n, c);
	return sum;
}

int main()
{
	int m,n,x,y;
	while (cin >> m >> n) {
		for (int i = 0; i < m; ++ i)
			cin >> maps[i];
		cin >> x >> y;
		char ch = maps[x][y];
		int  Max = 0;
		dfs(x, y, m, n, ch);
		for (int i = 0; i < m; ++ i)
		for (int j = 0; j < n; ++ j)
			if (maps[i][j] == ch)
				Max = max(Max, dfs(i, j, m, n, ch));
		cout << Max << endl;
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答您的问题。 首先,您需要在Java中使用MySQL Connector/J连接到MySQL数据库。然后,您需要编写SQL查询来检索ncov表中的所有数据,并按照您所需的方式进行排序。最后,您需要编写Java代码来处理结果并输出排名前三的大洲。 以下是一个示例代码: ```java import java.sql.*; public class NcovStats { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/ncov"; String user = "root"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement()) { // 查询每个大洲每天的确诊数、疑似数、死亡数和治愈数,并按确诊数降序排序 String sql = "SELECT continent, date, confirmed, suspected, deaths, cured " + "FROM ncov " + "ORDER BY confirmed DESC"; ResultSet rs = stmt.executeQuery(sql); // 统计每个大洲的总确诊数、疑似数、死亡数和治愈数 int[] confirmed = new int[6]; int[] suspected = new int[6]; int[] deaths = new int[6]; int[] cured = new int[6]; while (rs.next()) { String continent = rs.getString("continent"); int confirm = rs.getInt("confirmed"); int suspect = rs.getInt("suspected"); int death = rs.getInt("deaths"); int cure = rs.getInt("cured"); switch (continent) { case "Asia": confirmed[0] += confirm; suspected[0] += suspect; deaths[0] += death; cured[0] += cure; break; case "Europe": confirmed[1] += confirm; suspected[1] += suspect; deaths[1] += death; cured[1] += cure; break; case "North America": confirmed[2] += confirm; suspected[2] += suspect; deaths[2] += death; cured[2] += cure; break; case "South America": confirmed[3] += confirm; suspected[3] += suspect; deaths[3] += death; cured[3] += cure; break; case "Africa": confirmed[4] += confirm; suspected[4] += suspect; deaths[4] += death; cured[4] += cure; break; case "Oceania": confirmed[5] += confirm; suspected[5] += suspect; deaths[5] += death; cured[5] += cure; break; } } // 输出排名前三的大洲 String[] continents = {"Asia", "Europe", "North America", "South America", "Africa", "Oceania"}; int[] indices = {0, 1, 2, 3, 4, 5}; for (int i = 0; i < indices.length - 1; i++) { for (int j = i + 1; j < indices.length; j++) { if (confirmed[indices[i]] < confirmed[indices[j]]) { int tmp = indices[i]; indices[i] = indices[j]; indices[j] = tmp; } } } System.out.println("确诊数排名前三的大洲:"); for (int i = 0; i < 3; i++) { System.out.println((i + 1) + ". " + continents[indices[i]] + ": " + confirmed[indices[i]]); } for (int i = 0; i < indices.length - 1; i++) { for (int j = i + 1; j < indices.length; j++) { if (suspected[indices[i]] < suspected[indices[j]]) { int tmp = indices[i]; indices[i] = indices[j]; indices[j] = tmp; } } } System.out.println("疑似数排名前三的大洲:"); for (int i = 0; i < 3; i++) { System.out.println((i + 1) + ". " + continents[indices[i]] + ": " + suspected[indices[i]]); } for (int i = 0; i < indices.length - 1; i++) { for (int j = i + 1; j < indices.length; j++) { if (deaths[indices[i]] < deaths[indices[j]]) { int tmp = indices[i]; indices[i] = indices[j]; indices[j] = tmp; } } } System.out.println("死亡数排名前三的大洲:"); for (int i = 0; i < 3; i++) { System.out.println((i + 1) + ". " + continents[indices[i]] + ": " + deaths[indices[i]]); } for (int i = 0; i < indices.length - 1; i++) { for (int j = i + 1; j < indices.length; j++) { if (cured[indices[i]] < cured[indices[j]]) { int tmp = indices[i]; indices[i] = indices[j]; indices[j] = tmp; } } } System.out.println("治愈数排名前三的大洲:"); for (int i = 0; i < 3; i++) { System.out.println((i + 1) + ". " + continents[indices[i]] + ": " + cured[indices[i]]); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 请注意,这只是一个示例代码,并且需要根据您的实际数据库和表结构进行修改。此外,该代码仅处理了每个大洲的总数,而不是每天的数据。如果您需要每天的数据,请在SQL查询中添加日期过滤条件,并修改Java代码以处理每天的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值