标签:
input | output |
---|---|
9 7 1 1 ........# .#######. .#.....#. .#.#.#.#. .#.....#. .#######. #........ |
3 |
9 7 3 3 ........# .#######. .#.....#. .#.#.#.#. .#.....#. .#######. #........ |
2 |
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 }
标签:
原文地址:http://www.cnblogs.com/StupidBoy/p/5077674.html