原题链接:https://leetcode.com/problems/perfect-squares/
int numSquares(int n) { //0点到n点的最短距离 queue<int> q; vector<int> dist(n + 1, INT_MAX); q.push(0); dist[0] = 0; while (q.size()) { int t = q.front(); q.pop(); if(t == n) return dist[n]; for (int i = 1; i * i + t <= n; i ++ ) { int j = i * i + t; if (dist[j] >= dist[t] + 1) { dist[j] = dist[t] + 1; q.push(j); } } } return 0; }