蓝桥杯 试题 算法提高 Cat And Mouse

原题

分析

题目简单,但是其中下标注意不要写错,用devcpp不习惯,整体替换时多替换了,导致后面出现问题,花了一会儿,送分题不能浪费太多时间。


#include <stdio.h>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

char c[110][110];
typedef struct point{
	int x,y;
}point;
point s,e;
char cr[110][110]={
	{"*...*....."},
	{"......*..."},
	{"...*...*.."},
	{".........."},
	{"...*.C...."},
	
	{"*.....*..."},
	{"...*......"},
	{"..M......*"},
	{"...*.*...."},
	{".*.*......"}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//return 0;
	
	for (int i=1;i<=10;i++){
		for (int j=1;j<=10;j++){
			c[i][j]=cr[i-1][j-1];
		//	cout<<c[i][j];
			if (c[i][j]=='C')s.x=i,s.y=j;
			else if (c[i][j]=='M')e.x=i,e.y=j;
		}
	}
/*
for (int i=1;i<=10;i++){
		for (int j=1;j<=10;j++){
			cout<<c[i][j];
		
		}
		cout<<endl;
	}
*/
	
	int m1=0,m2=0;
	int x1=s.x,y1=s.y,x2=e.x,y2=e.y;
	//cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2;
	c[x1][y1]='.';
	c[x2][y2]='.';
	int sum=0;
	while (1){
		/*
		cout<<"time=="<<sum<<endl;
		cout<<x1<<"   "<<y1<<"----"<<x2<<"   "<<y2<<endl;
		cout<<m1<<"   "<<m2<<endl;
		printf("curr time:%d\n", sum);
		printf("cat:(%d,%d,%d) --- ",  x1, y1,m1 );
		printf("mouse:(%d,%d,%d)\n",  x2, y2,m2 );
		*/
		
		//cout<<1<<endl;
		if (m1==0){
			x1--;
			if (c[x1][y1]=='*'||x1<=0 ){
				m1=1;
				x1++;
			}
		}else if (m1==1){
			y1++;
			if (c[x1][y1]=='*'||y1>10){
				m1=2;
				y1--;
			}
		}else if (m1==2){
			x1++;
			if (c[x1][y1]=='*'||x1>10){
				m1=3;
				x1--;
			}
		}else{
			y1--;
			if (c[x1][y1]=='*'||y1<=0){
				m1=0;
				y1++;
			}
		}
		
		if (m2==0){
			x2--;
			if (c[x2][y2]=='*'||x2<=0){
				m2=1;
				x2++;
			}
		}else if (m2==1){
			y2++;
			if (c[x2][y2]=='*'||y2>10){
				m2=2;
				y2--;
			}
		}else if (m2==2){
			x2++;
			if (c[x2][y2]=='*'||x2>10){
				m2=3;
				x2--;
			}
		}else{
			y2--;
			if (c[x2][y2]=='*'||y2<=0){
				m2=0;
				y2++;
			}
		}
		sum++;
		if (x1==x2 && y1==y2){
			break;
		}
	}
	cout<<sum<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值