题目:L1-112 现代战争 - 团体程序设计天梯赛-练习集
题意: 给定一个n*m的矩阵 每次找到该矩阵中最大值Vij 然后将该最大值所在的行和列“炸掉”,给一个数K 表示要炸k次 最后输出 炸完之后的值
思路:用一个数组保存矩阵 用两个逻辑数组 保存该列和行 是否被炸的情况即可 如果被炸则continue 没有则可以炸掉 最后输出也使用该逻辑数组跳过被炸部分,由于数据范围较小 则不用优化即可 代码如下:
#include<iostream>
using namespace std;
typedef long long ll;
ll g[1005][1005];//保存
bool col[1005],row[1005];//列和行
int main()
{
int n,m,k;
cin>>n>>m>>k;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) cin>>g[i][j];
while(k--)
{
int x,y,maxn = -1;
for(int i=0;i<n;i++)
{
if(col[i]) continue;
for(int j=0;j<m;j++)
{
if(row[j]) continue;
if(g[i][j]>maxn)
{
x=i,y=j,maxn=g[i][j];
}
}
}
col[x] = true,row[y]=true;
}
for(int i=0;i<n;i++)
{
bool head = true;//由于PTA格式要求 所以需要用一个逻辑值来调整输出方式
if(col[i]) continue;
for(int j=0;j<m;j++)
{
if(row[j]) continue;
if(head) {cout<<g[i][j];head=false;}
else cout<<' '<<g[i][j];
}
cout<<endl;
}
return 0;
}