[USACO11OPEN]Corn Maze S
https://www.luogu.com.cn/problem/P1825
#include <bits/stdc++.h> using namespace std; const int N=350; char g[N][N]; int n,m,d[N][N],sx,sy; bool v[N][N]; int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0}; void cs(int &x,int &y){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(g[i][j]==g[x][y]&&(i!=x||j!=y)) { x=i; y=j; return; } } } } int main() { cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++){ cin>>g[i][j]; if(g[i][j]=='@'){ sx=i; sy=j; } } } queue<pair<int,int> >q; q.push(make_pair(sx,sy)); v[sx][sy]=1; while(q.size()) { pair<int,int>now=q.front(); q.pop(); //cout<<now.first<<" "<<now.second<<" "<<d[now.first][now.second]<<endl; if(g[now.first][now.second]=='=') { cout<<d[now.first][now.second]; return 0; } if(g[now.first][now.second]>='A'&&g[now.first][now.second]<='Z') { int t=d[now.first][now.second]; //cout<<"before"<<now.first<<" "<<now.second<<endl; cs(now.first,now.second); // cout<<"after"<<now.first<<" "<<now.second<<endl; d[now.first][now.second]=t; } // cout<<now.first<<" "<<now.second<<" "<<d[now.first][now.second]<<endl; for(int k=0;k<4;k++) { int nx=now.first+dx[k]; int ny=now.second+dy[k]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&g[nx][ny]!='#'&&!v[nx][ny]) { v[nx][ny]=1; d[nx][ny]=d[now.first][now.second]+1; q.push(make_pair(nx,ny)); } } } return 0; }
合并果子 / [USACO06NOV] Fence Repair G
https://www.luogu.com.cn/problem/P1090
#include <bits/stdc++.h> using namespace std; int ans,n; priority_queue<int,vector<int>,greater<int> >q; int main() { cin>>n; int x; for(int i=1;i<=n;i++) { cin>>x; q.push(x); } while(q.size()>1){ int a=q.top(); q.pop(); int b=q.top(); q.pop(); ans+=(a+b); q.push(a+b); } cout<<ans; return 0; }
[USACO3.1]总分 Score Inflation
https://www.luogu.com.cn/problem/P2722
#include <bits/stdc++.h> using namespace std; int a[10005],b[10005],f[10005]; int n,m; int main() { cin>>m>>n; for(int i=1;i<=n;i++) cin>>a[i]>>b[i]; for(int i=1;i<=n;i++){ for(int j=b[i];j<=m;j++) f[j]=max(f[j],f[j-b[i]]+a[i]); } cout<<f[m]; return 0; }