样例输入:
2
1 2
3 4
样例输出:
10 10
10 10
就是每个元素与它相邻的8个元素相加(上下左右,左、右上角,左、右下角)
解释:2、3、4都是样例输入中1的相邻数,1+2+3+4=10,所以1的位置输出10
#include<bits/stdc++.h>
using namespace std;
int x1[8]={0,1,1,1,0,-1,-1,-1};
int y8[8]={-1,-1,0,1,1,1,0,-1};
int x,y,n,arr[1001][1001];
int main(){
ios::sync_with_stdio(false);
cout.tie();
cin.tie();
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>arr[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int l=0;l<8;l++){
x=i+x1[l];
y=j+y8[l];
if(x>n || y>n){
continue;
}else{
arr[i][j]+=arr[x][y];
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}