Programming/백준
[BOJ] 1012번 유기농배추
M0ca
2018. 7. 12. 13:51
반응형
들어가는 시점만 알면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> #include <queue> using namespace std; int arr[51][51] {0}; int t,m,n,k; int cnt; void search(int x,int y) { if(arr[y][x]){ arr[y][x] = 0; if(x > 0) search(x-1,y); if(x < m) search(x+1,y); if(y > 0) search(x,y-1); if(y < n - 1) search(x,y+1); } } int main() { cin >> t; for(int a = 0; a < t; a++){ cin >> m >> n >> k; for(int i = 0; i < k; i++) { int x,y; cin >> x >> y; arr[y][x] = 1; } for(int i =0; i < n; i++) { for(int j = 0; j < m; j++) { if(arr[i][j]) { cnt++; search(j,i); } } } cout << cnt << endl; cnt = 0; } return 0; } | cs |
반응형