数据范围才500,枚举即可。n方过500还是绰绰有余的。
因为四周的数字都是0而且你动不了,所以最终状态一定是所有格子里的数都是0,所以直接枚举每个格子里的数能不能变成0就行。
#include <iostream>
using namespace std;
int main(){
int x;
cin>>x;
int lst[x+2][x+2];
for(int i=0; i<=x+1; i++){
for(int j=0; j<=x+1; j++){
lst[i][j] = 0;
}
}
for(int i=1; i<=x; i++){
for(int j=1; j<=x; j++){
cin>>lst[i][j];
}
}
for(int i=1; i<=x; i++){
for(int j=1; j<=x; j++){
if(0 >= lst[i+1][j] and 0 >= lst[i-1][j] and 0 >= lst[i][j+1] and 0 >= lst[i][j-1] and lst[i][j] > 0){
lst[i][j] = 0;
}
if(0 <= lst[i+1][j] and 0 <= lst[i-1][j] and 0 <= lst[i][j+1] and 0 <= lst[i][j-1] and lst[i][j] < 0){
lst[i][j] = 0;
}
}
}
int no=0;
for(int i=1; i<=x; i++){
for(int j=1; j<=x; j++){
if(lst[i][j] != 0)no++;
}
}
if(no==0)cout<<"YES";
else cout<<"NO";
}

京公网安备 11010502036488号