本文主要是介绍模拟小练,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
昨天和今天做了一下模拟。有些感触。总结一下。
第一题:
Poj 1068 点击打开链接
题目和括号配对有点联系,从而就想到了用栈。一开始没有想到用,突然就来了灵感A了。算是用了正确的数据结构吧。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;const int N=45;
struct Bracksts{char c;int d;
}b[N];int main(){int T;scanf("%d",&T);while(T--){int n,p[N],top=0;char s[N];scanf("%d",&n);p[0]=0;for(int i=1;i<=n;i++)scanf("%d",&p[i]);for(int i=1;i<=n;i++){for(int j=1;j<=p[i]-p[i-1];j++)s[top++]='(';s[top++]=')';}s[top]='\0';for(int i=0;i<2*n;i++){b[i].c=s[i];b[i].d=i;}stack<Bracksts> st;st.push(b[0]);for(int i=1;i<2*n;i++){if(b[i].c=='(') st.push(b[i]);else {Bracksts deom=st.top();st.pop();printf("%d ",(b[i].d-deom.d+1)/2);}}printf("\n");}return 0;
}
第二题:
Poj 2632 点击打开链接
这道题,让我写了不少时间,很是费力,冗余也特别多。我想模拟应该及时的调试应该有好的效果吧!
#include <cstdio>
#include <cstring>
#include <algorithm>
#define CLR(arr,val) memset(arr,val,sizeof(arr))
using namespace std;const int N=105;
const char d[]={'N','W','S','E'};
int Map[N][N];
int n,m,a,b;
struct Robot{int x,y;int dir;
}robot[N];int Move(int q,char p,int r){if(p=='L') {robot[q].dir=(robot[q].dir+r)%4;
// printf("%d %d %d\n",robot[q].x,robot[q].y,robot[q].dir);return -1;}if(p=='R') {robot[q].dir=(robot[q].dir-r+4000)%4;return -1;}if(p=='F'){if(robot[q].dir==0){//Nfor(int i=1;i<=r;i++){if(robot[q].x+i>b) return 0;if(Map[robot[q].x+i][robot[q].y])return Map[robot[q].x+i][robot[q].y];}Map[robot[q].x][robot[q].y]=0;robot[q].x=robot[q].x+r;}if(robot[q].dir==1){//Wfor(int i=1;i<=r;i++){if(robot[q].y-i<1) return 0;if(Map[robot[q].x][robot[q].y-i])return Map[robot[q].x][robot[q].y-i];}Map[robot[q].x][robot[q].y]=0;robot[q].y=robot[q].y-r;}if(robot[q].dir==2){//Sfor(int i=1;i<=r;i++){if(robot[q].x-i<1) return 0;if(Map[robot[q].x-i][robot[q].y])return Map[robot[q].x-i][robot[q].y];}Map[robot[q].x][robot[q].y]=0;robot[q].x=robot[q].x-r;}if(robot[q].dir==3){//Efor(int i=1;i<=r;i++){if(robot[q].y+i>a) return 0;if(Map[robot[q].x][robot[q].y+i])return Map[robot[q].x][robot[q].y+i];}Map[robot[q].x][robot[q].y]=0;robot[q].y=robot[q].y+r;}Map[robot[q].x][robot[q].y]=q;}return -1;
}int main(){
// freopen("1.txt","r",stdin);int T;scanf("%d",&T);while(T--){CLR(Map,0);scanf("%d%d",&a,&b);scanf("%d%d",&n,&m);int i;char demo;for(i=1;i<=n;i++){scanf("%d%d %c",&robot[i].y,&robot[i].x,&demo);for(int j=0;j<4;j++){if(d[j]==demo)robot[i].dir=j;}Map[robot[i].x][robot[i].y]=i;}int No_Robot[N],rep[N];char action[N];for(int j=1;j<=m;j++)scanf("%d %c%d",&No_Robot[j],&action[j],&rep[j]);for(i=1;i<=m;i++){int flag=Move(No_Robot[i],action[i],rep[i]);if(flag==0){printf("Robot %d crashes into the wall\n",No_Robot[i]);break;}//0代表撞到墙了啊.else if(flag>0){printf("Robot %d crashes into robot %d\n",No_Robot[i],flag);break;//>0其他代表其他的Robot上了.}}if(i>m) printf("OK\n");}return 0;
}
该题需要注意的是:方向的正确性,一定要保证转弯时候正确性。
第三题:
Poj 1573 点击打开链接
这道题在hd上A过,又A了一次。比较简单。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;const int N=13;
int Vis[N][N],R,C,start;
char map[N][N];void solve(){int nowx=1,nowy=start,step=0;Vis[1][start]=0;while(1){if(map[nowx][nowy]=='N') nowx=nowx-1;else if(map[nowx][nowy]=='S') nowx=nowx+1;else if(map[nowx][nowy]=='W') nowy=nowy-1;else if(map[nowx][nowy]=='E') nowy=nowy+1;step++;
// for(int i=1;i<=R;i++){
// for(int j=1;j<=C;j++)
// printf("%d ",Vis[i][j]);
// printf("\n");
// }
// printf("-----------\n");if(nowx<1||nowx>R||nowy<1||nowy>C){printf("%d step(s) to exit\n",step);break;}else if(Vis[nowx][nowy]!=-1) {printf("%d step(s) before a loop of %d step(s)\n",Vis[nowx][nowy],step-Vis[nowx][nowy]);break;}Vis[nowx][nowy]=step;}
}
int main(){while(scanf("%d%d%d",&R,&C,&start)&&(R||C||start)){memset(Vis,-1,sizeof(Vis));for(int i=1;i<=R;i++){for(int j=1;j<=C;j++)cin>>map[i][j];}solve();}return 0;
}
第四题:
Poj 2993&&1993 点击打开链接
这两道题的输入输出交换了一下,处理繁杂。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;const int N=100;
const char Type[]={'K','Q','R','B','N','P'};
struct Pieces{int x;char y,type;
}W[N],B[N];bool cmpW(Pieces x1,Pieces y1){int numberx,numbery;for(int i=0;i<=5;i++){if(x1.type==Type[i]) numberx=i;if(y1.type==Type[i]) numbery=i;}if(numberx<numbery) return true;else if(numberx==numbery){if(x1.x<y1.x) return true;else if(x1.x==y1.x&&x1.y<y1.y) return true;else return false;}else return false;
}bool cmpB(Pieces x1,Pieces y1){int numberx,numbery;for(int i=0;i<=5;i++){if(x1.type==Type[i]) numberx=i;if(y1.type==Type[i]) numbery=i;}if(numberx<numbery) return true;else if(numberx==numbery){if(x1.x>y1.x) return true;else if(x1.x==y1.x&&x1.y<y1.y) return true;else return false;}else return false;
}int main(){char demo;int topW=0,topB=0;for(int i=17;i>=1;i--){for(int j=0;j<33;j++){cin>>demo;if(demo>='A'&&demo<='Z'){W[topW].x=i/2;W[topW].y=j/4+'a';W[topW].type=demo;topW++;}else if(demo>='a'&&demo<='z'){B[topB].x=i/2;B[topB].y=j/4+'a';B[topB].type=demo-32;topB++;}}}sort(W,W+topW,cmpW);sort(B,B+topB,cmpB);printf("White: ");for(int i=0;i<topW;i++){if(W[i].type!='P')printf("%c%c%d%c",W[i].type,W[i].y,W[i].x,i==topW-1?'\n':',');else printf("%c%d%c",W[i].y,W[i].x,i==topW-1?'\n':',');}printf("Black: ");for(int i=0;i<topB;i++){if(B[i].type!='P')printf("%c%c%d%c",B[i].type,B[i].y,B[i].x,i==topB-1?'\n':',');else printf("%c%d%c",B[i].y,B[i].x,i==topB-1?'\n':',');}
}
通过上面的几道题。可以发现
第一,模拟的题意都是不难理解的,而且思路比较清晰;
第二,模拟写起来比较繁杂,考验代码能力;
第三。写模拟,好的数据结构+强的代码能力,对于模拟就不会有任何畏惧。
模拟需要多写,才能锻炼出来。恩,以后要多些这些。
这篇关于模拟小练的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!