22分,求调
查看原帖
22分,求调
189610
APolaris楼主2024/11/26 19:50
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>

using namespace std;

char m[12][12];//              N,E,S,W
int t,f[3],c[3]; //[0]记录方向{0,1,2,3},[1]->x,[2]->y 
void move(int dir,int x,int y,bool flag){ //flag判断是f or c,f为true,c为false 
	if(dir==0){ //北 
		if(m[x-1][y]=='*'){ //障碍 
			if(flag==true) 
				f[0]=1;
			else c[0]=1;
		}
		else{
			if(flag==true)//农夫
				f[1]--;
			else c[1]--;
		}
	}
	else if(dir==1){//东 
		if(m[x][y+1]=='*'){ //障碍 
			if(flag==true)//农夫
				f[0]=2;
			else c[0]=2;
		}
		else{
			if(flag==true)//农夫
				f[2]++;
			else c[2]++;
		}
	}
	else if(dir==2){ //南 
		if(m[x+1][y]=='*'){ //障碍 
			if(flag==true)//农夫
				f[0]=3;
			else c[0]=3;
		}
		else{
			if(flag==true)//农夫
				f[1]++;
			else c[1]++;
		}
	}
	else{ //西 
		if(m[x][y-1]=='*'){ //障碍 
			if(flag==true)//农夫
				f[0]=0;
			else c[0]=0;
		}
		else{
			if(flag==true)//农夫
				f[2]--;
			else c[2]--;
		}
	}
}

bool judge(){
	if(c[1]==f[1]&&c[2]==f[2])
		return false;
	return true;
}

int main()
{
	memset(m,'*',sizeof m);
	for(int i=1;i<=10;i++)
	{
		for(int j=1;j<=10;j++)
		{
			scanf("%c",&m[i][j]);
			if(m[i][j]=='F'){
				f[1]=i;
				f[2]=j;
			}
			else if(m[i][j]=='C'){
				c[1]=i;
				c[2]=j;
			}
		}
		char c=getchar();  //存入换行符 
	}
	while(judge()){
		move(f[0],f[1],f[2],true);
		move(c[0],c[1],c[2],false);
		t++;
		if(t>9999){
			cout<<0;
			return 0;
		}
	}
	printf("%d\n",t);
	return 0;
}
2024/11/26 19:50
加载中...