代码随想录第五十八天打卡

101.  孤岛的总面积

基础题目 可以自己尝试做一做 。

代码随想录

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node {
	int x, y;
	node(int x, int y) :x(x), y(y) {};
};
int res=0;
queue<node>que;
int n, m;
void bfs(vector<vector<int>>&mp,int x,int y) {
	bool flat=true;
	int total = 1;
	if (x == 0 || y == 0 || x == n - 1 || y == m - 1)flat = false;
	que.push(node(x, y));
	int dx[] = { 0,0,1,-1 };
	int dy[] = { 1,-1,0,0 };
	while (!que.empty()) {
		node cur = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int xx = cur.x + dx[i];
			int yy = cur.y + dy[i];
			if (xx < 0 || yy < 0 || xx >= n || yy >= m || mp[xx][yy] == 0)continue;
			if (xx == 0 || yy == 0 || xx == n - 1 || yy == m - 1)flat = false;
			mp[xx][yy] = 0;
			que.push(node(xx, yy));
			total++;
		}
	}
	if (flat)res += total;
}
void solve() {
	cin >> n >> m;
	vector<vector<int>>mp(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> mp[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (mp[i][j] == 1) {
				mp[i][j] = 0;
				bfs(mp, i, j);
			}
		}
	}
	cout << res << endl;
}
int main() {
	solve();
	return 0;
}

102.  沉没孤岛

和上一题差不多,尝试自己做做

代码随想录

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct node {
	int x, y;
	node(int x, int y) :x(x), y(y) {};
};
queue<node>que;
int n, m;
void bfs(vector<vector<int>>&mp,vector<vector<int>>&visited,int x,int y) {
	que.push(node(x, y));
	vector<node>path; path.push_back(node(x, y));
	bool flag = true;
	int dx[] = { 0,0,-1,1 };
	int dy[] = { -1,1,0,0 };
	if (x == 0 || x == n - 1 || y == 0 || y == m - 1)flag = false;
	while (!que.empty()) {
		node cur = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int xx = cur.x + dx[i];
			int yy = cur.y + dy[i];
			if (xx < 0 || xx >= n || yy < 0 || yy >= m || !visited[xx][yy] || mp[xx][yy] == 0)continue;
			if (xx == 0 || xx == n - 1 || yy == 0 || yy == m - 1)flag = false;
			visited[xx][yy] = 0;
			que.push(node(xx, yy));
			path.push_back(node(xx, yy));
		}
	}
	if (flag) {
		for (auto cur : path) {
			mp[cur.x][cur.y] = 0;
		}
	}
}

void solve() {
	cin >> n >> m;
	vector<vector<int>>mp(n, vector<int>(m, 0));
	vector<vector<int>>visited(n, vector<int>(m, 1));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> mp[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (visited[i][j] && mp[i][j] == 1) {
				bfs(mp, visited, i, j);
				visited[i][j] = 0;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << mp[i][j] << " ";
		}
		cout << endl;
	}
}
int main() {
	solve();
	return 0;
}

103.  水流问题

需要点优化思路,建议先自己读题,相处一个解题方法,有时间就自己写代码,没时间就直接看题解,优化方式 会让你 耳目一新。

代码随想录

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct node {
	int x, y;
	node(int x, int y) :x(x), y(y) {};
};
queue<node>que;
int n, m;
void bfs(vector<vector<int>>& mp, vector<vector<int>>& visited, int x, int y) {
	visited[x][y] = 1;
	que.push(node(x, y));
	int dx[] = { 1,-1,0,0 };
	int dy[] = { 0,0,1,-1 };
	while (!que.empty()) {
		node cur = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int xx = cur.x+dx[i];
			int yy = cur.y+dy[i];
			if (xx < 0 || xx >= n || yy < 0 || yy >= m || visited[xx][yy] == 1 || mp[xx][yy] < mp[cur.x][cur.y])continue;
			visited[xx][yy] = 1;
			que.push(node(xx, yy));
		}
	}
}

void solve() {
	cin >> n >> m;
	vector<vector<int>>mp(n, vector<int>(m, 0));
	vector<vector<int>>visited1(n, vector<int>(m, 0));
	vector<vector<int>>visited2(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> mp[i][j];
		}
	}
	for (int i = 0; i <m; i++) {
		bfs(mp, visited1, 0, i);
		bfs(mp, visited2, n - 1, i);
	}
	for (int i = 0; i < n; i++) {
		bfs(mp, visited1, i, 0);
		bfs(mp, visited2, i, m - 1);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (visited1[i][j] && visited2[i][j]) {
				cout << i << " " << j << endl;
			}
		}
	}
}
int main() {
	solve();
	return 0;
}

104.建造最大岛屿

同样优化思路也会让你耳目一新,自己想比较难想出来。

代码随想录

#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
vector<int>areal = {0};
int n, m;
struct node {
	int x, y;
	node(int x, int y) :x(x), y(y) {};
};
queue<node>que;
int bfs(vector<vector<int>>&mp,vector<vector<int>>&visited,int x,int y,int number) {
	int res = 1;
	que.push(node(x, y));
	visited[x][y] = number;
	int dx[] = { 0,0,-1,1 };
	int dy[] = { 1,-1,0,0 };
	while (!que.empty()) {
		node cur = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int xx = cur.x + dx[i];
			int yy = cur.y + dy[i];
			if (xx<0 || xx>n - 1 || yy<0 || yy>m - 1 || mp[xx][yy] == 0 || visited[xx][yy] == number)continue;
			visited[xx][yy] = number;
			res++;
			que.push(node(xx, yy));
		}
	}
	return res;
}
void solve() {
	cin >> n >> m;
	vector<vector<int>>mp(n, vector<int>(m, 0));
	vector<vector<int>>visited(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> mp[i][j];
		}
	}
	int max_area = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!visited[i][j] && mp[i][j] == 1) {
				int area=bfs(mp, visited, i, j,areal.size());
				max_area = max(max_area, area);
				areal.push_back(area);
			}
		}
	}
	int res = 0;
	bool flag = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (visited[i][j] == 0) {
				flag = true;//防止没有水的情况
				int total = 0;
				set<int>s;//用集合来相加,防止周围都是一个岛屿的情况
				if (i > 0)s.insert(areal[visited[i - 1][j]]);
				if (i < n - 1)s.insert(areal[visited[i + 1][j]]);
				if (j > 0)s.insert(areal[visited[i][j - 1]]);
				if (j < m - 1)s.insert(areal[visited[i][j + 1]]);
				for (int i : s)total += i;
				res = max(total+1, res);
			}
		}
	}
	if (flag)cout << res << endl;
	else cout << max_area << endl;
}
int main() {
	solve();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值