题目描述
作为篮球队教练,你需要从以下名单中选出1 号位至5 号位各一名球员,组成球队的首发阵容。 每位球员担任1号位至5号位时的评分如下表所示。请你计算首发阵容1号位至5号位的评分之和最大可能是多少? 该表格可以参考team.txt
1 97 90 0 0 0 2 92 85 96 0 0 3 0 0 0 0 93 4 0 0 0 80 86 5 89 83 97 0 0 6 82 86 0 0 0 7 0 0 0 87 90 8 0 97 96 0 0 9 0 0 89 0 0 10 95 99 0 0 0 11 0 0 96 97 0 12 0 0 0 93 98 13 94 91 0 0 0 14 0 83 87 0 0 15 0 0 98 97 98 16 0 0 0 93 86 17 98 83 99 98 81 18 93 87 92 96 98 19 0 0 0 89 92 20 0 99 96 95 81思路:深搜,每一种情况都遍历
#include<bits/stdc++.h> using namespace std; /*int a[20][5]={ {97, 90 ,0 ,0, 0}, {92 ,85, 96, 0, 0}, {0 ,0 ,0 ,0 ,93}, {0 ,0 ,0 ,80 ,86}, {89 ,83 ,97 ,0 ,0}, {82 ,86 ,0 ,0 ,0}, {0 ,0, 0 ,87 ,90}, {0 ,97, 96, 0, 0}, {0 ,0, 89 ,0 ,0}, {95 ,99, 0 ,0 ,0}, {0 ,0 ,96 ,97 ,0}, {0 ,0 ,0 ,93 ,98}, {94, 91, 0, 0, 0}, {0 ,83, 87, 0, 0}, {0, 0, 98, 97, 98}, {0 ,0 ,0 ,93 ,86}, {98, 83, 99, 98, 81}, { 93 ,87, 92, 96, 98}, {0 ,0 ,0 ,89 ,92}, {0 ,99 ,96 ,95 ,81} };*/ int a[20][5]; int vis[20]={0}; int maxx=-1; void DFS(int p,int p_sum){ if(p>=5){ if(p_sum>=maxx) maxx=p_sum; return ; } for(int i=0;i<20;i++){ if(vis[i]==1) continue; p_sum+=a[i][p]; vis[i]=1; DFS(p+1,p_sum); vis[i]=0; p_sum-=a[i][p]; } return ; } int main(){ //从文件中读 ifstream ifile; ifile.open("text.txt"); for(int i=0;i<20;i++){ int p; ifile>>p; for(int j=0;j<5;j++){ ifile>>a[i][j]; } } ifile.close(); DFS(0,0);//两个参数,第一个是当前的位置,第二个是当前的和 cout<<maxx<<endl; //往文件中写 ofstream file("out.txt"); file<<maxx<<endl; file.close(); return 0; }下图给出了一个迷宫的平面图,其中标记为1 的为障碍,标记为0 的为可 以通行的地方。 010000 000100 001001 110000 迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。 对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共10 步。其中D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行50 列),请找出一种通过迷宫的方式, 其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。 请注意在字典序中D<L<R<U。
输入
见文件:maze.txt
01010101001011001001010110010110100100001000101010 00001000100000101010010000100000001001100110100101 01111011010010001000001101001011100011000000010000 01000000001010100011010000101000001010101011001011 00011111000000101000010010100010100000101100000000 11001000110101000010101100011010011010101011110111 00011011010101001001001010000001000101001110000000 10100000101000100110101010111110011000010000111010 00111000001010100001100010000001000101001100001001 11000110100001110010001001010101010101010001101000 00010000100100000101001010101110100010101010000101 11100100101001001000010000010101010100100100010100 00000010000000101011001111010001100000101010100011 10101010011100001000011000010110011110110100001000 10101010100001101010100101000010100000111011101001 10000000101100010000101100101101001011100000000100 10101001000000010100100001000100000100011110101001 00101001010101101001010100011010101101110000110101 11001010000100001100000010100101000001000111000010 00001000110000110101101000000100101001001000011101 10100101000101000000001110110010110101101010100001 00101000010000110101010000100010001001000100010101 10100001000110010001000010101001010101011111010010 00000100101000000110010100101001000001000000000010 11010000001001110111001001000011101001011011101000 00000110100010001000100000001000011101000000110011 10101000101000100010001111100010101001010000001000 10000010100101001010110000000100101010001011101000 00111100001000010000000110111000000001000000001011 10000001100111010111010001000110111010101101111000思路:BFS+DFS,先用BFS找出最短路径,找的过程把结点记录下来,我的是放到一个vector数组里面,里面用序号记录从哪里来,然后在用DFS回溯。
写的时候注意输入还有dir的顺序,因为题目要求是DLRU的优先级。
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR #include<bits/stdc++.h> using namespace std; struct A{ int number; //当前结点在ector中的序号 int from_number;//当前结点在ector中的上一个结点的序号 int x; int y; int sept; }start,now; vector<A> v; char a[30][50]; int vis[30][50]; int dir[4][2]={1,0,0,-1,0,1,-1,0}; string s=""; queue<A> q; void DFS(int p){ if(v[p].from_number==-1) return ; DFS(v[p].from_number); int xx=v[p].x-v[v[p].from_number].x; int yy=v[p].y-v[v[p].from_number].y; if(xx==-1&&yy==0) s+="U"; else if(xx==1&&yy==0) s+="D"; else if(xx==0&&yy==1) s+="R"; else if(xx==0&&yy==-1) s+="L"; } int BFS(){ start.x=0; start.y=0; start.sept=0; start.number=0; start.from_number=-1; v.push_back(start); vis[0][0]=1; q.push(start); while(!q.empty()){ now=q.front(); q.pop(); if(now.x==29&&now.y==49){ DFS(now.number); return now.sept; } for(int i=0;i<4;i++){ start.x=now.x+dir[i][0]; start.y=now.y+dir[i][1]; start.sept=now.sept+1; start.number=v.size(); start.from_number=now.number; if(start.x>=0&&start.x<30&&start.y>=0&&start.y<50 &&vis[start.x][start.y]==0&&a[start.x][start.y]=='0'){ q.push(start); v.push_back(start); vis[start.x][start.y]=1; } } } return -1; } int main(){ //从文件中读 ifstream ifile; ifile.open("text.txt"); for(int i=0;i<30;i++){ ifile>>a[i]; } ifile.close(); //注意a的类型,这里定义的是char型 cout<<BFS()<<endl; //往文件中写 ofstream file("out.txt"); file<<s<<endl; file.close(); return 0; }思路二:在结构体里面加路径
#include<bits/stdc++.h> using namespace std; struct A{ int x; int y; int sept; string path=""; }start,now; char a[30][50]; int vis[30][50]; int dir[4][2]={1,0,0,-1,0,1,-1,0}; char dir_c[4]={'D','L','R','U'}; queue<A> q; string BFS(){ start.x=0; start.y=0; start.sept=0; start.path=""; vis[0][0]=1; q.push(start); while(!q.empty()){ now=q.front(); q.pop(); if(now.x==29&&now.y==49){ return now.path; } for(int i=0;i<4;i++){ start.x=now.x+dir[i][0]; start.y=now.y+dir[i][1]; start.sept=now.sept+1; start.path=now.path+dir_c[i]; if(start.x>=0&&start.x<30&&start.y>=0&&start.y<50 &&vis[start.x][start.y]==0&&a[start.x][start.y]=='0'){ q.push(start); vis[start.x][start.y]=1; } } } return "-1"; } int main(){ //从文件中读 ifstream ifile; ifile.open("text.txt"); for(int i=0;i<30;i++){ ifile>>a[i]; } ifile.close(); //往文件中写 ofstream file("out.txt"); file<<BFS()<<endl; file.close(); return 0; }