PTA 打地鼠

本文详细描述了一个C++程序,涉及老鼠类的定义、构造函数以及在一个二维地图上执行击打操作后,统计剩余老鼠数量和生命值为1的老鼠数量。
摘要由CSDN通过智能技术生成

因为是今年大一重带的期末考试题,所以目前找不到电子版的题目,这里就不多描述了,如果重带学子之后复习的时候遇到了这题就直接看就行了

#include <bits/stdc++.h>
using namespace std;
//声明一个老鼠类 
class mice
{
	public:
		int x;
		int y;
		int hp;
		mice(int x,int y):x(x),y(y),hp(2){};//有参构造 
};
int main()
{
	int m,n;
	cin>>m>>n;
	vector<mice> v;
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			char c;//画地图
			cin>>c;
			if(c=='#')//有老鼠,就直接将老鼠信息完善并推入数组里面 
			{
				mice temp(i,j);
				v.push_back(temp);
			} 
		}
	}
	int num;
	cin>>num;
	for(int i=0;i<num;i++)
	{
		int x,y;
		cin>>x>>y;
		//因为考虑余波,因此以击打坐标为基础产生四个衍生坐标 
		int x1=x-1;
		int x2=x+1;
		int y1=y-1;
		int y2=y+1;
		//进行判定 
		for(vector<mice>::iterator it=v.begin();it!=v.end();it++)
		{
			if(it->x==x&&it->y==y) it->hp-=2;
			else if(it->x==x1&&it->y==y) it->hp-=1;
			else if(it->x==x2&&it->y==y) it->hp-=1;
			else if(it->x==x&&it->y==y1) it->hp-=1;
			else if(it->x==x&&it->y==y2) it->hp-=1;
		}
	}
	int count1,count2;
	for(vector<mice>::iterator it=v.begin();it!=v.end();it++)
	{
		if(it->hp>0) count1++;
		if(it->hp==1) count2++;
	}
	cout<<v.size()-count1<<" "<<count2;
	return 0;
}

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值