33玄关
查看原帖
33玄关
881317
spessert楼主2024/4/26 11:25
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct DIR{
	int x,y;
}dir;
queue<DIR> quman,qucow;
char mp[15][15];
int main(){
	int x=-1,y=0;//北
	quman.push({x,y});
	qucow.push({x,y});
	x=0,y=1;//东 
	quman.push({x,y});
	qucow.push({x,y});
	x=1,y=0;//南 
	quman.push({x,y});
	qucow.push({x,y});
	x=0,y=-1;//西 
	quman.push({x,y});
	qucow.push({x,y});
	for(int i=1;i<=10;i++){
		for(int j=1;j<=10;j++){
			cin>>mp[i][j];
		}
	}
	//找到牛和人的位置
	int cowx,cowy,perx,pery;
	for(int i=1;i<=10;i++){
		for(int j=1;j<=10;j++){
			if(mp[i][j]=='F'){
				perx=i; pery=j;
			}
			if(mp[i][j]=='C'){
				cowx=i; cowy=j;
			}
		}
	}
	int time=0;
	//牛和人移动
	while(!(perx==cowx&&pery==cowy)){
		int tx=perx+quman.front().x;
		int ty=pery+quman.front().y;
		while(mp[tx][ty]=='*'||tx<1||ty<1||tx>10||ty>10){
			//超出边界或者遇到障碍
			int xx=quman.front().x;
			int yy=quman.front().y;
			quman.pop();
			quman.push({xx,yy}); //换方向 
			tx=perx+quman.front().x;
			ty=pery+quman.front().y;
		}
		perx=tx; pery=ty;
		
		int dx=cowx+qucow.front().x;
		int dy=cowy+qucow.front().y;
		while(mp[dx][dy]=='*'||dx<1||dy<1||dx>10||dy>10){
			//超出边界或者遇到障碍
			int xx=qucow.front().x;
			int yy=qucow.front().y;
			qucow.pop();
			qucow.push({xx,yy}); //换方向 
			dx=cowx+qucow.front().x;
			dy=cowy+qucow.front().y;
		}
		cowx=dx; cowy=dy;
		time++;
		if(time>1000000){ //超出时间限制当成永远不可能相遇 
			cout<<0;
			return 0;
		}
	} 
	cout<<time;
	 
	return 0;
}
2024/4/26 11:25
加载中...