Submission #00011


ソースコード

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<bits/stdc++.h>
using namespace std;
int W, H;
char s[20][20];
int Sx, Sy;
const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
int dfs(int x, int y)
{
if(x < 0 || x >= W || y < 0 || y >= H) return(0);
if(s[y][x] == '@') return(0);
s[y][x] = '@';
int ret = 1;
for(int i = 0; i < 4; i++) {
ret += dfs(x + dx[i], y + dy[i]);
}
return(ret);
}
int main()
{
while(cin >> W >> H, W) {
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
cin >> s[i][j];
if(s[i][j] == 'S') {
Sx = j;
Sy = i;
}
}
}
s[Sy][Sx] = '.';
int ret = dfs(Sx, Sy);
cout << ret << endl;
cout << ret * (ret + 1) << endl;
}
// a1 = 2
// an+1 - an = 2n+2
// n-1
// an = 2 + Σ (2k+2) = 2 + n*(n-1) + 2(n-1) = n * (n - 1) + 2n = 2n + n(n-1) = n((n - 1) + 2)
// k=1
}

ステータス

項目 データ
問題 0001 - もりたのクッキー☆kiss
ユーザー名 ei1333
投稿日時 2015-09-16 16:54:54
言語 C++11
状態 Accepted
得点 100
ソースコード長 929 Byte
最大実行時間 19 ms
最大メモリ使用量 712 KB

セット

セット 得点 Cases
1 ALL 100 / 100 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
DFS_in1.txt AC 17 ms 476 KB
1
DFS_in2.txt AC 15 ms 712 KB
1
DFS_in3.txt AC 19 ms 560 KB
1