#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y;
int bu;
};
int n,m;
char a[105][105];
int vis[105][105];
queue<node> q;
int xa[4]={1,0,-1,0};
int ya[4]={0,1,0,-1};
void bfs(){
q.push(node{1,1,0});
vis[1][1]=1;
while(q.size()){
node t=q.front();
if(t.x==n&&t.y==m){
cout<<t.bu;
exit(0);
}
q.pop();
for(int i=0;i<4;i++){
int fx=t.x+xa[i];
int fy=t.y+ya[i];
if(fx<1||fx>n||fy<1||fy>m||vis[fx][fy]==1||a[fx][fy]){
continue ;
}
if(a[fx][fy]>='A'&&a[fx][fy]<='Z'){
int xx,yy,b=0;
for(int i=1;i<=n;i++){
for(int j=1;i<=m;j++){
if(a[i][j]==a[fx][fy]){
xx=i;
yy=j;
b=1;
break;
}
}
if(b==1) break;
}
vis[fx][fy]=1;
q.push(node{xx,yy,t.bu+1});
}else{
vis[fx][fx]=1;
q.push(node{fx,fy,t.bu+1});
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
bfs();
cout<<0;
return 0;
}
2 个赞
#include<bits/stdc++.h>
using namespace std;
int n,m;
int vis[105][105];
char mp[105][105];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool p=false;
struct node{
int x,y;
int step;
};
queue<node> que;
void bfs(int sx,int sy);
void tp(int &x,int &y);//传送函数 引用
int main(){
//输入
//从1,1开始搜索
//判断到不了的情况
return 0;
}
void tp(int &x,int &y)
{//遍历整张地图,简单粗暴找另一扇门
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(mp[i][j]==mp[x][y]&&(i!=x||j!=y))
{
//更新坐标
x=i;
y=j;
return;
}
}
}
}
void bfs(int sx,int sy)
{
vis[sx][sy]=1;
que.push({sx,sy,0});
while(que.size()!=0){
//取队头丢对头pop()
//cout<<hd.x<<' '<<hd.y<<' '<<hd.step<<endl;
que.pop();
if(hd.x==n&&hd.y==m){
cout<<hd.step<<'\n';
p=true;
return;
}
for(int i=0;i<4;i++){
//判断合法性
vis[nx][ny]=1;
if(isupper(mp[nx][ny])){
tp(nx,ny);
}
que.push({nx,ny,nstep});
}
}
}
1 个赞
简单
1 个赞
暴力一点
1 个赞
你不是没做出吗
1 个赞
有用给个解决方案
1 个赞
好心给你一个吧
1 个赞
谢了
1 个赞