窮舉完每個黑色棋子,並對其進行八個方向的窮舉,
方向的設定可以利用一個8x2的陣列來存起來,這樣在後續利用for迴圈窮舉時會變得比較容易。
# 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';
}
C. 完美平方數 #include <iostream> #include <math.h> using namespace std; int numSquares(int n); int main() { int num, ans;
Jan 27, 2022E. 鐵路 本題源自於Onling Judge:514 - Rails 題目目標在於給定出站順序的前提下,利用已知入站順序1~N, 確定是否仍能夠以目標出站順序離開。 有一個簡單的想法,我們利用queue的特性來維護出站順序、 利用stack的特性來維護入站順序, 並依次比較它們的front/top是否相同,如果相同就安排出站(pop)。
Jan 27, 2022C. 組合 #include<bits/stdc++.h> using namespace std; vector<int> a; bool first=true,f=true; fstream input,output; void find(int g,vector<int> &can,int p){ if(!g){ if(!first){
Sep 19, 2021D. Flood Fill # include <bits/stdc++.h> using namespace std; int m[102][102]; struct Fill{ int x,y,t; };
Sep 16, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up