(1) USACO2.1 Ordered Fractions
列舉 排序即可,注意1/1
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=165,L=1e5; struct fr{ int a,b; fr(int q=0,int w=1):a(q),b(w){} }f[L]; int n,cnt=0; inline bool cmp(fr &x,fr &y){ return (double)x.a/x.b<(double)y.a/y.b; } inline int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } int main(){ cin>>n; for(int i=0;i<=n;i++) for(int j=i+1;j<=n;j++) if(gcd(i,j)==1) f[++cnt]=fr(i,j); sort(f+1,f+1+cnt,cmp); for(int i=1;i<=cnt;i++) printf("%d/%d\n",f[i].a,f[i].b); cout<<"1/1"; }
(2) USACO1.5Number Triangles
基礎DP
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; int r,d[1001][1001],a[1001][1001]; int dp(int i,int j){ if(d[i][j]>=0) return d[i][j]; return d[i][j]=a[i][j]+(i==r?0:max(dp(i+1,j),dp(i+1,j+1))); } int main(){ scanf("%d",&r);//cin>>r; memset(d,-1,sizeof(d)); for(int i=1;i<=r;i++) for(int j=1;j<=i;j++) scanf("%d",&a[i][j]);//cin>>a[i][j]; int ans=-10000000; cout<<dp (1,1); }
(3) USACO1.2 Transformations
模擬
#include <iostream> using namespace std; const int N=12; int n; char a[N][N],r[N][N],t[N][N]; bool ro90(char a[N][N]){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]!=r[j][n-i+1]) return false; return true; } bool ro180(char a[N][N]){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]!=r[n-i+1][n-j+1]) return false; return true; } bool ro270(char a[N][N]){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]!=r[n-j+1][i]) return false; return true; } void img(char a[N][N],char t[N][N]){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) t[i][n-j+1]=a[i][j]; } bool check(char r[N][N],char t[N][N]){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(r[i][j]!=t[i][j]) return false; return true; } int solve(){ if(ro90(a)) return 1; if(ro180(a)) return 2; if(ro270(a)) return 3; img(a,t); if(check(r,t)) return 4; if(ro90(t)) return 5; if(ro180(t)) return 5; if(ro270(t)) return 5; if(check(r,t)) return 6; return 7; } int main(int argc, const char * argv[]) { cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)cin>>a[i][j]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)cin>>r[i][j]; cout<<solve(); }
(4) USACO1.4Mother's Milk
dfs,六種倒水方法,fill簡化
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=25; int vis[N][N][N]; int l[4],ta,tb,tc; int ans[N]; inline void fill(int &a,int &b,int num){ int tmp=min(l[num]-a,b); a+=tmp; b-=tmp; } void dfs(int a,int b,int c){//printf("%d %d %d\n",a,b,c); if(vis[a][b][c]) return; if(a==0) ans[c]=true; vis[a][b][c]=1; ta=a;tb=b;tc=c;//1 fill(ta,tb,1);dfs(ta,tb,tc); ta=a;tb=b;tc=c;//2 fill(ta,tc,1);dfs(ta,tb,tc); ta=a;tb=b;tc=c;//3 fill(tb,ta,2);dfs(ta,tb,tc); ta=a;tb=b;tc=c;//4 fill(tb,tc,2);dfs(ta,tb,tc); ta=a;tb=b;tc=c;//5 fill(tc,ta,3);dfs(ta,tb,tc); ta=a;tb=b;tc=c;//6 fill(tc,tb,3);dfs(ta,tb,tc); } int main(){ cin>>l[1]>>l[2]>>l[3]; dfs(0,0,l[3]); for(int i=0;i<=l[3];i++) if(ans[i]) cout<<i<<" "; }
(5)USACO迷宮
裸DFS
#include<iostream> using namespace std; int n,m,t,sx,sy,fx,fy,ans=0;int x,y; int e[8][8],vis[8][8],dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; void dfs(int x,int y){ if(x<1||y<1||x>n||y>m) return; if(e[x][y]) return; if(x==fx&&y==fy) {ans++;return;} if(vis[x][y]) return; vis[x][y]=1; for(int i=0;i<4;i++) dfs(x+dx[i],y+dy[i]); vis[x][y]=0; } int main(){ cin>>n>>m>>t>>sx>>sy>>fx>>fy; for(int i=0;i<t;i++) {cin>>x>>y;e[x][y]=1;} dfs(sx,sy); cout<<ans; }