结构体的构造函数!+利用bfs数图中的联通块数、利用bfs求图中从起点到终点最短距离

bfs的模板:

void bfs(x){
    queue<type> q;
    q.push(x);
    while(!q.empty()){
        取出队首;
        访问队首;
        弹出队首(q.pop());
        将符合条件的、未曾入对的下一层结点入队,并设置为已入队;
    }
}

连通块问题:

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
#pragma warning(disable:4996)
//算法笔记P276
const int maxn = 110;
int n, m, g[maxn][maxn], inq[maxn][maxn],ans=0;//n行m列
struct node {
	int x, y;
	node(int _x, int _y) :x(_x), y(_y) {}
};
bool isok(int x, int y) {
	if (x < 0 || y < 0 || x == n || y == m)return false;
	if (g[x][y] == 0 || inq[x][y] == 1)return false;
	return true;
}
int ax[4] = { 0,0,-1,1 };
int ay[4] = { 1,-1,0,0 };
void bfs(int x, int y) {
	queue<node> q;
	node head(x, y);
	q.push(head);
	inq[x][y] = 1;
	while (!q.empty()) {
		node now = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int newx = now.x + ax[i];
			int newy = now.y + ay[i];
			if (isok(newx,newy)) {
				node temp(newx, newy);
				q.push(temp);
				inq[newx][newy] = 1;
			}
		}
	}
}

int main() {
	memset(inq, 0, sizeof(inq));
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			scanf("%d", &g[i][j]);
	for(int i=0;i<n;i++)
		for (int j = 0; j < m; j++) {
			if (g[i][j] == 1 && inq[i][j] == 0) {
				ans++;
				bfs(i, j);
			}
		}
	printf("%d\n", ans);
	return 0;
}
/*
6 7
0 1 1 1 0 0 1
0 0 1 0 0 0 0 
0 0 0 0 1 0 0 
0 0 0 1 1 1 0
1 1 1 0 1 0 0 
1 1 1 1 0 0 0
*/

走迷宫(输出起点s到终点t的最短步数,其中*为墙壁),用到了结构体的构造函数和默认构造函数,注意!有了自己写的构造函数就必须初始化刚定义的结构体,如果想不初始化,则要手动加上默认构造函数!!!

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
#pragma warning(disable:4996)
//算法笔记P280
const int maxn = 110;
int n, m, ans=0,inq[maxn][maxn];//n行m列
char g[maxn][maxn];
struct node {
	int x, y,step;//位置坐标x,y 层数step
	node(int _x, int _y,int _step) :x(_x), y(_y), step(_step){}
	node() {}
}s,t;
bool isok(int x, int y) {
	if (x < 0 || y < 0 || x == n || y == m)return false;
	if (g[x][y] == '*')return false;
	return true;
}
int ax[4] = { 0,0,-1,1 };
int ay[4] = { 1,-1,0,0 };
int bfs() {
	queue<node> q;
	q.push(s);
	while (!q.empty()) {
		node now = q.front();
		q.pop();
		if (now.x == t.x&&now.y == t.y)return now.step;
		for (int i = 0; i < 4; i++) {
			int newx = now.x + ax[i];
			int newy = now.y + ay[i];
			if (isok(newx,newy)) {
				node temp(newx, newy,now.step+1);
				q.push(temp);
				inq[newx][newy] = 1;
			}
		}
	}
	return -1;
}

int main() {
	memset(inq, 0, sizeof(inq));
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) {
		getchar();
		for (int j = 0; j < m; j++) {
			g[i][j] = getchar();
			if (g[i][j] == 'S') { s.x = i; s.y = j; }
			if (g[i][j] == 'T') { t.x = i; t.y = j; }
		}
		g[i][m + 1] = '\0';
	}
	s.step = 0;
	printf("%d\n", bfs());
	return 0;
}
/*
5 5
.....
.*.*.
.*S*.
.***.
...T*
*/

猜你喜欢

转载自blog.csdn.net/ur_ytii/article/details/112977181