1 条题解
-
0
luogu
```cpp #include<queue> #include<cstdio> #include<cstring> #include<algorithm> #define re register #define IN inline using namespace std; const int maxn=50; const int inf=0x7f7f7f; int dx[8]={2,2,-2,-2,1,-1,1,-1},dy[8]={-1,1,-1,1,2,2,-2,-2}; struct node{ int to,next; }a[100010]; int len,head[maxn*maxn],x,y,st,ed; void adde(int x,int y){a[++len].to=y;a[len].next=head[x];head[x]=len;} int n,m,mp[maxn][maxn],d[maxn*maxn],p[maxn][maxn]; long long f[maxn*maxn]; bool b[maxn][maxn],vis[maxn*maxn]; IN void dfs(int o,int x,int y){ b[x][y]=1; for(re int k=0;k<8;k++){ int xx=x+dx[k],yy=y+dy[k]; if(xx<1 || yy<1 || xx>n || yy>m || b[xx][yy])continue; if(mp[xx][yy]==1) dfs(o,xx,yy); else b[xx][yy]=1,adde(o,p[xx][yy]); } } IN void init(){ for(re int i=1;i<=n;i++) for(re int j=1;j<=m;j++) p[i][j]=(i-1)*m+j; for(re int i=1;i<=n;i++){ for(re int j=1;j<=m;j++){ scanf("%d",&mp[i][j]); if(mp[i][j]==3) st=p[i][j]; if(mp[i][j]==4) ed=p[i][j]; } } for(re int i=1;i<=n;i++) for(re int j=1;j<=m;j++){ if(mp[i][j]==0||mp[i][j]==3){ memset(b,0,sizeof(b)); dfs(p[i][j],i,j); } } } queue<int> q; IN void spfa(){ memset(d,63,sizeof(d)); d[st]=0,vis[st]=f[st]=1; q.push(st); while(!q.empty()){ x=q.front(); q.pop(); vis[x]=0; for(re int k=head[x];k;k=a[k].next){ int v=a[k].to; if(d[v]>d[x]+1){ d[v]=d[x]+1;f[v]=f[x]; if(!vis[v]){ vis[v]=1; q.push(v); } } else if(d[v]==d[x]+1) f[v]+=f[x]; } } } int main(){ scanf("%d%d",&n,&m); init();spfa(); if(d[ed]<inf)printf("%d\n%lld\n",d[ed]-1,f[ed]); else printf("-1\n"); return 0; }
- 1
信息
- ID
- 602
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 6
- 标签
- 递交数
- 3
- 已通过
- 3
- 上传者