题目:
https://codeforces.com/group/d3FEQxSUNi/contest/335515/problem/D
最开始是1朝上
#include<stdio.h> #include<queue> #include<string.h> using namespace std; const int mm=1e4+7; int dis[mm],vis[mm]={0}; int d[7][5]={{0,0,0,0},{2,3,4,5},{1,3,4,6},{1,2,5,6},{1,2,5,6},{1,3,4,6},{2,3,4,5}}; struct node { int top; int num; }; node nod[mm]; queue<node> q; void bfs() { dis[1]=0; vis[0]=1; node start; start.top=1; start.num=0; q.push(start); while(!q.empty()) { node st=q.front(); q.pop(); for(int i=0;i<4;i++) { int res=st.num+d[st.top][i]; if(!vis[res]&&res<=10000&&res>=1) { vis[res]=1; dis[res]=dis[st.num]+1; node w; w.top=d[st.top][i]; w.num=res; q.push(w); } } } } int main() { int t; scanf("%d",&t); memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); bfs(); while(t--) { int n; scanf("%d",&n); { if(vis[n]==0) printf("-1\n"); else printf("%d\n",dis[n]); } } }