目录
A - Shuffle'm Up
题目链接:
HRBU 2021年暑期训练阶段二Day3 - Virtual Judge
题意:
给定两个字符串A,B以及一个最终结果的字符串,要求将A,B两个串交叉结合起来,优先级为B的先进行插入接着再是A插入,如果插入的结果和最终结果不同,则把现有的串前半部分作为A的新串,后半部分作为B的新串,然后重复以上步骤,如果还是达不到结果则输出-1,否则输出步数。例如:
A串:ABC B串:DEF 结果串:DAEBFC
该结果可以一次完成,因此答案就是1 1
做法:
先把两个串进行交叉插入,接着先进行第一次判断,如果不满足条件继续重复题目里的步骤,把字符串前后分开,整合成两个新串,其次我们要判断这个交叉插入得到的串是否出现过,因为要是这个串已经出现过了,就说明这个题目误解,也就是输出-1,用于标记字符串是否出现过,我们使用map表示一个关系的映射,用来统计这个串是否出现过。最后代码中出现了一个string的函数substr是用于将字符串截取子串的一个函数,有兴趣可以了解一下,不推荐经常使用,时间复杂度较高
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<string.h> #include<cstdlib> #include<fstream> #include<queue> #include<stack> #include<map> #include<set> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; const ll maxn=110; int main() { int T; cin>>T; for(int i=1; i<=T; i++) { int n; string s1,s2,str,s; map<string,int> mp; cin>>n; cin>>s1>>s2>>str; s=str; int ans=0; while(1) { int len=0; for(int j=0; j<n; j++) { s[len++]=s2[j]; s[len++]=s1[j]; } s[len]='\0'; ans++; if(s==str) { cout<<i<<" "<<ans<<endl; break; } else if(mp[s]==1) { cout<<i<<" "<<-1<<endl; break; } else { mp[s]=1; s1=s.substr(0,n); s2=s.substr(n,n); } } } return 0; }
B - Prime Path
题目链接:
HRBU 2021年暑期训练阶段二Day3 - Virtual Judge
题意:
给定两个四位数的数字a,b,要求把素数a变成素数b,并且变化过程中相邻的两个数字之间只能有一个数位上的数字不同,同时这些数字都得是素数并且不能重复出现(素数:除了本身和1不存在任何因子的数字)
做法:
对数字进行搜索,搜索期间需要约束两个条件:1、得到的这个数字得是一个素数。
2、这个数字和上一个数字之间只有一个数位上的数字不同,且这个数字之前不能出现过。
判断素数,写一个√ 级别的就ok,关于数字是否出现过,这个只需要做一个标记数组标记一下就好了,最大的问题是该怎么保证数字之间相差只有一位,不如我们把原来的数字上的每一位都记录下来,然后把进行四次循环,每一次循环都去改变一个数位上的数字,这样不就保证的数位上只相差一位数字,需要注意的是因为数据范围必须是四位数,所以千位遍历时不可以出现千位为0的情况
#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> #include <stack> #include <queue> #include <vector> #include <bitset> #include <set> #include <utility> using namespace std; typedef long long ll; const int maxn = (int)1e5 + 5; bool str[10010],vi[maxn]; int m; struct Node { int x; int step; Node(int xx=0,int Step=0):x(xx),step(Step){}; }N[maxn]; void prime() { memset(str,false,sizeof(str)); str[1]=true; for(int i=2;i*i<10010;i++) { if(!str[i]) { for(int j=i*i;j<=10010;j+=i) str[j]=true; } } } int bfs(int n) { queue<Node> q; N[0].x=n; N[0].step=0; q.push(N[0]); vi[n]=true; while(!q.empty()) { Node Front=q.front(); Node now; q.pop(); int num[4]; int t=0; while(Front.x) { num[t]=Front.x%10; Front.x=Front.x/10; t++; } int nape; for(int i=1;i<=9;i++) { nape=i*1000+num[2]*100+num[1]*10+num[0]; if(vi[nape]==false&&!str[nape]) { now.x=nape; now.step=Front.step+1; vi[nape]=true; q.push(now); if(now.x==m) return now.step; } } for(int i=0;i<=9;i++) { nape=i*100+num[3]*1000+num[1]*10+num[0]; if(vi[nape]==false&&!str[nape]) { now.x=nape; now.step=Front.step+1; vi[nape]=true; q.push(now); if(now.x==m) return now.step; } } for(int i=0;i<=9;i++) { nape=i*10+num[3]*1000+num[2]*100+num[0]; if(vi[nape]==false&&!str[nape]) { now.x=nape; now.step=Front.step+1; vi[nape]=true; q.push(now); if(now.x==m) return now.step; } } for(int i=0;i<=9;i++) { nape=i+num[3]*1000+num[2]*100+num[1]*10; if(vi[nape]==false&&!str[nape]) { now.x=nape; now.step=Front.step+1; vi[nape]=true; q.push(now); if(now.x==m) return now.step; } } } } int main() { prime(); int n,T; cin>>T; while(T--) { cin>>n>>m; memset(vi,false,sizeof(vi)); memset(N,0,sizeof(N)); if(n==m) cout<<0<<endl; else cout<<bfs(n)<<endl; } return 0; }
C - Function Run Fun
题目链接:
HRBU 2021年暑期训练阶段二Day3 - Virtual Judge
题意:
给定了一个类似于三元函数的式子,并且告诉了你计算的规律,要求算出给定三个参数时算出式子的结果
做法:
类似于dp的状态转移方程(没想到吧!我也没想到!)只需要按照题目里给的式子去编写一个递归函数就可以了,稍微注意一点就是三个参数同时为-1时才是退出输入(表示被坑!QAQ)
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<string.h> #include<cstdlib> #include<fstream> #include<queue> #include<stack> #include<map> #include<set> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; const ll maxn=810; int a,b,c; int dp[25][25][25]; int w(int x,int y,int z) { if(x<=0||y<=0||z<=0) return 1; if(x>20||y>20||z>20) return w(20,20,20); if(dp[x][y][z]) return dp[x][y][z]; if(x<y&&y<z) return dp[x][y][z]=w(x,y,z-1)+w(x,y-1,z-1)-w(x,y-1,z); return dp[x][y][z]=w(x-1,y,z)+w(x-1,y-1,z)+w(x-1,y,z-1)-w(x-1,y-1,z-1); } int main() { while(~scanf("%d%d%d",&a,&b,&c)) { if(a==-1&&b==-1&&c==-1) break; printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c)); } return 0; }
D - 蜘蛛牌
题目链接:
HRBU 2021年暑期训练阶段二Day3 - Virtual Judge
做法:
用数组去标记牌是否已经有序,例如:v[3]=0,而v[4~6]=1,那么3这张牌直接去找7就可以了,因为4,5,6,7都已经连在一起了
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<string.h> #include<cstdlib> #include<fstream> #include<queue> #include<stack> #include<map> #include<set> #define INF 0x3f3f3f3f3 using namespace std; typedef long long ll; const ll maxn=1e5+10; int T,a[15],pos[15]; int ans; void dfs(int t,int sum) { if(sum>ans) return; if(t==9) { ans=ans<sum?ans:sum; return; } for(int i=1;i<=9;i++) { if(!a[i]) { a[i]=1; for(int j=i+1;j<=10;j++) { if(!a[j]) { dfs(t+1,sum+abs(pos[j]-pos[i])); break; } } a[i]=0; } } } int main() { cin>>T; while(T--) { memset(pos,0,sizeof(pos)); for(int i=1;i<=10;i++) { int x; cin>>x; pos[x]=i; } ans=1e9; dfs(0,0); cout<<ans<<endl; } return 0; }
E - Nightmare Ⅱ
题目链接:
HRBU 2021年暑期训练阶段二Day3 - Virtual Judge
题意:
给你一个迷宫的地图,里面存在三种角色,一个M一个G还有两个Z
Z会吃掉M和G,且Z无视墙壁X的阻挡,Z每秒移动两个单元格,M每次移动三个单元格,G每次移动一个单元格,需要求出M找到G的最短时间否则输出-1。
做法:
双向BFS,对于两个Z以及M,G都进行一次搜索
题目注意点:M,G都是可以移动的,Z是可以穿过墙壁的,并且运动的顺序是Z先动,然后才是M,G所以Z的运动轨迹我们其实可以不用考虑的,只要判定M,G与Z之间的距离,就可以判断出M,G是否遇上Z。
关于距离,这里我们引入一个新的名词:曼哈顿距离
曼哈顿距离的计算公式:假设某个点(x,y)到某个点(x1,y1),两点间的曼哈顿距离即是|x-x1|+|y-y1|
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<string.h> #include<cstdlib> #include<fstream> #include<queue> #include<stack> #include<map> #include<set> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; const ll maxn=810; int n,m,mx,my,gx,gy,z1x,z1y,z2x,z2y,step; int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; char mp[maxn][maxn]; bool vis[2][maxn][maxn]; struct Node { int x,y; }; queue<Node> Q[2]; bool judge(int x,int y) { //step*2则是因为鬼魂每次移动两个单元格 //曼哈顿距离其实就是当前位置到鬼魂位置的最短距离,但是是基于单元格的最短距离而非直线距离 if((abs(x-z1x)+abs(y-z1y))<=2*step||(abs(x-z2x)+abs(y-z2y))<=2*step)//判断当前位置到达鬼魂处的曼哈顿距离 return 0; return 1; } int bfs(int t) { int cnt=Q[t].size();//只需要搜索某一时刻的点 while(cnt--) { Node now=Q[t].front(); Q[t].pop(); if(!judge(now.x,now.y))//鬼魂先移动,所以先判定是否有遇到鬼魂的可能 continue; for(int i=0; i<4; i++) { int xx=now.x+dir[i][0]; int yy=now.y+dir[i][1]; if(xx<0||xx>=n||yy<0||yy>=m)//判断是否越界 continue; if(vis[t][xx][yy]||mp[xx][yy]=='X') continue; if(!judge(xx,yy)) continue; if(vis[t^1][xx][yy])//判断另一个人是否到达过这个位置,到达了说明两人相遇,输出结果 return 1; vis[t][xx][yy]=true; Q[t].push((Node){xx,yy}); } } return 0; } int slove() { step=0; memset(vis,false,sizeof(vis)); while(!Q[0].empty())//循环之中需要初始化数据防止后续数据受到影响 Q[0].pop(); while(!Q[1].empty()) Q[1].pop(); vis[0][mx][my]=vis[1][gx][gy]=true; Q[0].push((Node){mx,my}); Q[1].push((Node){gx,gy}); while(!Q[0].empty()||!Q[1].empty()) { step++; if(bfs(0)||bfs(0)||bfs(0)||bfs(1))//男孩一次走三个单元格,女孩一次一个单元格 return step; } return -1; } int main() { int T; scanf("%d",&T); while(T--) { int zn=0; scanf("%d%d",&n,&m); memset(vis,false,sizeof(vis)); for(int i=0; i<n; i++) { scanf("%s",mp[i]); for(int j=0; j<m; j++) { if(mp[i][j]=='M')//人物位置 mx=i,my=j; if(mp[i][j]=='Z')//鬼魂起点 { zn++; if(zn==1) z1x=i,z1y=j; else z2x=i,z2y=j; } if(mp[i][j]=='G')//人物位置 gx=i,gy=j; } } int ans=slove(); printf("%d\n",ans); } return 0; }
F - Color the ball
题目链接:
HRBU 2021年暑期训练阶段二Day3 - Virtual Judge
做法:
把对于某一个点的操作变成一个区间端点的操作,会大大的降低时间复杂度
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<string.h> #include<cstdlib> #include<fstream> #include<queue> #include<stack> #include<map> #include<set> using namespace std; typedef long long ll; const ll maxn=1e5+10; int N,a,b; int ans[maxn]; int main() { while(cin>>N&&N) { memset(ans,0,sizeof(ans)); for(int i=1;i<=N;i++) { cin>>a>>b; ans[a]+=1; ans[b+1]-=1; } int num=0; for(int i=1;i<=N;i++) { num+=ans[i]; if(i!=N) cout<<num<<" "; else cout<<num<<endl; } } return 0; }
G - Tempter of the Bone
题目链接:
HRBU 2021年暑期训练阶段二Day3 - Virtual Judge
题意:
一只小狗不小心进入了一个迷宫,迷宫里的方格会消失,然而迷宫的门之后在t秒打开一下,然后关闭
做法:
对于狗子到门的距离,求出一个曼哈顿距离,然后判断这个距离是否为偶数且不小于0即可,关于曼哈顿距离上一题有介绍
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<string.h> #include<cstdlib> #include<fstream> #include<queue> #include<stack> #include<map> #include<set> using namespace std; typedef long long ll; const ll maxn=7; int N,M,T,sx,sy,ex,ey,flag; char mp[maxn][maxn]; bool vis[maxn][maxn]; int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; struct Node { int x,y,step; }; bool judge(int a,int b) { if(a>=1&&a<=N&&b>=1&&b<=M) return true; return false; } /*int bfs(int a,int b) { queue<Node> Q; vis[a][b]=true; Q.push((Node){a,b,0}); while(!Q.empty()) { Node now=Q.front(); Q.pop(); if(now.x==ex&&now.y==ey) return now.step; for(int i=0;i<4;i++) { int xx=now.x+dir[i][0]; int yy=now.y+dir[i][1]; if(judge(xx,yy)&&!vis[xx][yy]&&mp[xx][yy]=='.') { vis[xx][yy]=true; Q.push((Node){xx,yy,now.step+1}); } } } }*/ void dfs(int x,int y,int t) { if(flag) return; if(x==ex&&y==ey) { if(t==T) flag=1; return; } int dis=(T-t)-(abs(ex-x)+abs(ey-y)); if(dis<0||dis%2) return; for(int i=0;i<4;i++) { int xx=x+dir[i][0]; int yy=y+dir[i][1]; if(xx<1||xx>M||yy<1||yy>M) continue; if(mp[xx][yy]=='X') continue; if(!vis[xx][yy]) { vis[xx][yy]=true; dfs(xx,yy,t+1); vis[xx][yy]=false; } } } int main() { while(cin>>N>>M>>T&&N&&M&&T) { for(int i=1;i<=N;i++) { for(int j=1;j<=M;j++) { cin>>mp[i][j]; if(mp[i][j]=='S') sx=i,sy=j; if(mp[i][j]=='D') ex=i,ey=j; } } //cout<<sx<<" "<<sy<<" "<<ex<<" "<<ey<<endl; memset(vis,false,sizeof(vis)); vis[sx][sy]=true; //int ans=bfs(sx,sy); flag=0; dfs(sx,sy,0); if(!flag) cout<<"NO"<<endl; else cout<<"YES"<<endl; } return 0; }