洛谷 P2004 领地选择

题目描述

作为在虚拟世界里统帅千军万马的领袖,小 Z 认为天时、地利、人和三者是缺一不可的,所以,谨慎地选择首都的位置对于小 Z 来说是非常重要的。

首都被认为是一个占地C×C 的正方形。小 Z 希望你寻找到一个合适的位置,使得首都所占领的位置的土地价值和最高。

输入格式

第一行三个整数 N,M,C表示地图的宽和长以及首都的边长。

接下来 N 行每行 M 个整数,表示了地图上每个地块的价值。价值可能为负数。

输出格式

一行两个整数 X,Y表示首都左上角的坐标。

输入输出样例

输入 #1

3 4 2
1 2 3 1
-1 9 0 2
2 0 1 1

输出 #1

1 2

说明/提示

对于 60\%60% 的数据,N,M≤50。

对于 90\%90% 的数据,N,M≤300。

对于 100\%100% 的数据,1≤N,M≤10^3,1≤C≤min(N,M)。

#include<bits/stdc++.h>
using namespace std;
long long a[1003][1003];
int main()
{
	long long n,m,c;
	cin>>n>>m>>c;
	long long x,y;
	for(long long i=1;i<=n;i++)
	{
		for(long long j=1;j<=m;j++)
		{
			cin>>a[i][j];
			a[i][j]+=a[i][j-1];求出每一行数组的前缀和
		}
	}
//	for(int i=1;i<=n;i++)
//	{
//		for(int j=1;j<=m;j++)
//		{
//			cout<<a[i][j]<<" ";
//		}
//		cout<<"\n";
//	}
	long long sum=0;
	long long max1=-0x3f3f3f3f;
	for(long long i=1;i<=n-c+1;i++)
	{
		for(long long j=1;j<=m-c+1;j++)//这两层循环是定位首都左上角的坐标
		{
			sum=0;
			for(int k=0;k<c;k++)//这一层循环是每次求出c*c大小的首都
			{
				long long t=a[i+k][j+c-1]-a[i+k][j-1];
				sum+=t;
				if(sum>max1)
				{
					max1=sum;
					x=i;
					y=j;
				}
			}
		}
	}
	cout<<x<<" "<<y;
	return 0;
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您编写这个程序。以下是程序的基本思路: 1. 定义两个拟人角色,每个角色有一个领地和一个小球。 2. 每个角色有两个基本动作:移动和打球。 3. 角色可以移动到自己的领地内,或者向前移动一格。 4. 玩家可以通过键盘输入来控制角色的动作。 5. 如果一个角色将小球打入对方领地,那么这个角色获得一分。 6. 游戏终止条件:一方获得 5 分或者游戏时间到。 下面是程序的核心代码: ```python import random class Player: def __init__(self, name): self.name = name self.score = 0 self.pos = 0 def move(self): choice = input(f"{self.name}, please choose your move (1: stay, 2: move forward): ") if choice == "1": print(f"{self.name} stays in place.") elif choice == "2": self.pos += 1 print(f"{self.name} moves to position {self.pos}.") else: print("Invalid input.") self.move() def hit_ball(self, other): if self.pos == 0: print(f"{self.name} can't hit the ball from their own territory.") else: if random.random() < 0.5: print(f"{self.name} hits the ball towards {other.name}'s territory!") if self.pos > other.pos: print(f"{self.name} scores a point!") self.score += 1 else: print(f"{self.name} misses the ball.") else: print(f"{self.name} misses the ball.") def display_score(self): print(f"{self.name}: {self.score} points") ``` 下面是游戏的主函数: ```python def play_game(): player1 = Player("Player 1") player2 = Player("Player 2") while True: player1.move() player2.move() player1.hit_ball(player2) player2.hit_ball(player1) player1.display_score() player2.display_score() if player1.score >= 5: print(f"{player1.name} wins!") break elif player2.score >= 5: print(f"{player2.name} wins!") break ``` 您可以运行 `play_game()` 函数来开始游戏。希望这个程序符合您的要求!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssssss555555777777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值