链接:https://ac.nowcoder.com/acm/problem/14505
来源:牛客网
题目描述
现在给出一个正方形地图,其边长为n,地图上有的地方是空的,有的地方会有敌人。
我们现在有一次轰炸敌人的机会,轰炸敌人的区域是一个k*k的正方形区域,现在需要你解决的问题就是计算最多轰炸的敌人数量是多少。
输入描述:
本题包含多组数据,每组数据第一行输入两个数n,k。
接下来n行,每行n个数字,表示这个点上的敌人数量。
数据范围:
1<=n<=50
1<=k<=n
每个点上的敌人数量不超过100个(0<=a[i][j]<=100)。
输出描述:
每组数据输出包含一行,表示计算的结果。
示例1
输入
复制
4 2
1 1 0 0
1 1 0 0
0 0 2 2
0 0 2 2
输出
复制
8
说明
样例中,显然轰炸右下角那个部分能够击败最多的敌人
就是在一个二维表中找到最大的子矩阵和,采用二维前缀和的方法(原理就是求平面中某一块的面积。概率论的知识)。注意多组输入,第一发就没注意到这个
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;
using namespace std;
map<int,map<int,int> >flag;
//炸弹区最优选择
int n,k;
int main()
{
while(cin>>n>>k)
{
int a[57][57];
int dp[55][55];
int maxn = 0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>dp[i][j];
dp[i][j] = dp[i][j] + dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
}
}
cout<<endl<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
for(int i=k;i<=n;i++)
{
for(int j=k;j<=n;j++)
{
maxn = max(maxn,dp[i][j]-dp[i-k][j]-dp[i][j-k]+dp[i-k][j-k]);
}
}
cout<<maxn<<endl;
}
return 0;
}
链接:https://ac.nowcoder.com/acm/problem/17340
来源:牛客网
题目描述
Chiaki has an n x n matrix. She would like to fill each entry by -1, 0 or 1 such that r1,r2,…,rn,c1,c2, …, cn are distinct values, where ri be the sum of the i-th row and ci be the sum of the i-th column.
输入描述:
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 200), indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 200) – the dimension of the matrix.
输出描述:
For each test case, if no such matrix exists, output impossible'' in a single line. Otherwise, output
possible’’ in the first line. And each of the next n lines contains n integers, denoting the solution matrix. If there are multiple solutions, output any of them.
示例1
输入
复制
2
1
2
输出
复制
impossible
possible
1 0
1 -1
观察题目要求:只要n是奇数,就不行,如果n是偶数,这样填:
下三角全部填1,上三角全部填-1,主对角线上半部分填1,下半部分填0;
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;
using namespace std;
map<int,map<int,int> >flag;
int i,j,k,l,s,n,m,test,a[205][205];
int main() {
scanf("%d",&test);
while (test--) {
scanf("%d",&n);
if (n&1) puts("impossible");
else {
puts("possible");
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
a[i][j] = 1;
for(int j=1;j<i;j++)
a[i][j] = -1;
}
for(int i=1;i<=n/2;i++)
{
a[i][i] = 1;
a[n-i+1][n-i+1] = 0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
}
return 0;
}