代码仅供参考,不保证全对。
#include
using namespace std;
int main(){
int n,m,a;
cin>>n>>m>>a;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
arr1[(i+1)%n][(j+1)%m]=arr[i][j];
}
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=0;k<a;k++){
if(arr1[i][j][k]!=arr[i][j][k])ans++;
}
}
}
cout<<ans<<endl;
return 0;
}
#include
using namespace std;
const int N=1e5+5;
int n,x,y;
vector<int>edges[N];
int len,vis[N];
void dfs(int u, int dis){
len=max(len,dis);
for(int i=0;i<edges[u].size();i++){
int v=edges[u][i];
if(vis[v])continue;
vis[v]=1;
dfs(v,dis+1);
}
}
void init(){
memset(vis,0,sizeof(vis));
vis[x]=1;
vis[y]=1;
}
int main(){
cin>>n;
for(int i=2;i<=n;i++){
int x;
cin>>x;
edges[x].push_back(i);
edges[i].push_back(x);
}
cin>>x>>y;
int ans=0;
init();
dfs(x,0);
ans=len;
len=0;
init();
dfs(y,0);
ans+=len;
cout<<ans+1<<endl;
return 0;
}
/*
7
1 2 3 4 5 3
3 7
*/
dp[i]=min(dp[i],dp[i-k]+cost(i-k+1,i))
cost(i-k+1,i)
指i-k+1
到i
放到同一个果篮需要的花费#include
using namespace std;
const int N=1e4+5;
int dp[N];
int arr[N];
int main(){
int n,m,s;
cin>>n>>m>>s;
for(int i=1;i<=n;i++)cin>>arr[i];
for(int i=1;i<=n;i++){
int mi = arr[i];
int ma = arr[i];
for(int k=1;k<=min(i,m);k++){
mi = min(mi,arr[i-k+1]);
ma = max(ma,arr[i-k+1]);
int cost = k*((mi+ma)/2)+s;
if(dp[i]==0||cost+dp[i-k]<dp[i]){
dp[i]=cost+dp[i-k];
}
}
}
cout<<dp[n]<<endl;
return 0;
}
/*
6 4 3
1 4 5 1 4 1
*/
#include
using namespace std;
const int N = 500+5;
int n,m,k;
int p[N],q[N];
int mp[N][N];
int s1,s2,t1,t2;
int vis[N][N];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int ans;
int cal_dis(int x, int y){
int dis = abs(p[0]-x)+abs(q[0]-y);
for(int pp=1;pp<k;pp++){
dis=min(dis,abs(p[pp]-x)+abs(q[pp]-y));
}
return dis;
}
void dfs(int x,int y,int dis){
if(x==t1&&y==t2){
ans=max(ans,dis);
return;
}
for(int i=0;i<4;i++){
int nx=dx[i]+x;
int ny=dy[i]+y;
if(nx<1||nx>n||ny<1||ny>m||mp[nx][ny]||vis[nx][ny])continue;
vis[nx][ny]=1;
dp[nx][ny]=min(dis,cal_dis(nx,ny));
dfs(nx,ny,min(dis,cal_dis(nx,ny)));
vis[nx][ny]=0;
}
}
int dp[N][N];
int main(){
cin>>n>>m>>k;
for(int i=0;i<k;i++){
cin>>p[i]>>q[i];
mp[p[i]][q[i]]=1;
}
cin>>s1>>s2>>t1>>t2;
vis[s1][s2]=1;
dfs(s1,s2,cal_dis(s1,s2));
cout<<ans<<endl;
return 0;
}