提交时间:2024-01-23 16:17:00
运行 ID: 124990
//ZJU Felix 2022.3.14 #include <iostream> using namespace std; int mat[1000][1000]; //matrix to store original info int leftresult[1000][1000]; //left result of each spot light int rightresult[1000][1000]; //right result of each spot light int upresult[1000][1000]; //up result of each spot light int downresult[1000][1000]; //down result of each spot light int main() { int n, m; cin >> n >> m; //read in the original matrix for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> mat[i][j]; } } //first round //calculate left and up for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mat[i][j] == 1) continue; //people //up if (i == 0) { upresult[i][j] = 0; } else { upresult[i][j] = mat[i - 1][j] == 1 ? 1 : upresult[i - 1][j]; } //left if (j == 0) { leftresult[i][j] = 0; } else { leftresult[i][j] = mat[i][j - 1] == 1 ? 1 : leftresult[i][j - 1]; } } } //second round //calculate right and down for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { if (mat[i][j] == 1) continue; //people //down if (i == n-1) { downresult[i][j] = 0; } else { downresult[i][j] = mat[i + 1][j] == 1 ? 1 : downresult[i + 1][j]; } //right if (j == m-1) { rightresult[i][j] = 0; } else { rightresult[i][j] = mat[i][j + 1] == 1 ? 1 : rightresult[i][j + 1]; } } } //get the sum int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { sum += upresult[i][j] + leftresult[i][j] + downresult[i][j] + rightresult[i][j]; } } cout << sum; return 0; }