P2033 2AC+10WA+1MLE 求助
  • 板块灌水区
  • 楼主haiqian
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/4 22:10
  • 上次更新2024/8/13 13:47:48
查看原帖
P2033 2AC+10WA+1MLE 求助
679630
haiqian楼主2024/8/4 22:10

rt,

题面link

记录详情

#include<iostream>
#include<string.h>
using namespace std;

char map[15][15] = {};
char re[15] = {'1','^','<','v','>'};
int stfx[256] = {};
int fangx[15] = {0,-1,0,1,0};
int fangy[15] = {0,0,-1,0,1};

int posx=1,posy=1,posfx,step=0;
int nextx,nexty,pux,puy;
string cmd,fx;

bool check(int chx,int chy){//判断越界
	if(chx>=1 && chx<=8 && chy>=1 && chy<=8){
		return true;
	}
	return false;
}

void push(int x,int y,int pufx){
	//下一步的x,y
	pux = x+fangx[pufx];
	puy = y+fangy[pufx];
	//cout<<posx<<" "<<posy<<" "<<x<<" "<<y<<" "<<pufx<<"\n";
	if(check(pux,puy)==0) return ;//如果越界,返回
	if(isalpha(map[pux][puy])){//如果是棋子(任意字母)
		push(pux,puy,pufx);//递归找到下一个棋子(如果有的话)
	}
	map[pux][puy] = map[x][y];//移动棋子
	map[x][y] = '.';//原来棋子变空
	posx = pux,posy = puy;//把人的新位置给posx,posy
}

//上左下右
int main(){
	stfx['^'] = 1,stfx['<'] = 2,stfx['v'] = 3,stfx['>'] = 4;
	for(int i=1;i<=8;++i){
		for(int j=1;j<=8;++j){
			cin>>map[i][j];
			if(map[i][j]!='.' && isalpha(map[i][j])==0){//如果是人
				posx = i,posy = j;
				posfx = stfx[(int)map[i][j]];
			}
		}
	}
	while(1){
		cin>>cmd;
		if(cmd=="#") break;
		if(cmd=="move"){//如果是走路
			cin>>step;
			for(int i=1;i<=step;++i){
				push(posx,posy,posfx);//走step步,每次开始推
			}
		}else if(cmd=="turn"){//如果是转弯
			cin>>fx;
			if(fx=="left"){
				posfx++;
			}
			if(fx=="right"){
				posfx--;
			}
			if(fx=="back"){
				posfx+=2;
			}
			if(posfx<=0) posfx+=4;
			if(posfx>=5) posfx-=4;
		}
	}
	map[posx][posy] = re[posfx];//让当前朝向对齐posfx里的朝向
	for(int i=1;i<=8;++i){
		for(int j=1;j<=8;++j){
			cout<<map[i][j];
		}
		cout<<"\n";
	}
	return 0;
}

求助各位

2024/8/4 22:10
加载中...