17111 Football team
时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0
题型: 编程题 语言: C++;C
Description
As every one known, a football team has 11 players . Now, there is a big problem in front of the Coach Liu. The final contest is getting closer. Who is the center defense, the full back or the forward? ...... There are n wonderful players for n positions in the team and Coach Liu know everyone's abilities at different positions in matches. Assume that the team's power is the sum of the abilities of all n players according to their positions, could you help Coach Liu to find out the max power his team can get?
输入格式
The first line is an integer n(n<11). Followed by n rows. Each row has n integer (0 to 1000) which represents the abilities of one player at different positions.
输出格式
The max power.
输入样例
10 4 6 3 3 4 5 7 4 9 0 5 9 3 4 6 1 7 3 9 3 1 5 8 0 5 4 2 7 9 3 4 6 9 4 7 3 7 9 5 4 2 0 1 3 2 5 8 4 6 2 1 5 8 4 2 6 8 0 4 2 1 4 2 6 8 9 4 2 6 8 1 2 9 5 6 4 2 7 5 7 2 4 7 5 8 5 3 2 6 4 2 4 6 4 8 7 3 5 7 3
输出样例
76
简单回溯:wa在由于now是全局变量,在每一次到达结束状态时,now的值不会因递归返回而改变(不像局部变量),而我是希望在递归枚举所有情况是now的值应该是返回上一状态的,
故因 加一句:now-=mat[i][j]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n; int mat[12][12]; int vis[12]; int now=0; int maxr=0; void dfs( int cur) { int i; if (cur==n) { if (now>maxr) maxr=now;} else for (i=0;i<10;++i){ if (!vis[i]){ now+=mat[cur][i]; //尝试选择第cur行第i列的数 vis[i]=1; dfs(cur+1); vis[i]=0; now-=mat[cur][i]; //谨记,now需随递归返回原来的值 } } } int main() { memset (vis,0, sizeof (vis)); memset (mat,0, sizeof (mat)); scanf ( "%d" ,&n); for ( int i=0;i<n;++i) for ( int j=0;j<n;++j) scanf ( "%d" ,&mat[i][j]); dfs(0); printf ( "%d\n" ,maxr); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧