杭电9-Just another board game

传送门

题意:

给你一个 n ∗ m n*m nm的网格,每个格子里有其相应的权重,最初有一个棋子在 ( 1 , 1 ) (1,1) (1,1)上,棋子最终所在的位置为最终值,a想要最大化这个值,b要最小化这个值。

思路:

从整场比赛来看,如果某人不是最后一次移动棋子的那个人,那么他应该考虑的是:尽量让另一个人下次所能移动的最小/最大值最大/最小
例如,如果是b最后一次移动棋子(k为偶数),b想要最小化这个值,而a则需要将棋子移动到每一列的最小值最大的那一列,如果是a最后一次移动棋子(k为奇数),道理和上面差不多,最后只需要将用此方法得到的答案与(1,1)的值进行比对即可。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
#define ll long long
ll maxx_n[100010];
ll maxx_m[100010];

int main()
{
    
    
	int t;
	cin>>t;
	while(t--)
	{
    
    
		int n,m;
		ll k;
		ll s = 0;
		scanf("%d%d%lld",&n,&m,&k);
		memset(maxx_n,0,sizeof(maxx_n));
		memset(maxx_m,0x3f3f3f3f,sizeof(maxx_m));
		for(int i = 0; i < n; i++)
		{
    
    
			for(int j = 0; j < m; j++)
			{
    
    
				ll op;
				scanf("%lld",&op);
				if(i == 0 && j == 0)s = op;
				maxx_n[i] = max(maxx_n[i], op);
				maxx_m[j] = min(maxx_m[j], op);
			}
		}
		if(k == 1)
		{
    
    
			printf("%lld\n", maxx_n[0]);
		}
		else if(k == 2)
		{
    
    
			ll ans = 0;
			for(int i = 0; i < m; i++)ans = max(maxx_m[i], ans);
			printf("%lld\n",max(ans, s));
		}
		else if(k%2)
		{
    
    
			ll ans = 0x3f3f3f3f;
			for(int i = 0; i < n; i++)ans = min(ans, maxx_n[i]);
			printf("%lld\n",max(ans, s));
		}
		else if(k%2==0)
		{
    
    
			ll ans = 0;
			for(int i = 0; i < m; i++)ans = max(maxx_m[i], ans);
			printf("%lld\n",max(ans, s));
		}
	}
}

おすすめ

転載: blog.csdn.net/p15008340649/article/details/119766934