代码之代码……
2024-02-18 20:24:41
发布于:浙江
01迷宫
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
struct node{
int x,y;
};
int n,m,f[N][N];
node a[N*N];
int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
char s[N][N];
bool vis[N][N];
void bfs(int x,int y){
a[1].x=x;
a[1].y=y;
int cnt=1;
queue <node> q;
vis[x][y]=1;
q.push(node{x,y});
while(!q.empty()){
node now=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=now.x+dx[i];
int yy=now.y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(s[now.x][now.y]!=s[xx][yy]&&!vis[xx][yy]){
vis[xx][yy]=1;
cnt++;
a[cnt].x=xx,a[cnt].y=yy;
q.push(node{xx,yy});
}
}
}
}
for(int i=1;i<=cnt;i++){
f[a[i].x][a[i].y]=cnt;
vis[a[i].x][a[i].y]=0;
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>s[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!f[i][j]) bfs(i,j);
}
}
while(m--){
int x,y;
cin>>x>>y;
cout<<f[x][y]<<endl;
}
return 0;
}
红黄蓝筷子
代码
#include<bits/stdc++.h>
using namespace std;
int n,m,k,H[205],Y[205],B[205],ans[205][205][205];
int dfs(int x,int y,int z){
if(ans[x][y][z]){
return ans[x][y][z];
}
int a=0,b=0,c=0;
if(x>=1 && y>=1){
a=dfs(x-1,y-1,z)+H[x]*Y[y];
}
if(z>=1 && y>=1){
b=dfs(x,y-1,z-1)+B[z]*Y[y];
}
if(x>=1 && z>=1){
c=dfs(x-1,y,z-1)+H[x]*B[z];
}
ans[x][y][z]=max(max(a,b),c);
return ans[x][y][z];
}
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
cin>>H[i];
}
for(int i=1;i<=m;i++){
cin>>Y[i];
}
for(int i=1;i<=k;i++){
cin>>B[i];
}
sort(H+1,H+n+1);
sort(Y+1,Y+m+1);
sort(B+1,B+k+1);
cout<<dfs(n,m,k);
return 0;
}
小信打怪
代码
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int n, I, J, K, L, T;
map<int , int >f;
int dfs(int n) {
if (n == 1)return L;
if (n == 0)return 0;
if (f.count(n))return f[n];
int x = n * L;
x = min(x, dfs(n / 2) + I + n % 2 * L);
x = min(x, dfs((n + 1) / 2) + I + n % 2 * L);
x = min(x, dfs(n / 3) + J + (n - n / 3 * 3) * L);
x = min(x, dfs((n + 2) / 3) + J + ((n + 2) / 3 * 3 - n) * L);
x = min(x, dfs(n / 5) + K + (n - n / 5 * 5) * L);
x = min(x, dfs((n + 4) / 5) + K + ((n + 4) / 5 * 5 - n) * L);
return f[n] = x;
}
signed main() {
cin >> T;
while (T--) {
cin>>n>>I>>J>>K>>L;
f.clear();
cout << dfs(n) << endl;
}
}
全部评论 1
@majmDZB
2024-02-18 来自 浙江
0好啊,很好啊(乐
2024-02-19 来自 浙江
0
有帮助,赞一个