【搜索】(零散刷题记录)

P1451 求细胞数量

题目链接:P1451 求细胞数量 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

bfs:

#include <iostream>
#include <string>
#include <queue>
using namespace std;

int dx[4] = {1, -1, 0, 0};

int dy[4] = {0, 0, 1, -1};
string s[105];
bool vis[105][105];

struct point {
	int x, y;
};

int main() {
	int n, m, ans = 0;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	queue<point>q;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (s[i][j] != '0' && vis[i][j] == 0) {
				ans++;
				vis[i][j] = 1;
				point p;
				p.x = i, p.y = j;
				q.push(p);
				while (!q.empty()) {
					for (int k = 0; k < 4; k++) {
						int xx = q.front().x + dx[k];
						int yy = q.front().y + dy[k];
						if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[xx][yy] == 0 && s[xx][yy] != '0') {
							point t;
							t.x = xx, t.y = yy;
							q.push(t);
							vis[xx][yy] = 1;
						}
					}
					q.pop();
				}
			}
		}
	}
	cout << ans;
	return 0;
}

P1460 [USACO2.1]健康的荷斯坦奶牛 Healthy Holsteins

题目链接:P1460 [USACO2.1]健康的荷斯坦奶牛 Healthy Holsteins - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

dfs: 

#include <iostream>
#include <cstring>
using namespace std;
int v, g, p = (1 << 29), cnt;
int a[30], t[30];
int b[20][30];
int ans[20], minn[20];

bool check() {
	for (int i = 1; i <= v; i++) {
		if (t[i] < a[i]) {
			return false;
		}
	}
	return true;
}

void dfs(int i) {
	if (i > g ) {
		if (p > cnt && check()) {
			p = cnt;
			for (int j = 1; j <= p; j++) {
				ans[j] = minn[j];
			}
		}
		return;
	}
	cnt++;
	minn[cnt] = i;
	for (int j = 1; j <= v; j++) {
		t[j] += b[i][j];
	}
	dfs(i + 1);
	minn[cnt] = 0;
	cnt--;
	for (int j = 1; j <= v; j++) {
		t[j] -= b[i][j];
	}
	dfs(i + 1);
}

int main() {
	cin >> v;
	for (int i = 1; i <= v; i++) {
		cin >> a[i];
	}
	cin >> g;
	for (int i = 1; i <= g; i++) {
		for (int j = 1; j <= v; j++) {
			cin >> b[i][j];
		}
	}
	dfs(1);
	cout << p << ' ';
	for (int i = 1; i <= p; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}

P2372 yyy2015c01挑战算周长

题目链接:P2372 yyy2015c01挑战算周长 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

bfs: 

#include <iostream>
#include <queue>
using namespace std;

int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};

int dy[8] = {1, -1, 0, 0, -1, 1, 1, -1};
char mapp[30][30];
bool vis[30][30];

int main() {
	int m, n, x, y, ans = 0;
	queue<pair<int, int>>q;
	cin >> m >> n >> x >> y;
	for (int i = 0; i < m; i++) {
		cin >> mapp[i];
	}
	q.push(make_pair(x - 1, y - 1));
	vis[x - 1][y - 1] = 1;
	while (!q.empty()) {
		for (int i = 0; i < 8; i++) {
			int xx = q.front().first + dx[i];
			int yy = q.front().second + dy[i];
			if (xx >= 0 && xx < m && yy >= 0 && yy < n && vis[xx][yy] == 0 && mapp[xx][yy] == 'X') {
				vis[xx][yy] = 1;
				q.push(make_pair(xx, yy));
			}
		}
		q.pop();
	}
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			if (vis[i][j]) {
				for (int k = 0; k < 4; k++) {
					int xx = i + dx[k];
					int yy = j + dy[k];
					if (xx < 0 || xx >= m || yy < 0 || yy >= n || vis[xx][yy] == 0) {
						ans++;
					}
				}
			}
		}
	}
	cout << ans;
	return 0;
}

P1683 入门

题目链接:P1683 入门 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

bfs:

#include <iostream>
#include <queue>
using namespace std;
char a[30][30];
bool vis[30][30];

int dx[4] = {0, 0, 1, -1};

int dy[4] = {1, -1, 0, 0};

int main() {
	int n, m, x, y, ans = 1;
	cin >> m >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		for (int j = 0; j < m; j++) {
			if (a[i][j] == '@') {
				x = i;
				y = j;
			} else if (a[i][j] == '#') {
				vis[i][j] = 1;
			}
		}
	}
	vis[x][y] = 1;
	queue<pair<int, int>>q;
	q.push(make_pair(x, y));
	while (!q.empty()) {
		for (int i = 0; i < 4; i++) {
			int xx = q.front().first + dx[i];
			int yy = q.front().second + dy[i];
			if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[xx][yy] == 0) {
				ans++;
				vis[xx][yy] = 1;
				q.push(make_pair(xx, yy));
			}
		}
		q.pop();
	}
	cout << ans;
	return 0;
}

P1331 海战

题目链接:P1331 海战 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

dfs:

#include <iostream>
using namespace std;
int n, m, ans;
char a[1010][1010];

int dx[4] = {0, 0, 1, -1};

int dy[4] = {1, -1, 0, 0};

void dfs(int x, int y) {
	for (int i = 0; i < 4; i++) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		if (xx >= 0 && xx < n && yy >= 0 && yy < m && a[xx][yy] == '#') {
			a[xx][yy] = '.';
			dfs(xx, yy);
		}
	}
}

bool check(int x, int y) {
	int cnt = 0;
	if (a[x][y] == '#') {
		cnt++;
	}
	if (a[x + 1][y] == '#') {
		cnt++;
	}
	if (a[x][y + 1] == '#') {
		cnt++;
	}
	if (a[x + 1][y + 1] == '#') {
		cnt++;
	}
	if (cnt == 3) {
		return false;
	} else {
		return true;
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!check(i, j)) {
				cout << "Bad placement.";
				return 0;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] == '#') {
				a[i][j] = '.';
				dfs(i, j);
				ans++;
			}
		}
	}
	cout << "There are " << ans << " ships.";
	return 0;
}

P1657 选书

题目链接:P1657 选书 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

dfs: 

#include <iostream>
using namespace std;
int x, a[30][2], ans;
bool vis[30];

void dfs(int n) {
	if (n > x) {
		ans++;
		return;
	}
	if (vis[a[n][0]] == 0) {
		vis[a[n][0]] = 1;
		dfs(n + 1);
		vis[a[n][0]] = 0;
	}
	if (vis[a[n][1]] == 0) {
		vis[a[n][1]] = 1;
		dfs(n + 1);
		vis[a[n][1]] = 0;
	}
}

int main() {
	cin >> x;
	for (int i = 1; i <= x; i++) {
		cin >> a[i][0] >> a[i][1];
	}
	dfs(1);
	cout << ans;
	return 0;
}

P1913 L国的战斗之伞兵

题目链接:P1913 L国的战斗之伞兵 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

dfs:

#include <iostream>
using namespace std;

int n, m, ans;
bool vis[1010][1010];
char a[1010][1010];

void dfs(int i, int j) {
	if (a[i][j] == 'o') {
		ans++;
		return;
	}
	if (a[i][j] == 'u') {
		if (i - 1 >= 0 && vis[i - 1][j] == 0) {
			vis[i - 1][j] == 1;
			dfs(i - 1, j);
			vis[i - 1][j] = 0;
		}
	} else if (a[i][j] == 'd') {
		if (i + 1 < n && vis[i + 1][j] == 0) {
			vis[i + 1][j] = 1;
			dfs(i + 1, j);
			vis[i + 1][j] = 0;
		}
	} else if (a[i][j] == 'l') {
		if (j - 1 >= 0 && vis[i][j - 1] == 0) {
			vis[i][j - 1] = 1;
			dfs(i, j - 1);
			vis[i][j - 1] = 0;
		}
	} else if (a[i][j] == 'r') {
		if (j + 1 < m && vis[i][j + 1] == 0) {
			vis[i][j + 1] = 1;
			dfs(i, j + 1);
			vis[i][j + 1] = 0;
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			vis[i][j] = 1;
			dfs(i, j);
			vis[i][j] = 0;
		}
	}
	cout << ans;
	return 0;
}

P2360 地下城主

题目链接:P2360 地下城主 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

最短路径,bfs: 

#include <iostream>
#include <queue>
using namespace std;
int l, r, c;
bool flag;
char a[40][40][40];
bool vis[40][40][40];

int dx[6] = {0, 0, 0, 0, 1, -1};

int dy[6] = {1, -1, 0, 0, 0, 0};

int dz[6] = {0, 0, 1, -1, 0, 0};

struct Point {
	int x, y, z, cnt;
};

int main() {
	cin >> l >> r >> c;
	int x, y, z;
	for (int i = 0; i < l; i++) {
		for (int j = 0; j < r; j++) {
			cin >> a[i][j];
			for (int k = 0; k < c; k++) {
				if (a[i][j][k] == 'S') {
					x = i;
					y = j;
					z = k;
				}
			}
		}
	}
	vis[x][y][z] = 1;
	queue<Point>q;
	Point p;
	p.x = x;
	p.y = y;
	p.z = z;
	p.cnt = 0;
	q.push(p);
	while (!q.empty()) {
		for (int i = 0; i < 6; i++) {
			int xx = q.front().x + dx[i];
			int yy = q.front().y + dy[i];
			int zz = q.front().z + dz[i];
			if (xx >= 0 && xx < l && yy >= 0 && yy < r && zz >= 0 && zz < c && a[xx][yy][zz] != '#' && vis[xx][yy][zz] == 0) {
				vis[xx][yy][zz] = 1;
				Point t;
				t.x = xx;
				t.y = yy;
				t.z = zz;
				t.cnt = q.front().cnt + 1;
				q.push(t);
			}
		}
		if (a[q.front().x][q.front().y][q.front().z] == 'E') {
			flag = 1;
			cout << "Escaped in " << q.front().cnt << " minute(s).";
			return 0;
		}
		q.pop();
	}
	if (!flag) {
		cout << "Trapped!";
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值