信息学奥赛一本通 2.8:广度优先搜索

1252 走迷宫
#include <iostream>
#include <queue>
using namespace std;

const int N = 50;
char a[N][N];
int step[N][N];
int r, c;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

struct node{int x, y;};
queue<node> q;

void bfs() {
	node point, front;
	
	point.x = 1, point.y = 1;
	q.push(point);
	step[1][1] = 1;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i ++ ) {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 1 || xx > r || yy < 1 || yy > c) continue;
			if (a[xx][yy] == '#') continue;
			if (step[xx][yy] != 0) continue;
			
			point.x = xx, point.y = yy;
			q.push(point);
			step[xx][yy] = step[front.x][front.y] + 1;
		}
	}
}

int main() {
	cin >> r >> c;
	
	for (int i = 1; i <= r; i ++) {
		for (int j = 1; j <= c; j ++) {
			cin >> a[i][j];
		}
	}

	bfs();
	
	cout << step[r][c] << endl;
	
	return 0;
}
1254 走出迷宫
#include <iostream>
#include <queue>
using namespace std;

const int N = 105;
char a[N][N];
int step[N][N];
int n, m, sx, sy, tx, ty;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

struct node{int x, y;};
queue<node> q;

void bfs() {
	node point, front;
	
	point.x = sx, point.y = sy;
	q.push(point);
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i ++ ) {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
			if (a[xx][yy] == '#') continue;
			if (step[xx][yy] != 0) continue;
			
			point.x = xx, point.y = yy;
			q.push(point);
			step[xx][yy] = step[front.x][front.y] + 1;
		}
	}
}

int main() {
	cin >> n >> m;
	
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			cin >> a[i][j];
			if (a[i][j] == 'S') sx = i, sy = j;
			if (a[i][j] == 'T') tx = i, ty = j;
		}
	}

	bfs();
	
	cout << step[tx][ty] << endl;
	
	return 0;
}
1251 仙岛求药
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

const int N = 105;
char a[N][N];
int step[N][N];
int n, m, sx, sy, tx, ty;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

struct node{int x, y;};
queue<node> q;

void bfs() {
	node point, front;
	
	memset(step, -1, sizeof(step));
	point.x = sx, point.y = sy;
	q.push(point);
	step[sx][sy] = 0;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i ++ ) {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
			if (a[xx][yy] == '#') continue;
			if (step[xx][yy] != -1) continue;
			
			point.x = xx, point.y = yy;
			q.push(point);
			step[xx][yy] = step[front.x][front.y] + 1;
		}
	}
}

int main() {
	
	while (cin >> n >> m) {
		if (n==0 && m==0) return 0;
		for (int i = 1; i <= n; i ++) {
			for (int j = 1; j <= m; j ++) {
				cin >> a[i][j];
				if (a[i][j] == '@') sx = i, sy = j;
				if (a[i][j] == '*') tx = i, ty = j;
			}
		}
		
		bfs();
		cout << step[tx][ty] << endl;
	}

	return 0;
}
1256 献给阿尔吉侬的花束
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

const int N = 210;
char a[N][N];
int step[N][N];
int t, r, c, sx, sy, tx, ty;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

struct node{int x, y;};
queue<node> q;

void bfs() {
	node point, front;
	
	memset(step, -1, sizeof(step));
	point.x = sx, point.y = sy;
	q.push(point);
	step[sx][sy] = 0;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i ++ ) {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 1 || xx > r || yy < 1 || yy > c) continue;
			if (a[xx][yy] == '#') continue;
			if (step[xx][yy] != -1) continue;
			
			point.x = xx, point.y = yy;
			q.push(point);
			step[xx][yy] = step[front.x][front.y] + 1;
		}
	}
}

int main() {
	cin >> t;
	
	while (t --) {
		cin >> r >> c;
		for (int i = 1; i <= r; i ++) {
			for (int j = 1; j <= c; j ++) {
				cin >> a[i][j];
				if (a[i][j]=='S') sx=i, sy=j;	
				if (a[i][j]=='E') tx=i, ty=j;	
			}
		}
		
		bfs();
		
		if (step[tx][ty] == -1) cout << "oop!" <<endl;
		else cout << step[tx][ty] << endl; 
	}

	return 0;
}
1257 Knight Moves
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

const int N = 500;
int step[N][N];
int n, l, si, sj, ei, ej; 
int dx[8] = {-2, -1, +1, +2, +2, +1, -1, -2};
int dy[8] = {+1, +2, +2, +1, -1, -2, -2, -1};

struct node{int x, y;};

void bfs() {
	queue<node> q;
	node point, front;
	
	memset(step, -1, sizeof(step));
	point.x = si, point.y = sj;
	q.push(point);
	step[si][sj] = 0;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 8; i ++ ) {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 0 || xx == l || yy < 0 || yy == l) continue;
			if (step[xx][yy] != -1) continue;
			
			point.x = xx, point.y = yy;
			q.push(point);
			step[xx][yy] = step[front.x][front.y] + 1;
			
			if (xx == ei && yy == ej) return;
		}
	}
}

int main() {
	cin >> n;
	
	while (n--) {
		cin >> l >> si >> sj >> ei >> ej;
		if (si == ei && sj == ej) {
			cout << 0 << endl;
			continue;
		}
		
		bfs();
		cout << step[ei][ej] << endl;
	}
		
	return 0;
}
1255 迷宫问题
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

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

struct node{
	int x, y, pre;
}q[100];

int stk[100];
int head, tail, top;

void bfs() {
	q[++tail] = {0, 0, -1};
	a[0][0] = 1;

	while (head < tail)	{
		for (int i = 0; i < 4; i ++) {
			int xx = q[head+1].x + dx[i];
			int yy = q[head+1].y + dy[i];
			
			if (xx == -1 || xx == 5 || yy == -1 || yy == 5) continue;
			if (a[xx][yy] == 1) continue;
			
			q[++tail] = {xx, yy, head+1};
			a[xx][yy] = 1;
			
			if (xx == 4 && yy == 4) return;
		}
		
		head++;
	}
}

int main(){
	for (int i = 0; i < 5; i ++) {
		for (int j = 0; j < 5; j ++) {
			cin >> a[i][j];
		}
	}

	bfs();

	int i = tail;
	while (i != -1) {
		stk[++top] = i;
		i = q[i].pre;
	}
	
	while (top > 0) {
		i = stk[top];
		printf("(%d, %d)\n", q[i].x, q[i].y);
		top --;
	}
	
	return 0;
}
1253 抓住那头牛
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

const int N = 2e5 + 10;
int n, k, times[N];
queue<int> q;

void bfs(){
	memset(times, -1, sizeof(times));
	
	q.push(n);
	times[n] = 0;
	
	while(q.size()){
		int x = q.front();
		q.pop();
		
		int dx[3] = {x-1, x+1, x*2};
		for(int i = 0; i < 3; i++){
			int xx = dx[i];
			
			if(xx < 0 || xx > 2e5) continue;
			if(times[xx] != -1) continue;
			
			q.push(xx);
			times[xx] = times[x] + 1;
		}
	}
}

int main(){
	cin >> n >> k;
	
	bfs();
	
	cout << times[k] << endl;
	
	return 0;
}
1330 最少步数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int xa, ya, xb, yb;
int step[105][105];
int dx[12] = {-2,-1,+1,+2,+2,+1,-1,-2,-2,+2,+2,-2};
int dy[12] = {+1,+2,+2,+1,-1,-2,-2,-1,+2,+2,-2,-2};

struct node{int x, y;};

void bfs(int x, int y) {
	queue<node> q;
	node point, front;
	
	point.x = x, point.y = y;
	q.push(point);
	step[x][y] = 0;
	
	while (q.size()) {
		front = q.front();
		q.pop();
				
		for (int i = 0; i < 12; i ++) {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 1 || yy < 1 || xx > 100 || yy > 100) continue;
			if (step[xx][yy] != -1) continue;

			point.x = xx, point.y = yy;
			q.push(point);
			step[xx][yy] = step[front.x][front.y] + 1;			
		}
	}	
}

int main() {
	int xa, ya, xb, yb;
	cin >> xa >> ya >> xb >> yb;
	
	memset(step, -1, sizeof(step));
	bfs(xa, ya);
	cout << step[1][1] << endl; 
	
	memset(step, -1, sizeof(step));
	bfs(xb, yb);
	cout << step[1][1] << endl; 

	return 0;
}
1248 Dungeon Master
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 105;
int h, l, w;
int sa, sb, sc, ea, eb, ec;
char a[N][N][N];
int step[N][N][N];
int dx[6] = {+0,+0,+0,+0,+1,-1};
int dy[6] = {-1,+0,+1,+0,+0,+0};
int dz[6] = {+0,+1,+0,-1,+0,+0};
struct node{int x, y, z;};

void bfs() {
	memset(step, -1, sizeof(step));
	queue<node>q;
	node point, front;
	
	point.x = sa, point.y = sb, point.z = sc;
	q.push(point);
	step[sa][sb][sc] = 0;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 6; i ++) {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			int zz = front.z + dz[i];
			
			if (xx<1 || yy<1 || zz<1 || xx>h || yy>l || zz>w) continue;
			if (a[xx][yy][zz] == '#') continue;
			if (step[xx][yy][zz] != -1) continue;
	
			point.x = xx, point.y = yy, point.z = zz;
			q.push(point);
			step[xx][yy][zz] = step[front.x][front.y][front.z] + 1;			
		}
	}	
}

int main() {
	while (cin >> h >> l >> w ) {
		if (h==0 && l==0 && w==0) break;
		
		for (int i = 1; i <= h; i ++) {
			for (int j = 1; j <= l; j ++) {
				for (int k = 1; k <= w; k ++) {
					char ch;
					cin >> ch;
					a[i][j][k] = ch;
					if (ch == 'S') sa=i, sb=j, sc=k;
					if (ch == 'E') ea=i, eb=j, ec=k;
				}
			}
		}
		
		bfs();
		
		if (step[ea][eb][ec] == -1)	puts("Trapped!");
		else printf("Escaped in %d minute(s).\n", step[ea][eb][ec]);
	}

	return 0;
}
1329 细胞
#include <iostream>
#include <queue>
using namespace std;

const int N = 105;
int n, m;
int a[N][N];
bool vst[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

struct node{int x, y;};

void bfs(int x, int y) {
	queue<node> q;
	node point, front;
	
	point.x = x, point.y = y;
	q.push(point);
	vst[x][y] = true;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i ++)  {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
			if (a[xx][yy]==0) continue;
			if (vst[xx][yy]) continue;
			
			point.x = xx, point.y = yy;
			q.push(point);
			vst[xx][yy] = true;
		}
	}
}
	
int main() {
	cin >> n >> m;
	
	char ch;
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			cin >> ch;
			a[i][j] = ch - '0';
		}
	}
	
	int cnt = 0;
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			if (a[i][j]!=0 && vst[i][j]==false) {
				cnt ++ ;
				bfs(i, j);
			}
		}
	}
	
	cout << cnt << endl;
	
	return 0;
}
1249 Lake Counting
#include <iostream>
#include <queue>
using namespace std;

const int N = 200;
int n, m;
char a[N][N];
bool vst[N][N];
int dx[8] = {-1, -1, -1,  0, 0,  1,  1,  1};
int dy[8] = {-1,  0,  1, -1, 1, -1,  0,  1,};

struct node{int x, y;};

void bfs(int x, int y) {
	queue<node> q;
	node point, front;
	
	point.x = x, point.y = y;
	q.push(point);
	vst[x][y] = true;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 8; i ++)  {
			int xx = front.x + dx[i];
			int yy = front.y + dy[i];
			
			if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
			if (a[xx][yy] == '.') continue;
			if (vst[xx][yy]) continue;
			
			point.x = xx, point.y = yy;
			q.push(point);
			vst[xx][yy] = true;
		}
	}
}
	
int main() {
	cin >> n >> m;
	
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			cin >> a[i][j];
		}
	}
	
	int cnt = 0;
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			if (a[i][j] == 'W' && !vst[i][j]) {
				cnt ++ ;
				bfs(i, j);
			}
		}
	}
	
	cout << cnt;
	
	return 0;
}
1250 The Castle
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int N = 100;
int m, n, a[N][N];
bool vst[N][N];
int rooms, cnt, maxx;

//0,1,2,3: 西北东南 
int dx[4] = {0,-1,0,1};
int dy[4] = {-1,0,1,0};

struct node{int x, y;};

void bfs(int x, int y) {
	queue<node> q;
	node point, front;

	point.x = x, point.y = y;
	q.push(point);
	vst[x][y] = true;
	cnt ++;
	
	while (q.size()) {
		front = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i ++) {
			int t = a[front.x][front.y] >> i & 1;	//与运算
			
			if (t == 0) {
				int xx = front.x + dx[i];
				int yy = front.y + dy[i];
				
				if (xx < 1 || yy < 1 || xx > m || yy > n) continue;
				if (vst[xx][yy]) continue;
				
				point.x = xx, point.y = yy;
				q.push(point);
				vst[xx][yy] = true;
				cnt ++;		
			}
		}
	}	
}

int main(){
	cin >> m >> n;
	for (int i = 1; i <= m; i ++) {
		for (int j = 1; j <= n; j ++) {
			cin >> a[i][j];
		}
	}

	for (int i = 1; i <= m; i ++) {
		for (int j = 1; j <= n; j ++) {
			if (!vst[i][j]) {
				rooms ++;
				
				cnt = 0;
				bfs(i, j);				
				maxx = max(maxx, cnt);
			}
		}
	}
	
	cout << rooms << endl << maxx;

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值