洛谷P1183 多边形的面积

深搜灌水 +  皮克定理

 

 1 #include <bits/stdc++.h> 
 2 #define For(i,j,k) for(int i=j;i<=k;i++)
 3 #define LL long long 
 4 using namespace std ; 
 5 
 6 struct point{
 7     int x,y ; 
 8 }p[111] ; 
 9 int n,tmp[211][411],mp[211][411],a,b,s ; 
10  
11 inline LL read() 
12 {
13     LL x = 0 , f = 1 ; 
14     char ch = getchar() ; 
15     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ; } 
16     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar() ; } 
17     return x * f ;  
18 }
19 
20 inline void dfs(int x,int y) 
21 {
22     if(mp[x][y]) return ; 
23     mp[x][y] = 1 ,b++ ; 
24     dfs(x+1,y) ; dfs(x,y+1) ; 
25     dfs(x-1,y) ; dfs(x,y-1) ; 
26 }
27 
28 int main() 
29 {
30     n = read() ; 
31     For(i,1,n) {
32         p[ i ].x = read() ; 
33         p[ i ].y = read() ; 
34     }
35     p[n+1] = p[1] ; 
36     For(i,1,n) {
37         if(p[i].x==p[i+1].x) {
38             a+=abs(p[i+1].y-p[i].y) ; 
39             For(j,min(p[i].y,p[i+1].y),max(p[i].y,p[i+1].y)) 
40                 mp[p[i].x][j] = 1 ; 
41         }
42         else{
43             a+=abs(p[i+1].x-p[i].x) ; 
44             For(j,min(p[i].x,p[i+1].x),max(p[i].x,p[i+1].x)) 
45                 mp[j][p[i].y] = 1 ; 
46         }
47     } 
48     dfs( p[1].x+1,p[1].y+1 ) ; 
49     printf("%d\n",a/2+b-1) ; 
50     return 0 ; 
51 }

 

posted @ 2017-08-18 09:40  third2333  阅读(173)  评论(0编辑  收藏  举报