--- title: 3A 後花園 tags: solution --- # A. 後花園 - 本題源自於ZeroJudge:[f072 - 家裡的後花園(Garden)](https://zerojudge.tw/ShowProblem?problemid=f072) 利用變數 $L, R$ 和while迴圈來分別抓出最左邊和最右邊有圍牆的位置,接下來就是直接在這個區間內直接窮舉即可。 (對於每一格,都要檢查它的左右是不是有害蟲) ```cpp= # include <iostream> using namespace std; int main(){ int N,n,L,R,ans = 0; char m[1001]; cin>>N; m[0] = m[N+1] = 1; for(n = 1 ; n<=N ; n++) cin>>m[n]; L = 1, R = N; while(L<N && m[L] != '1') L++; while(R>-1 && m[R] != '1') R--; while(L<=R){ if(m[L] == '0' && m[L-1] != '9' && m[L+1] != '9') ans++; L++; } cout<<ans; } ```