Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
这是一个回合制游戏,每一回合开始前会进行上一回合的结算。
有一张 n∗n 的棋盘,我们出生在一个初始位置 (x,y),现在我们要选择一个位置建设城市。
你的人物每回合可以移动到距离你曼哈顿距离不超过 2 的位置,移动完成后可以选择是否建立城市。
建立城市后,你的人物消失,成为一个人口为 1 的城市,这个人口要下回合才可以工作。如果不移动,直接在 (x,y) 建城,第 1 回合就可以开始工作。
对于城市的每个居民,你可以安排他到距离城市曼哈顿距离小于等于 3 的位置进行工作,此居民可以瞬间到达该位置,每个位置最多安排一个居民,失业的人口不会生产任何食物。
注意,城市位置上必须有一个居民在工作。
结算按照如下顺序:
- 如果位置 (i,j) 上有一个工作居民,则获得 a[i][j] 的食物。
- 如果当前城市人口为 i,且食物达到 8∗i2 时,你获得一个新的居民,下一回合可以进行操作。
当结算后城市总人口达到 9 游戏结束。
初始食物数量为 0,人口上涨不会导致之前积累的食物消失。输出最少几个回合能让游戏结束。
Input
第一行一个正整数 test(1≤test≤10) 表示数据组数。
对于每组数据,第一行三个正整数 n(2≤n≤500),x(1≤x≤n),y(1≤y≤n),分别表示棋盘大小和起始位置。
接下来 n 行,每行 n 个整数,第 i 行第 j 列的整数表示 a[i]j。
Output
对于每组数据,一行一个整数表示答案。
Sample Input
1
10 9 8
1 2 2 1 2 3 1 1 2 1
2 1 3 3 3 2 3 2 3 1
1 1 3 1 1 3 2 2 1 2
3 1 3 1 3 3 1 3 1 3
3 2 3 1 3 1 2 2 2 1
2 3 2 3 2 2 3 1 2 3
3 1 3 3 2 2 3 2 3 3
1 3 3 2 3 2 2 2 1 1
3 3 1 2 3 2 1 2 1 2
1 1 3 1 3 1 1 1 3 3
Sample Output
39
分析
算出棋盘每个位置开始所需的回合数加上从出生点到该位置的回合数即为结果,选出最小值。
C++代码(来自asdkjc大佬)
#include<iostream>
using namespace std;
int n,x,y,minans;
int a[501][501],cot[5],ans[501][501];
void BfsAndCalc(int x,int y)
{
int cou=1,pro,round=0;
cot[1]=cot[2]=cot[3]=cot[4]=0;
for(int i=-3;i<=3;i++)
for(int j=-(3-abs(i));j<=3-abs(i);j++)
{
if(x+i>0&&x+i<=n&&y+j>0&&y+j<=n)cot[a[x+i][y+j]]++;
}
cot[a[x][y]]--;
pro=a[x][y];
cot[4]=0;
while(cou!=9)
{
int temp=(8*cou*cou-cot[4]+pro-1)/pro;
round+=temp;
cot[4]+=temp*pro;
cou++;
if(cot[3])
{
cot[3]--;
pro+=3;
}
else if(cot[2])
{
cot[2]--;
pro+=2;
}
else if(cot[1])
{
cot[1]--;
pro+=1;
}
// cout<<cot[3]<<" "<<cot[2]<<" "<<cot[1]<<endl;
}
ans[x][y]=round;
}
int main()
{
int test;
cin>>test;
while(test--)
{
minans=1e9;
cin>>n>>x>>y;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>a[i][j];
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
{
BfsAndCalc(i,j);
if((abs(x-i)+abs(y-j)+1)/2+ans[i][j]<minans)
{
minans=(abs(x-i)+abs(y-j)+1)/2+ans[i][j];
// cout<<minans<<" "<<i<<" "<<j<<endl;
}
}
cout<<minans<<endl;
}
return 0;
}
asdkjc原博客https://blog.csdn.net/weixin_44582673/article/details/107457333