這個我想了很久,但只能得 NA(45%),我還有哪些地方沒顧慮到? 可以幫我看一下嗎?
# include <bits/stdc++.h>
using namespace std;
int main()
{
int m,n,h,Max=0;
cin >> m >> n >> h;
vector<int> Count;
vector<int> row(n,0);
vector<vector<int>> Plant(m,row);
int x,y,Move;
for (int i=0; i<h; i++){
cin >> x >> y >> Move;
if (Move == 0)
Plant[x][y] = 696;
else
Plant[x][y] = 0;
// Around
int a = x-2, b = x+2, c = y-2, d = y+2;
// Left
while (a >= 0){
if (Plant[a][y] == 696){
for (int j=a+1; j<x; j++){
if (Move == 0)
Plant[j][y] += 1;
else
Plant[j][y] -= 1;
}
break;
}
a -= 1;
}
// Right
while (b < m){
if (Plant[b][y] == 696){
for (int k=b-1; k>x; k--){
if (Move == 0)
Plant[k][y] += 1;
else
Plant[k][y] -= 1;
}
break;
}
b += 1;
}
// Up
while (c >= 0){
if (Plant[x][c] == 696){
for (int l=c+1; l<y; l++){
if (Move == 0)
Plant[x][l] += 1;
else
Plant[x][l] -= 1;
}
break;
}
c -= 1;
}
// Down
while (d < n){
if (Plant[x][d] == 696){
for (int o=d-1; o>y; o--){
if (Move == 0)
Plant[x][o] += 1;
else
Plant[x][o] -= 1;
}
break;
}
d += 1;
}
// Find
for (int i=0; i<m; i++){
for (int j=0; j<n; j++){
if (Plant[i][j] == 2){
if (i == m-1 || j == n-1)
Plant[i][j] -= 1;
else{
if (Plant[i][j-1] == 0 || Plant[i][j+1] == 0 || Plant[i-1][j] == 0 || Plant[i+1][j] == 0)
Plant[i][j] -= 1;
else
continue;
}
}
else
continue;
}
}
// Count
int Total=0;
for (int aa=0; aa<m; aa++){
for (int bb=0; bb<n; bb++){
int Check = Plant[aa][bb];
if (Check != 0)
Total += 1;
}
}
if (Total > Max)
Max = Total;
Count.push_back(Total);
}
cout << Max << '\n';
int Index = Count.size()-1;
cout << Count[Index] << '\n';
return 0;
}
int a = x-2, b = x+2, c = y-2, d = y+2;
if (Plant[i][j-1] == 0 || Plant[i][j+1] == 0 || Plant[i-1][j] == 0 || Plant[i+1][j] == 0)
1. int a = x-2, b = x+2, c = y-2, d = y+2;這裡會有問題,因為可能隔壁有木樁
2. if (Plant[i][j-1] == 0 || Plant[i][j+1] == 0 || Plant[i-1][j] == 0 || Plant[i+1][j] == 0)會超出陣列範圍,所以有Segmentation fault
3. 加木樁要先把線拆掉
我覺得在陣列中可以把垂直線和水平線用不同方式記錄下來,程式會比較簡單一點