USACO 1.4 牛奶
题目:https://www.luogu.org/problemnew/show/P1215
令三元组(i,j,k)表示该情况是否出现,即可防止死循环
注意0这个答案有可能是合法的,答案要加上
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; inline int read() { int ans = 0,op = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') op = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { (ans *= 10) += ch - '0'; ch = getchar(); } return ans * op; } bool ok[21][21][21]; bool e[21]; int a,b,c; void dfs(int i,int j,int k) { if(i < 0 || j < 0 || k < 0 || i > a || j > b || k > c) return; if(ok[i][j][k]) return; if(i == 0) e[k] = 1; ok[i][j][k] = 1; dfs(i - (b - j),b,k); dfs(0,i + j,k); dfs(i,j - (c - k),c); dfs(i,0,j + k); dfs(a,j,k - (a - i)); dfs(i + k,j,0); dfs(a,j - a + i,k); dfs(i + j,0,k); dfs(i,b,k - b + j); dfs(i,j + k,0); dfs(i - c + k,j,c);//1 -> 3 dfs(0,j,i + k); } int main() { a = read(),b = read(),c = read(); dfs(0,0,c); for(int i = 0;i <= c;i++) if(e[i]) printf("%d ",i); return 0; }