Submission #41143
ソースコード
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include<bits/stdc++.h> using namespace std; typedef pair< int , int > P; const int SIZE = 1005; const int MAX_SIZE = 1000*1000+5; int h,w; int mp[MAX_SIZE]={}; int din[SIZE][SIZE]={}; char maze[SIZE][SIZE]; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int parent[MAX_SIZE]; queue<P> que; void init(); void unite( int x, int y); int find( int i); bool same( int x, int y); int main() { ios_base::sync_with_stdio( false ); cin.tie(NULL); int q; cin >> h >> w >> q; init(); for ( int i=0;i<h;i++){ cin >> maze[i]; } int panel=1; for ( int i=0;i<h;i++){ for ( int j=0;j<w;j++){ if (din[i][j] == 0 && maze[i][j] == '#' ){ que.push(P(i, j)); din[i][j] = panel; int count = 1; while (!que.empty()){ P p = que.front(); que.pop(); int x = p.second; int y = p.first; for ( int k=0;k<4;k++){ int nx = x+dx[k]; int ny = y+dy[k]; if (nx >= 0 && nx < w && ny >= 0 && ny < h && maze[ny][nx] == '#' && din[ny][nx] == 0){ din[ny][nx] = panel; count++; que.push( P(ny, nx) ); } } } mp[ panel ] = count; panel++; } } } for ( int i=0;i<q;i++){ int a,b,c; cin >> a >> b >> c; b--; c--; if (a == 1) cout << mp[ find(din[b][c]) ] << endl; else { int y,sum=0; maze[b][c] = '#' ; din[b][c] = y = panel; panel++; for ( int j=0;j<4;j++){ int nx = c+dx[j]; int ny = b+dy[j]; if (nx >= 0 && nx < w && ny >= 0 && ny < h && maze[ny][nx] == '#' ){ int x = find(din[ny][nx]); if (!same(x, y)){ sum += mp[x]; unite(x, y); y = find(y); } } } mp[ find(y) ] = sum+1; } } return 0; } void init() { for ( int i=0;i<h*w+5;i++) parent[i] = i; return ; } void unite( int x, int y) { x = find(x); y = find(y); if (x == y) return ; parent[y] = x; return ; } int find( int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } bool same( int x, int y) { return ( find(x) == find(y) ); } |
ステータス
項目 | データ |
---|---|
問題 | 0971 - 島の創造 |
ユーザー名 | ei1612 |
投稿日時 | 2018-08-10 12:07:09 |
言語 | C++14 |
状態 | Accepted |
得点 | 10 |
ソースコード長 | 2107 Byte |
最大実行時間 | 1064 ms |
最大メモリ使用量 | 22072 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | Easy | 1 / 1 | *easy, *sample* |
2 | ALL | 9 / 9 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # | |
---|---|---|---|---|---|
input-sample1 | AC | 27 ms | 6624 KB |
1
|
2
|
input-sample2 | AC | 23 ms | 6580 KB |
1
|
2
|
input01-easy | AC | 26 ms | 6660 KB |
1
|
2
|
input02-easy | AC | 26 ms | 6692 KB |
1
|
2
|
input03-easy | AC | 28 ms | 6832 KB |
1
|
2
|
input04 | AC | 513 ms | 11660 KB |
2
|
|
input05 | AC | 334 ms | 12260 KB |
2
|
|
input06 | AC | 493 ms | 13116 KB |
2
|
|
input07 | AC | 539 ms | 14352 KB |
2
|
|
input08 | AC | 571 ms | 15596 KB |
2
|
|
input09 | AC | 593 ms | 16960 KB |
2
|
|
input10 | AC | 634 ms | 19732 KB |
2
|
|
input11 | AC | 1064 ms | 21608 KB |
2
|
|
input12 | AC | 580 ms | 22072 KB |
2
|
|
input13 | AC | 21 ms | 17940 KB |
2
|
|
input14 | AC | 23 ms | 17892 KB |
2
|