这次的题目是著名的模拟(水逼)专题
题目难度都不大,思维也不深,就是一个字——烦
同时很多题目都有很多坑点
1608 题意是告诉你一个只有()的字符串(且匹配正确),每个)的左边有几个(,让你求每一对括号中包含了几对括号。
思路很水,模拟出原字符串再开个栈做一次就行了。
CODE
#include<cstdio> using namespace std; const int N=50; int t,n,s[N],stack[N],i,j,x,last,del,top; inline void read(int &x) { x=0; char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); } inline void work(int b,int e) { int l=0,r=0; for (int i=b;i<=e;++i) if (!s[i]) l++; else r++; printf("%d ",l<r?l:r); } int main() { read(t); while (t--) { read(n); last=1; del=0; for (i=1;i<=n;++i) { read(x); for (j=last;j<last+x-del;++j) s[j]=0; s[last+x-del]=1; last=last+x-del+1; del=x; } for (i=1;i<=2*n;++i) if (!s[i]) stack[++top]=i; else work(stack[top--],i); putchar(' '); } return 0; }
2632 就是一堆机器人行走让你求它们会不会撞到或者撞墙。模拟整个过程即可(毫无算法可言)
CODE
#include<cstdio> using namespace std; const int N=105; struct robot { int x,y; char side; }r[N]; struct place { bool b; int num; }f[N][N]; int t,a,b,m,n,i,j,x,num,res; char ch; bool flag; inline void read(int &x) { x=0; char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); } inline void turn_left(int num,int step) { if (r[num].side=='N') res=0; if (r[num].side=='W') res=1; if (r[num].side=='S') res=2; if (r[num].side=='E') res=3; res=(res+step)%4; if (res==0) r[num].side='N'; if (res==1) r[num].side='W'; if (res==2) r[num].side='S'; if (res==3) r[num].side='E'; } inline void turn_right(int num,int step) { if (r[num].side=='N') res=0; if (r[num].side=='E') res=1; if (r[num].side=='S') res=2; if (r[num].side=='W') res=3; res=(res+step)%4; if (res==0) r[num].side='N'; if (res==1) r[num].side='E'; if (res==2) r[num].side='S'; if (res==3) r[num].side='W'; } inline void forward(int num,int step) { if (!step) return; f[r[num].x][r[num].y].b=f[r[num].x][r[num].y].num=0; if (r[num].side=='N') { while (step--) { if (f[r[num].x][++r[num].y].b) { printf("Robot %d crashes into robot %d ",num,f[r[num].x][r[num].y].num); flag=1; return; } if (r[num].y>b) { printf("Robot %d crashes into the wall ",num); flag=1; return; } } } if (r[num].side=='E') { while (step--) { if (f[++r[num].x][r[num].y].b) { printf("Robot %d crashes into robot %d ",num,f[r[num].x][r[num].y].num); flag=1; return; } if (r[num].x>a) { printf("Robot %d crashes into the wall ",num); flag=1; return; } } } if (r[num].side=='S') { while (step--) { if (f[r[num].x][--r[num].y].b) { printf("Robot %d crashes into robot %d ",num,f[r[num].x][r[num].y].num); flag=1; return; } if (!r[num].y) { printf("Robot %d crashes into the wall ",num); flag=1; return; } } } if (r[num].side=='W') { while (step--) { if (f[--r[num].x][r[num].y].b) { printf("Robot %d crashes into robot %d ",num,f[r[num].x][r[num].y].num); flag=1; return; } if (!r[num].x) { printf("Robot %d crashes into the wall ",num); flag=1; return; } } } f[r[num].x][r[num].y].b=1; f[r[num].x][r[num].y].num=num; } int main() { read(t); while (t--) { for (i=0;i<N;++i) for (j=0;j<N;++j) f[i][j].b=f[i][j].num=0; flag=0; read(a); read(b); read(n); read(m); for (i=1;i<=n;++i) read(r[i].x),read(r[i].y),r[i].side=getchar(),f[r[i].x][r[i].y].b=1,f[r[i].x][r[i].y].num=i; while (m--) { read(num); ch=getchar(); read(x); if (flag) continue; if (ch=='L') turn_left(num,x); if (ch=='R') turn_right(num,x); if (ch=='F') forward(num,x); } if (!flag) puts("OK"); } return 0; }
1573 同上,一个机器人在地图上走。让你求要走多久才能走出去或是在循环的圈长。
DFS或模拟都可以,难度也不大
CODE
#include<cstdio> #include<cstring> using namespace std; const int N=20; char a[N][N]; int f[N][N],n,m,x,y,i,j; inline void read(int &x) { x=0; char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); } int main() { for (;;) { read(n); read(m); read(y); x=1; if (n+m+y==0) break; memset(f,0,sizeof(f)); for (i=1;i<=n;++i) { for (j=1;j<=m;++j) a[i][j]=getchar(); getchar(); } f[x][y]=1; for (;;) { if (a[x][y]=='N') { if (x==1) { printf("%d step(s) to exit ",f[x][y]); break; } if (!f[x-1][y]) f[x-1][y]=f[x][y]+1,x--; else { printf("%d step(s) before a loop of %d step(s) ",f[x-1][y]-1,f[x][y]-f[x-1][y]+1); break; } } if (a[x][y]=='E') { if (y==m) { printf("%d step(s) to exit ",f[x][y]); break; } if (!f[x][y+1]) f[x][y+1]=f[x][y]+1,y++; else { printf("%d step(s) before a loop of %d step(s) ",f[x][y+1]-1,f[x][y]-f[x][y+1]+1); break; } } if (a[x][y]=='S') { if (x==n) { printf("%d step(s) to exit ",f[x][y]); break; } if (!f[x+1][y]) f[x+1][y]=f[x][y]+1,x++; else { printf("%d step(s) before a loop of %d step(s) ",f[x+1][y]-1,f[x][y]-f[x+1][y]+1); break; } } if (a[x][y]=='W') { if (y==1) { printf("%d step(s) to exit ",f[x][y]); break; } if (!f[x][y-1]) f[x][y-1]=f[x][y]+1,y--; else { printf("%d step(s) before a loop of %d step(s) ",f[x][y-1]-1,f[x][y]-f[x][y-1]+1); break; } } } } return 0; }
2993&&2996 两道题刚好相反,是一个模拟棋盘的问题。
但主要是有一些没有任何用处的东西(+,-,|)影响了正常的操作。
注意下细节以及题意,多打几次就A了
2993CODE
#include<cstdio> #include<iostream> #include<string> using namespace std; char a[17][33]= { {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, {'|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|',':',':',':','|','.','.','.','|'}, {'+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+','-','-','-','+'}, }; string s1,s2; inline void white(string s) { while (s.size()) { string temp=""; for (int i=0;i<=s.find(",");++i) temp+=s[i]; if (temp[0]>='A'&&temp[0]<='Z') a[(8-temp[2]+'0'+1)*2-1][(temp[1]-'a'+1)*4-2]=temp[0],s.erase(0,4); else a[(8-temp[1]+'0'+1)*2-1][(temp[0]-'a'+1)*4-2]='P',s.erase(0,3); } } inline void black(string s) { while (s.size()) { string temp=""; for (int i=0;i<=s.find(",");++i) temp+=s[i]; if (temp[0]>='A'&&temp[0]<='Z') a[(8-temp[2]+'0'+1)*2-1][(temp[1]-'a'+1)*4-2]=temp[0]-'A'+'a',s.erase(0,4); else a[(8-temp[1]+'0'+1)*2-1][(temp[0]-'a'+1)*4-2]='p',s.erase(0,3); } } int main() { cin>>s1; cin>>s2; s2+=','; if (s1[0]=='W') white(s2); else black(s2); cin>>s1; cin>>s2; s2+=','; if (s1[0]=='W') white(s2); else black(s2); for (int i=0;i<17;++i) { for (int j=0;j<33;++j) putchar(a[i][j]); putchar(' '); } return 0; }
2996CODE
#include<cstdio> #include<iostream> #include<string> using namespace std; string a[10],s; int i,j; int main() { cin>>s; for (i=1;i<=8;++i) cin>>a[i],cin>>s; printf("White: "); for (i=8;i;--i) for (j=0;j<a[i].size();++j) if (a[i][j]=='K') printf("K%c%d",'a'+(j+2)/4-1,8-i+1); for (i=8;i;--i) for (j=0;j<a[i].size();++j) if (a[i][j]=='Q') printf(",Q%c%d",'a'+(j+2)/4-1,8-i+1); for (i=8;i;--i) for (j=0;j<a[i].size();++j) if (a[i][j]=='R') printf(",R%c%d",'a'+(j+2)/4-1,8-i+1); for (i=8;i;--i) for (j=0;j<a[i].size();++j) if (a[i][j]=='B') printf(",B%c%d",'a'+(j+2)/4-1,8-i+1); for (i=8;i;--i) for (j=0;j<a[i].size();++j) if (a[i][j]=='N') printf(",N%c%d",'a'+(j+2)/4-1,8-i+1); for (i=8;i;--i) for (j=0;j<a[i].size();++j) if (a[i][j]=='P') printf(",%c%d",'a'+(j+2)/4-1,8-i+1); printf(" Black: "); for (i=1;i<=8;++i) for (j=0;j<a[i].size();++j) if (a[i][j]=='k') printf("K%c%d",'a'+(j+2)/4-1,8-i+1); for (i=1;i<=8;++i) for (j=0;j<a[i].size();++j) if (a[i][j]=='q') printf(",Q%c%d",'a'+(j+2)/4-1,8-i+1); for (i=1;i<=8;++i) for (j=0;j<a[i].size();++j) if (a[i][j]=='r') printf(",R%c%d",'a'+(j+2)/4-1,8-i+1); for (i=1;i<=8;++i) for (j=0;j<a[i].size();++j) if (a[i][j]=='b') printf(",B%c%d",'a'+(j+2)/4-1,8-i+1); for (i=1;i<=8;++i) for (j=0;j<a[i].size();++j) if (a[i][j]=='n') printf(",N%c%d",'a'+(j+2)/4-1,8-i+1); for (i=1;i<=8;++i) for (j=0;j<a[i].size();++j) if (a[i][j]=='p') printf(",%c%d",'a'+(j+2)/4-1,8-i+1); return 0; }
最后发现我模拟题的代码都是复制来复制去的,导致程序看起来很。。。(臃肿)
下次还是尽量多清晰的分块吧