1319
//题意:最短路+方向
//在搜索的时候加上方向;
//良辰的代码 #include<stdio.h> #include<string.h> #include<queue> using namespace std; char map[200][200]; int use[200][200][4],n,m; int dir[5][2]={0,0,-1,0,1,0,0,-1,0,1};//原点,上下左右 struct node{ int x; int y; int count; int di; }; int BFS(int ix,int iy){ node tem; tem.x=ix; tem.y=iy; tem.count=0; tem.di=1; use[ix][iy][1]=1; queue<node> que; que.push(tem);////起点入队 while(!que.empty()) { node now=que.front(); que.pop(); node next=now; if(map[now.x][now.y]=='T') return now.count; next.x=now.x+dir[now.di][0];// next.y=now.y+dir[now.di][1];// if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&!use[next.x][next.y][next.di]&&map[next.x][next.y]!='#') { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now;// if(now.di==1||now.di==2)//1 { next.di=3; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now; next.di=4; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } } next=now; if(now.di==3||now.di==4)//2 { next.di=2; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now; next.di=1; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } } } return -1; } int main() { int T; scanf("%d",&T); while(T--) { memset(use,0,sizeof(use)); int ix=0,iy=0; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",map[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(map[i][j]=='S'){ ix=i;iy=j;break; } int ans; ans=BFS(ix,iy); printf("%d\n",ans); } return 0; }
想的太多,做的太少。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步