我怎麼就學不會\(DP\)呢node
連接數組
\(BFS\),雖然老師讓用\(DP\)寫法作。(蒟蒻不會,因此就用了\(BFS\),ide
咱們定義結構體。idea
元素有當前的和\((val)\),當前向上的數字\((up)\),已經走的步數\((tmp)\)spa
一開始\(val=0,tmp=0,up=1\),放入隊列,再定義一個記錄步數的數組,初始化爲\(-1\),而後就能夠爆搜了code
struct node{ int val,tmp,up; }now,net;
int sum[maxn]; struct node{ int val,tmp,up; }now,net; inline void bfs(){ now.val=0; now.tmp=0; now.up=1; queue<node> q; q.push(now); while(q.size()){ now=q.front(); q.pop(); if(now.val<maxn){ for(int i=1;i<=6;i++){ if(now.up==i||now.up+i==7||sum[now.val+i]!=-1) continue; net.tmp=now.tmp+1; net.val=now.val+i; net.up=i; sum[net.val]=net.tmp; q.push(net); } } } } int main(){ memset(sum,-1,sizeof sum); bfs(); int T=read(); while(T--){ int n=read(); printf("%d\n",sum[n]); } return 0; }
\[ The \quad End \]隊列
\[ \text{我要記住你的樣子,像魚記住水的擁抱,像雲在天空中停靠;}\\ \text{我要忘了你的樣子,魚忘了海的味道,全部夢和煩惱-《像魚》王貳浪} \]get