跳到正文

个人记录

广搜1 迷宫最短路径总结

zqrmon约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年7月7日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 10 分钟

版本

  1. v1当前

广搜1 迷宫最短路径总结

T627222 围圈报数

C++
#include <iostream>
using namespace std;
int main() {
	int c[100005];
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		c[i] = i + 1;
	c[n] = 1;
	int p = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j < m; j++)
			p = c[p];
		cout << p[c] << " ";
		c[p] = c[c[p]];
	}
	return 0;
}

T626711 子序列

C++
#include <bits/stdc++.h>
using namespace std;

bool f(const string& s, const string& t) {
    int i = 0, j = 0;
    while (i < s.size() && j < t.size()) {
        if (s[i] == t[j]) {
            i++;
        }
        j++;
    }
    return i == s.size();
}

int main() {
    string s, t;
    while (cin >> s >> t) {
        if (f(s, t)) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
    return 0;
}

T627569 【基础】迷宫出口

C++
#include <bits/stdc++.h>
using namespace std;
int a[105][105],n,kx,ky,jx,jy;
struct node{
	int x,y;
};
int a1[4]={1,0,-1,0};
int a2[4]={0,1,0,-1};
queue<node> q;
int main() {
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
	}
	cin>>kx>>ky>>jx>>jy;
	if(a[kx][ky]==1){
		cout<<"NO";
		return 0;
	}
	q.push({kx,ky});
	a[kx][ky]=1;
	while(q.size()>0){
		node f=q.front();
		q.pop();
		if(f.x==jx&&f.y==jy){
			cout<<"YES";
			return 0;
		}	
		for(int i=0;i<4;i++){
			if(f.x+a1[i]<1 or f.x+a1[i]>n or f.y+a2[i]<1 or f.y+a2[i]>n or a[f.x+a1[i]][f.y+a2[i]]==1){
				continue;
			}
			a[f.x+a1[i]][f.y+a2[i]]=1;
			q.push({f.x+a1[i],f.y+a2[i]});
		}
	}
	cout<<"NO";
	return 0;
}

T627572 走出迷宫

C++
#include <bits/stdc++.h>
using namespace std;
char a[105][105];
int n,kx,ky,jx,jy,m;
struct node{
    int x,y,s;
};
int a1[4]={1,0,-1,0};
int a2[4]={0,1,0,-1};
queue<node> q;
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'){
                kx=i,ky=j;
            }
            if(a[i][j]=='T'){
                jx=i,jy=j;
            }
        }
    }
    q.push({kx,ky,0});
    a[kx][ky]='#';
    while(!q.empty()){
        node f=q.front();
        q.pop();
        if(f.x==jx&&f.y==jy){
            cout<<f.s;
            return 0;
        }    
        for(int i=0;i<4;i++){
            int nx=f.x+a1[i],ny=f.y+a2[i];
            if(nx<1||nx>n||ny<1||ny>m||a[nx][ny]=='#'){
                continue;
            }
            a[nx][ny]='#';
            q.push({nx,ny,f.s+1});
        }
    }
    return 0;
}

仙岛求药

C++
#include <bits/stdc++.h>
using namespace std;
char a[105][105];
int n, kx, ky, jx, jy, m;
struct node {
	int x, y, s;
};
int a1[4] = {1, 0, -1, 0};
int a2[4] = {0, 1, 0, -1};
queue<node> q;
int main() {
	while (1) {
		cin >> n >> m;
		bool b=0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> a[i][j];
				if (a[i][j] == '@') {
					kx = i, ky = j;
				}
				if (a[i][j] == '*') {
					jx = i, jy = j;
				}
			}
		}
		q.push({kx, ky, 0});
		a[kx][ky] = '#';
		while (q.size()>0) {
			node f = q.front();
			q.pop();
			if (f.x == jx && f.y == jy) {
				cout << f.s;
				b=1;
				break;
			}
			for (int i = 0; i < 4; i++) {
				int nx = f.x + a1[i], ny = f.y + a2[i];
				if (nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] == '#') {
					continue;
				}
				a[nx][ny] = '#';
				q.push({nx, ny, f.s + 1});
			}
		}
		if(b==0){
			cout<<-1;
		}
		cout<<endl;
	}

	return 0;
}

U536924 迷宫之输出最短步数2

C++
#include <bits/stdc++.h>
using namespace std;
char g[505][505];
int d[505][505];
int n,x=1,y=1,m;
struct N{
    int x,y,s;
};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
queue<N> q;

int main() {
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>g[i][j];
            d[i][j]=0;
        }
    }
    
    q.push({x,y,0});
    g[x][y]='1';
    d[x][y]=0;
    
    while(!q.empty()){
        N f=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int nx=f.x+dx[i],ny=f.y+dy[i];
            if(nx<1||nx>n||ny<1||ny>m||g[nx][ny]=='1'){
                continue;
            }
            g[nx][ny]='1';
            d[nx][ny]=f.s+1;
            q.push({nx,ny,f.s+1});
        }
    }
    
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<d[i][j];
            if(j<m) cout<<" ";
        }
        cout<<endl;
    }
    return 0;
}

U536928 迷宫之输出最短步数3

C++
#include <bits/stdc++.h>
using namespace std;
char g[505][505];
int d[505][505];
int n,x=1,y=1,m;
struct node{
	int x,y,s;
};
int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
queue<node> q;
int main() {
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>g[i][j];
			d[i][j]=0;
		}
	}
	
	q.push({x,y,0});
	g[x][y]='1';
	d[x][y]=0;
	
	while(!q.empty()){
		node f=q.front();
		q.pop();
		for(int i=0;i<8;i++){
			int nx=f.x+dx[i],ny=f.y+dy[i];
			if(nx<1||nx>n||ny<1||ny>m||g[nx][ny]=='1'){
				continue;
			}
			g[nx][ny]='1';
			d[nx][ny]=f.s+1;
			q.push({nx,ny,f.s+1});
		}
	}
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cout<<d[i][j];
			if(j<m) cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}
原文