Submission #00046
ソースコード
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 49 50 51 52 53 54 55 | #include<iostream> using namespace std; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; int cnt = 0; char data[20][20]; bool flag[20][20]; int sx, sy; int w, h; void dfs( int x, int y) { if ( !flag[y][x] ) { cnt++; flag[y][x] = true ; } for ( int i=0; i<4; i++) { int nx = x+dx[i], ny = y+dy[i]; if ( nx >= 0 && ny >= 0 && nx < w && ny < h ) { if ( data[ny][nx] != '@' && flag[ny][nx] == false ) dfs(nx, ny); } } return ; } int main() { while ( cin >> w >> h, w+h ) { for ( int i=0; i<h; i++) { for ( int j=0; j<w; j++) { cin >> data[i][j]; flag[i][j] = false ; if ( data[i][j] == 'S' ) { data[i][j] = '.' ; sy = i; sx = j; } } } cnt = 0; dfs(sx, sy); cout << cnt << endl; if ( cnt == 0 ) cout << 0 << endl; else { int ans = 2; for ( int i=2; i<=cnt; i++) { ans = ans + i*2; } cout << ans << endl; } } } |
ステータス
項目 | データ |
---|---|
問題 | 0001 - もりたのクッキー☆kiss |
ユーザー名 | ei1430 |
投稿日時 | 2015-09-16 17:56:56 |
言語 | C++11 |
状態 | Accepted |
得点 | 100 |
ソースコード長 | 972 Byte |
最大実行時間 | 23 ms |
最大メモリ使用量 | 556 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 100 / 100 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
DFS_in1.txt | AC | 22 ms | 476 KB |
1
|
DFS_in2.txt | AC | 23 ms | 452 KB |
1
|
DFS_in3.txt | AC | 23 ms | 556 KB |
1
|