1 条题解
-
0
#include <bits/stdc++.h> using namespace std; const int maxn=1010; const int INF=0x3f3f3f3f; typedef long long LL; typedef unsigned long long ull; //还是这个讲的比较好https://www.cnblogs.com/xsl19/p/12380226.html const int M=1e6+10; int dis[M],vis[M]; int n,m,t,tot; int head[M]; char mp[510][510]; struct node{ int to,w,nex; }ed[M<<1]; void adde(int x,int y,int w){ ed[++tot].to=y; ed[tot].w=w; ed[tot].nex=head[x]; head[x]=tot; } int id(int x,int y){ return (x)*(m+1)+y; } void bfs(int x){ memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); dis[0]=0; deque<int> q; //双端队列 q.push_back(x); while(!q.empty()){ int u=q.front(); q.pop_front(); if(vis[u]) continue; vis[u]=1; for(int i=head[u];i;i=ed[i].nex){ int tt=ed[i].to; if(vis[tt]) continue; if(dis[tt]>dis[u]+ed[i].w){ dis[tt]=dis[u]+ed[i].w; if(!ed[i].w) q.push_front(tt); //如果是正向的话,就插在队首 else q.push_back(tt); } } } } int main(){ scanf("%d",&t); while(t--){ scanf("%d %d",&n,&m); tot=0; memset(head,0,sizeof(head)); for(int i=1;i<=n;i++) scanf("%s",mp[i]+1); if((n+m)%2) { printf("NO SOLUTION\n"); continue; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(mp[i][j]=='\\'){ adde(id(i-1,j-1),id(i,j),0); //起点--终点原本的方向 adde(id(i,j),id(i-1,j-1),0); //终点--起点原本的方向 adde(id(i,j-1),id(i-1,j),1); //终点--起点 反向 adde(id(i-1,j),id(i,j-1),1); //起点--终点 反向 } else{ adde(id(i,j-1),id(i-1,j),0); adde(id(i-1,j),id(i,j-1),0); adde(id(i,j),id(i-1,j-1),1); adde(id(i-1,j-1),id(i,j),1); } } } bfs(0); printf("%d\n",dis[id(n,m)]); } return 0; }
- 1
信息
- ID
- 904
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者