Code
#include<iostream>
using namespace std;
#define MAX_SIZE 10005
char map[MAX_SIZE][MAX_SIZE];
int main()
{
int ans = 0;
FILE *fp = fopen("2020.txt","r");
int n=0;
int m=0;
if(!fp){
printf("open file fail!\n");
exit(1);
}
int cur;
while(!feof(fp)){
cur = fgetc(fp);
if(cur=='\n'){
n++;
m=0;
continue;
}
map[n][m++]=cur;
}
n++;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int nx = i+3;
int ny = j+3;
if(map[i][j]!='2') continue;
if(ny<m &&
map[i][j+1]=='0' &&
map[i][j+2]=='2' &&
map[i][j+3]=='0'
) // 横向
ans++;
if(nx<n &&
map[i+1][j]=='0' &&
map[i+2][j]=='2' &&
map[i+3][j]=='0'
) // 竖向
ans++;
if(nx<n && ny<m &&
map[i+1][j+1]=='0' &&
map[i+2][j+2]=='2' &&
map[i+3][j+3]=='0'
) // 斜向
ans++;
}
}
cout<<ans;
return 0;
}
Answer
16520