#29706: 幫幫忙


yp11051231@yphs.tp.edu.tw (910-36 楊宸)

學校 : 臺北市私立延平高級中學
編號 : 165190
來源 : [203.72.178.1]
最後登入時間 :
2024-04-13 11:45:10
g596. 2. 動線安排 -- 2021年11月APCS | From: [203.72.178.1] | 發表日期 : 2022-03-23 17:17

這個我想了很久,但只能得 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;

}

 
#29714: Re:幫幫忙


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
g596. 2. 動線安排 -- 2021年11月APCS | From: [39.8.233.87] | 發表日期 : 2022-03-24 09:49


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. 加木樁要先把線拆掉

我覺得在陣列中可以把垂直線和水平線用不同方式記錄下來,程式會比較簡單一點

 
ZeroJudge Forum