题目
思路
- 直接套bfs模板
代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e3+5,INF=0x3f3f3f3f;
struct Node{
int x,y,s;
}t,t1;
char graph[N][N];
bool vis[N][N];
queue<Node> q;
int dx[4]={0,0,1,1};
int dy[4]={-1,1,0,0};
int sx,sy,ex,ey;
int n,m;
void bfs(){
sx=1,sy=1,ex=n,ey=m;//起始坐标点 结束坐标点
t.x=sx,t.y=sy,t.s=0;//初始化这个点
q.push(t);//入队
vis[sx][sy]=1;//标记已经走过这条路
while(!q.empty()){
t = q.front();
q.pop();
if(t.x==ex && t.y==ey){
cout << t.s << "\n";
return;
}
for(int i=0;i<4;i++){
int u=t.x+dx[i],v=t.y+dy[i];
if(u<1||u>n||v<1||v>m||vis[u][v]||graph[u][v]==graph[t.x][t.y]) continue;
vis[u][v]=1;
t1.x=u,t1.y=v,t1.s = t+1;
q.push(t1);
}
}
cout << "-1\n";
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n>> m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin >>graph[i][j];
cin >> n;
}
标签:sy,sx,vis,int,graph,矩阵,小红,t1,牛客
From: https://www.cnblogs.com/rzbooks/p/18066614