模擬題大放送(5題)poj1608 + poj2632 + poj1573 + poj2993 + poj2996


第一道:POJ 1608

題意:給出P序列--每個數字表示一個右括號左邊有幾個左括號。要求輸出W序列--每個右括號往左遇到幾個左括號才能找到和其相匹配的左括號。

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
int r,l,ind;
int a[500];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,x;		scanf("%d",&n);
		r = l = 0;
		ind = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d",&x);
			for (int j = r; j < x; j++)
			{
				a[ind++] = 1;//left
			}
			r = x;
			a[ind++] = 0;
		}
		int num = 0,tmp;
		for (int i = 0; i < ind; i++)
		{
			tmp = 1;
			if(a[i] == 0){
				num ++;
				for (int j = i-1; j >= 0; j--)
				{
					if(a[j] == 1){
						a[j] = -1;
						if(num < n)printf("%d ",tmp);
						else printf("%d\n",tmp);
						break;
					}
					else if(a[j] == 0) tmp++;
				}
			}
		}
	}
	return 0;
}

第二道:POJ 2632

題意:模擬機器人的運動,判斷是否有衝突

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
int dir[5][2] = {{0,0},{0,1},{-1,0},{0,-1},{1,0}};
struct robot{
	int x,y;
	int face;
}r[105];//1 N ,3 S, 2 W, 4 E
int map[105][105];
int main(){
	int t,a,b,n,m;
	scanf("%d",&t);
	while(t--){
		memset(map,0,sizeof(map));
		scanf("%d%d",&a,&b);
		scanf("%d%d",&n,&m);
		char tmp;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d %d %c",&r[i].x,&r[i].y,&tmp);
			map[r[i].x][r[i].y] = i;
			if(tmp == 'N')r[i].face = 1;
			else if(tmp == 'S')r[i].face = 3;
			else if(tmp == 'W')r[i].face = 2;
			else if(tmp == 'E')r[i].face = 4;
		}
		int ind,rep,flag = 0;
		for (int i = 1; i <= m; i++)
		{
			scanf("%d %c %d",&ind,&tmp,&rep);
			if(flag){continue;}
			if(tmp == 'L'){
				for (int k = 0; k < rep; k++)
				{
					if(r[ind].face == 4)r[ind].face = 1;
					else r[ind].face ++;
				}
			}else if(tmp == 'R'){
				for (int k = 0; k < rep; k++)
				{
					if(r[ind].face == 1)r[ind].face = 4;
					else r[ind].face --;
				}
			}else if(tmp == 'F'){
				map[r[ind].x][r[ind].y] = 0;
				for (int k = 0; k < rep; k++)
				{
					r[ind].x += dir[r[ind].face][0];
					r[ind].y += dir[r[ind].face][1];
					if(r[ind].x < 1 || r[ind].x > a){flag = 1;break;}
					if(r[ind].y < 1 || r[ind].y > b){flag = 1;break;}
					if(map[r[ind].x][r[ind].y]!=0){
						flag = 2;
						printf("Robot %d crashes into robot %d\n",ind,map[r[ind].x][r[ind].y]);
						break;
					}
				}
				if(flag == 1)printf("Robot %d crashes into the wall\n",ind);
				if(flag == 0)map[r[ind].x][r[ind].y] = ind;
			}
		}
		if(flag == 0)printf("OK\n");
	}
	return 0;
}

第三道:POJ 1573

題意:也是模擬機器人的運動,判斷是安全走出 還是在途中循環了,循環的要輸出 每次循環幾步。

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
int dir[5][2] = {{0,0},{-1,0},{0,-1},{1,0},{0,1}};
char map[15][15];
int a,b;
int dirc(char tmp){
	if(tmp == 'N')	   return 1;
	else if(tmp == 'S')return 3;
	else if(tmp == 'W')return 2;
	else if(tmp == 'E')return 4;
	return 0;
}
bool out(int x,int y){
	if(x < 1 || x > a)return 1;
	if(y < 1 || y > b)return 1;
	return 0;
}
int vis[15][15];
int main(){
	int x,y,st;
	while(scanf("%d %d %d%*c",&a,&b,&st)!=EOF,a|b|st){
		memset(map,'0',sizeof(map));
		for (int i = 1; i <= a; i++)		scanf("%s",map[i]+1);
		memset(vis,0,sizeof(vis));
		x = 1,y = st;
		int flag = 1,step = 0,d;
		while(flag == 1){
			if(vis[x][y] == 0){
				vis[x][y] = 1;
				d = dirc(map[x][y]);
				x += dir[d][0];		y += dir[d][1];
				step++;
				if(out(x,y)){
					flag = 0;
					printf("%d step(s) to exit\n",step);
					break;
				}
			}else {//loop
				while(1){
					if(vis[x][y] < 2){//再從開始loop的地方跑一遍,吧vis加到2
						vis[x][y]++;
						d = dirc(map[x][y]);
						x += dir[d][0];		y += dir[d][1];
					}else break;
				}

				int newstep = 0;
				int nx = 1,ny = st;
				while(1){//就可以再從最初的起點跑,跑到vis爲2的地方,就知道了真相o(* ̄▽ ̄*)ゞ 
					if(vis[nx][ny] == 2){
						printf("%d step(s) before a loop of %d step(s)\n",newstep,step-newstep);
						break;
					}
					d = dirc(map[nx][ny]);
					nx += dir[d][0];		ny += dir[d][1];
					newstep++;
				}
				flag = 0;
			}
		}		
	}
	return 0;
}

第四道:POJ 2993

題意:給你國際象棋裏黑白棋子的位置,輸出棋盤。

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<ctype.h>
using namespace std;
#define INF 0x3f3f3f3f
int q[9] = {1,1,3,5,7,9,11,13,15};
int p[9] = {1,2,6,10,14,18,22,26,30};
char map[35][35] = {
	"+---+---+---+---+---+---+---+---+",
	"|...|:::|...|:::|...|:::|...|:::|",
	"+---+---+---+---+---+---+---+---+",
	"|:::|...|:::|...|:::|.:.|:::|...|",
	"+---+---+---+---+---+---+---+---+",
	"|...|:::|...|:::|...|:::|...|:::|",
	"+---+---+---+---+---+---+---+---+",
	"|:::|...|:::|...|:::|...|:::|...|",
	"+---+---+---+---+---+---+---+---+",
	"|...|:::|...|:::|...|:::|...|:::|",
	"+---+---+---+---+---+---+---+---+",
	"|:::|...|:::|...|:::|...|:::|...|",
	"+---+---+---+---+---+---+---+---+",
	"|...|:::|...|:::|...|:::|...|:::|",
	"+---+---+---+---+---+---+---+---+",
	"|:::|...|:::|...|:::|...|:::|...|",
	"+---+---+---+---+---+---+---+---+",
};
int main(){
	char w[200]={},b[200]={};
	gets(w);
	int length = strlen(w),x,y;
	for (int i = 7; i < length; i++)
	{
		if(isupper(w[i]) && (w[i-1]==' '||w[i-1]==',')){
			x = w[++i]-'a'+1;
			y = '8'-w[++i]+1;
			map[q[y]][p[x]] = w[i-2];
		}else if(islower(w[i]) && w[i-1] == ','){
			x = w[i]-'a'+1;
			y = '8'-w[++i]+1;
			map[q[y]][p[x]] = 'P';
		}
	}
	gets(b);
	for (int i = 7; i < length; i++)
	{
		if(isupper(b[i]) && (b[i-1]==' '||b[i-1]==',')){
			x = b[++i]-'a'+1;
			y = '8'-b[++i]+1;
			map[q[y]][p[x]] = b[i-2]+32;
		}else if(islower(b[i]) && b[i-1] == ','){
			x = b[i]-'a'+1;
			y = '8'-b[++i]+1;
			map[q[y]][p[x]] = 'p';
		}
	}

	for (int i = 0; i < 17; i++)
	{
		printf("%s\n",map[i]);
	}
	//system("pause");
	return 0;
}
 
第五題:POJ 2996

題意:和上題反過來,給出棋盤,輸出棋子位置,注意有輸出格式要求。

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<ctype.h>
using namespace std;
#define INF 0x3f3f3f3f
const int length = 33;
char col[9] = {0,'a','b','c','d','e','f','g','h'};
int row[9] = {0,1,2,3,4,5,6,7,8};
struct player{
	int k[2],q[2],r[2][2],b[2][2],n[2][2],p[8][2];
}W,B;
int main(){
	int num = 8,i,j;
	char tmp[35],a[35];
	int k[2]={0,0},q[2]={0,0},r[2]={0,0},b[2]={0,0},n[2]={0,0},p[2]={0,0};
	while(num > 0){
		gets(tmp);
		gets(a);
		for (i = 2,j = 1; i < length; i+= 4,j++)
		{
			if(islower(a[i])){
				if(a[i] == 'k'){
					B.k[0] = num;	B.k[1] = j;
					k[0]++;
				}else if(a[i] == 'q'){
					B.q[0] = num;	B.q[1] = j;
					q[0]++;
				}else if(a[i] == 'r'){
					B.r[r[0]][0] = num;	B.r[r[0]][1] = j;
					r[0]++;
				}else if(a[i] == 'b'){
					B.b[b[0]][0] = num;	B.b[b[0]][1] = j;
					b[0]++;
				}else if(a[i] == 'n'){
					B.n[n[0]][0] = num;	B.n[n[0]][1] = j;
					n[0]++;
				}else if(a[i] == 'p'){
					B.p[p[0]][0] = num;	B.p[p[0]][1] = j;
					p[0]++;
				}
			}else if(isupper(a[i])){
				if(a[i] == 'K'){
					W.k[0] = num;	W.k[1] = j;
					k[1]++;
				}else if(a[i] == 'Q'){
					W.q[0] = num;	W.q[1] = j;
					q[1]++;
				}else if(a[i] == 'R'){
					W.r[r[1]][0] = num;	W.r[r[1]][1] = j;
					r[1]++;
				}else if(a[i] == 'B'){
					W.b[b[1]][0] = num;	W.b[b[1]][1] = j;
					b[1]++;
				}else if(a[i] == 'N'){
					W.n[n[1]][0] = num;	W.n[n[1]][1] = j;
					n[1]++;
				}else if(a[i] == 'P'){
					W.p[p[1]][0] = num;	W.p[p[1]][1] = j;
					p[1]++;
				}
			}
		}
		num --;
	}
	gets(tmp);
	//print
	
	printf("White: ");
	for (i = 0; i < k[1]; i++)printf("K%c%d", col[W.k[1]],row[W.k[0]]);
	for (i = 0; i < q[1]; i++)printf(",Q%c%d",col[W.q[1]],row[W.q[0]]);
	for (i = 0; i < r[1]; i++)printf(",R%c%d",col[W.r[i][1]],row[W.r[i][0]]);
	for (i = 0; i < b[1]; i++)printf(",B%c%d",col[W.b[i][1]],row[W.b[i][0]]);
	for (i = 0; i < n[1]; i++)printf(",N%c%d",col[W.n[i][1]],row[W.n[i][0]]);
	//輸出格式。
	for (i = 0; i < p[1]; i++)
	{
		for (int k = 0; k < p[1]-i; k++)
		{
			if(W.p[k][0] > W.p[k+1][0]){
				swap(W.p[k][0],W.p[k+1][0]);swap(W.p[k][1],W.p[k+1][1]);
			}else if(W.p[k][0] == W.p[k+1][0] && W.p[k][1] > W.p[k+1][1]){
				swap(W.p[k][0],W.p[k+1][0]);swap(W.p[k][1],W.p[k+1][1]);
			}
		}
	}
	for (i = 0; i < p[1]; i++)printf(",%c%d", col[W.p[i][1]],row[W.p[i][0]]);
	printf("\n");

	printf("Black: ");
	for (i = 0; i < k[0]; i++)printf("K%c%d", col[B.k[1]],row[B.k[0]]);
	for (i = 0; i < q[0]; i++)printf(",Q%c%d",col[B.q[1]],row[B.q[0]]);
	for (i = 0; i < r[0]; i++)printf(",R%c%d",col[B.r[i][1]],row[B.r[i][0]]);
	for (i = 0; i < b[0]; i++)printf(",B%c%d",col[B.b[i][1]],row[B.b[i][0]]);
	for (i = 0; i < n[0]; i++)printf(",N%c%d",col[B.n[i][1]],row[B.n[i][0]]);
	for (i = 0; i < p[0]; i++)printf(",%c%d", col[B.p[i][1]],row[B.p[i][0]]);
	printf("\n");

	//system("pause");
	return 0;
}

╮(╯▽╰)╭,5道簡單的模擬題,本以爲能AK,現實就是打臉,編的慢,慢,慢!!!
發佈了119 篇原創文章 · 獲贊 10 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章