---
title: S1D 黑白棋
tags: solution
---
# D. 黑白棋
- 本題源自於 程式自學平台:[ITSA Online Contest 74th Problem 7. 找出合法棋步](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=50119)
(需先登入才能閱覽題目)
窮舉完每個黑色棋子,並對其進行八個方向的窮舉,
方向的設定可以利用一個8x2的陣列來存起來,這樣在後續利用for迴圈窮舉時會變得比較容易。
```cpp=
# include <iostream>
using namespace std;
int ch[10][10];
int main(){
int x, y, z, d, tx, ty, ans = 0;
int dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
for(x = 0 ; x<10 ; x++)
ch[x][9] = ch[x][0] = ch[9][x] = ch[0][x] = 3;
for(x = 1 ; x<=8 ; x++)
for(y = 1 ; y<=8 ; y++)
cin>>ch[x][y];
for(x = 1 ; x<=8 ; x++){
for(y = 1 ; y<=8 ; y++){
if(ch[x][y] != 1)
continue;
for(d = 0 ; d<8 ; d++){
if(ch[tx = x+dir[d][0]][ty = y+dir[d][1]] != 2)
continue;
while(ch[tx+=dir[d][0]][ty+=dir[d][1]] == 2);
if(ch[tx][ty] == 0){
ch[tx][ty] = 3;
ans++;
}
}
}
}
cout<<ans<<'\n';
}
```