アルゴリズムのメモ
2009.07.09 22:34 ACM/ICPC 2009
#include using namespace std; #define rep(i, n) for ( int i = 0; i < n; i++ ) #define MAX 50 int W, H, G[MAX][MAX]; void dfs( int i, int j){ G[i][j] = 0; static const int di[8] = {0, -1, -1, -1, 0, 1, 1, 1}; static const int dj[8] = {1, 1, 0, -1, -1, -1, 0, 1}; rep(r, 8){ int ni = i + di[r]; int nj = j + dj[r]; if ( ni < 0 || nj < 0 || ni >= H || nj >= W ) continue; if ( G[ni][nj] ) dfs(ni, nj); } } main(){ while( cin >> W >> H && W && H ){ rep(i, H) rep(j, W) cin >> G[i][j]; int cnt = 0; rep(i, H) rep(j, W) if ( G[i][j] ){ cnt++; dfs(i, j); } cout << cnt << endl; } }
テーマ : プログラミング - ジャンル : コンピュータ
| コメント(1) | トラックバック(0) | ↑ページトップ |
Tayama | URL | 2009.07.13 23:01
管理人にのみ表示
↑ページトップ
この記事にトラックバックする(FC2ブログユーザー)
Footmark
このブログをリンクに追加する Powered By FC2 ブログ