ural 1250. Sea Burial
1250. Sea Burial
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
There is Archipelago in the middle of a shoreless ocean. An ancient tribe of cannibals lives there. Shamans of this race have been communicating with gods and admonishing people for ages. They could generate a rain during a drought and clear the sky in a raining season. A long time ago the first shaman of the tribe jumped into one of the seas and drowned while being in a sacred trance. Since then all the land inside this sea is regarded as sacred. According to an ancient law, all shamans must be buried on a sacred land. However, souls of dead shamans cannot get on with each other, so each shaman must be buried on a separate island. An old prophecy says that if two shamans are buried on the same land, then a dreadful time will come and the tribe will perish.
How many shamans will the tribe outlive? This problem bothered all the chiefs of the tribe who were coming into power. So one of the chiefs ordered to compile a map of Archipelago. The cannibals toiled for a whole year and coped with the task. But the map was too large and complicated to count all the sacred islands. So the tribe's shaman appealed to gods and asked them to help with counting the islands. And the tribe was blessed with a Programmer and a Computer, which came down to earth in a cloud of light and fire. Yes, you are this Programmer, and you are destined to live with these cannibals until you have counted the islands; then you'll be transferred back home. Remember that there may be seas inside islands, and islands inside those seas.
Input
The first input line contains four integers W, H, X and Y, separated with one or several spaces. 1 ≤ W, H ≤ 500 are respectively the width and the height of the map. 1 ≤ X ≤ W and 1 ≤ Y ≤ Hare the coordinates of the point where the first shaman drowned. The next H lines contain description of the map. Each line contains W symbols describing correspondent fragments of the map. Symbol "." stands for a sea fragment and symbol "#" stands for a land fragment. Two fragments belong to the same sea if they are adjacent horizontally, vertically, or diagonally. Two fragments belong to the same island if they are adjacent horizontally or vertically (but not diagonally). Land fragments that are adjacent to the map's border are not considered as islands. Coordinates are counted from the left upper corner.
Output
The output should contain a single integer, which is the number of the islands inside the sacred sea.
Samples
input | output |
---|---|
9 7 1 1 ........# .#######. .#.....#. .#.#.#.#. .#.....#. .#######. #........ |
3 |
9 7 3 3 ........# .#######. .#.....#. .#.#.#.#. .#.....#. .#######. #........ |
2 |
Problem Author: Stanislav Skorb (prepared by Ivan Dashkevich)
Problem Source: Open collegiate programming contest for student teams, Ural State University, March 15, 2003
Problem Source: Open collegiate programming contest for student teams, Ural State University, March 15, 2003
Tags: graph theory
Difficulty: 723
题意:给出一张海图,#代表陆地,.代表海洋。
八方向相连为同一片海域,四方向相连为同一个岛屿。与边界相连的岛屿不算。
给出一个点,问这个点所在海域包含了几个岛屿。
注意如果包含一个岛屿,那么这个岛屿的内海包含的岛屿也算。
分析:暴力搜索。注意处理即可。
可以先把掉落所在海域求出。
然后将不在海域之内的所有格子标记。
然后问题就变为求一张图内#连通块的个数。
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define ft first 30 #define sd second 31 #define mk make_pair 32 33 inline int Getint() 34 { 35 int Ret = 0; 36 char Ch = ' '; 37 bool Flag = 0; 38 while(!(Ch >= '0' && Ch <= '9')) 39 { 40 if(Ch == '-') Flag ^= 1; 41 Ch = getchar(); 42 } 43 while(Ch >= '0' && Ch <= '9') 44 { 45 Ret = Ret * 10 + Ch - '0'; 46 Ch = getchar(); 47 } 48 return Flag ? -Ret : Ret; 49 } 50 51 const int N = 510; 52 const int DX[] = {-1, 0, 1, 0, -1, -1, 1, 1}, DY[] = {0, -1, 0, 1, -1, 1, -1, 1}; 53 #define Land 1 54 #define Sea 2 55 int n, m, sx, sy; 56 int graph[N][N], mark[N][N]; 57 int ans; 58 59 inline void Input() 60 { 61 scanf("%d%d%d%d", &n, &m, &sx, &sy); 62 swap(sx, sy), swap(n, m); 63 for(int i = 1; i <= n; i++) 64 for(int j = 1; j <= m; j++) 65 { 66 char ch = ' '; 67 while(ch != '.' && ch != '#') ch = getchar(); 68 graph[i][j] = ch == '.' ? Sea : Land; 69 } 70 } 71 72 inline bool Check(int x, int y, int state) 73 { 74 if(x < 1 || x > n || y < 1 || y > m) return 0; 75 if(!(graph[x][y] & state) || mark[x][y]) return 0; 76 return 1; 77 } 78 79 inline void Draw(int sx, int sy, int state, int tag) 80 { 81 static int que[N * N][2], tail; 82 mark[sx][sy] = tag; 83 tail = 1, que[1][0] = sx, que[1][1] = sy; 84 for(int head = 1; head <= tail; head++) 85 { 86 int ux = que[head][0], uy = que[head][1]; 87 for(int t = 0; t < 8; t++) 88 { 89 if(t > 3 && graph[ux][uy] == Land) break; 90 int vx = ux + DX[t], vy = uy + DY[t]; 91 if(!Check(vx, vy, state)) continue; 92 mark[vx][vy] = tag, tail++; 93 que[tail][0] = vx, que[tail][1] = vy; 94 } 95 } 96 } 97 98 inline void Solve() 99 { 100 if(graph[sx][sy] == '#') 101 { 102 puts("0"); 103 return; 104 } 105 106 Draw(sx, sy, Sea, 1); 107 for(int i = 1; i <= n; i++) 108 { 109 Draw(i, 0, Land | Sea, 2); 110 Draw(i, m + 1, Land | Sea, 2); 111 } 112 for(int i = 1; i <= m; i++) 113 { 114 Draw(0, i, Land | Sea, 2); 115 Draw(n + 1, i, Land | Sea, 2); 116 } 117 118 for(int i = 1; i <= n; i++) 119 for(int j = 1; j <= m; j++) 120 if(!mark[i][j] && graph[i][j] == Land) 121 { 122 ans++; 123 Draw(i, j, Land, 3); 124 } 125 126 printf("%d\n", ans); 127 } 128 129 int main() 130 { 131 freopen("a.in", "r", stdin); 132 Input(); 133 Solve(); 134 return 0; 135 }