首页 > 代码库 > HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 524 Accepted Submission(s): 151Problem DescriptionTanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1).
Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component).
InputThe first line contains apositiveinteger T(T≤10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C, 0<R,C≤109 the second line contains an integer n, the number of bad coconuts, 0≤n≤200 from the third line, there comes n lines, each line contains two integers, xi and yi, which means in cell(xi,yi), there is a bad coconut.
It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time.
OutputFor each test case, output "Case #x:" in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order.
Sample Input2 3 3 2 1 2 2 1 3 3 1 2 2
Sample OutputCase #1: 2 1 6 Case #2: 1 8
Source2016CCPC东北地区大学生程序设计竞赛 - 重现赛
Recommendwange2014 | We have carefully selected several similar problems for you: 5932 5931 5930 5928 5923
Statistic | Submit | Discuss | Note
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5925
题目大意:
R行C列的网格(R,C<=109),有N(N<=200)个障碍,求联通块数量和各个大小(从小到大)。
题目思路:
【离散化+BFS】
首先障碍数量很小,所以可以离散化,把一大块没有障碍的网格缩成一个,权值A[i][j]为原先的小网格数。
缩网格只要以每一个障碍为中心画水平垂直线即可。细节注意一下
缩完之后有不超过400*400的矩形,暴力枚举一遍联通块,BFS,每个矩形走一次。即可得到各个联通块大小。排序输出即可
注意0的时候输出两行0.
(被这题卡了一晚上好气啊,写了随机数据把百度首页的代码全HACK掉了,数据打了崩溃溢出或者WA的。这也能AC吗。最后发现自己多打了一个+1。+1s)
1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 //#include<stdbool.h> 19 #include<math.h> 20 #pragma comment(linker,"/STACK:1024000000,1024000000") 21 #define min(a,b) ((a)<(b)?(a):(b)) 22 #define max(a,b) ((a)>(b)?(a):(b)) 23 #define abs(a) ((a)>0?(a):(-(a))) 24 #define lowbit(a) (a&(-a)) 25 #define sqr(a) ((a)*(a)) 26 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 27 #define mem(a,b) memset(a,b,sizeof(a)) 28 #define eps (1e-10) 29 #define J 10000 30 #define mod 1000000007 31 #define MAX 0x7f7f7f7f 32 #define PI 3.14159265358979323 33 #define N 1004 34 using namespace std; 35 typedef long long LL; 36 double anss; 37 LL aans; 38 int cas,cass; 39 int n,m,lll,ans; 40 int nc,nn,mm; 41 LL s[N],r[N],rr[N],a[N][N]; 42 int dx[]={-1,1,0,0}; 43 int dy[]={0,0,-1,1}; 44 bool u[N][N]; 45 struct xxx 46 { 47 int x,y,x1,y1; 48 }c[N]; 49 bool cmp1(xxx aa,xxx bb) 50 { 51 if(aa.x!=bb.x)return aa.x<bb.x; 52 return aa.y<bb.y; 53 } 54 bool cmp2(xxx aa,xxx bb) 55 { 56 if(aa.y!=bb.y)return aa.y<bb.y; 57 return aa.x<bb.x; 58 } 59 void spfa(int sx,int sy) 60 { 61 queue<int>q1,q2; 62 int i,j,x,y,xx,yy; 63 LL sum; 64 q1.push(sx),q2.push(sy); 65 u[sx][sy]=1;sum=a[sx][sy]; 66 while(!q1.empty()) 67 { 68 x=q1.front(),q1.pop(); 69 y=q2.front(),q2.pop(); 70 for(j=0;j<4;j++) 71 { 72 xx=x+dx[j],yy=y+dy[j]; 73 if(xx<1 || xx>nn || yy<1 || yy>mm || u[xx][yy])continue; 74 u[xx][yy]=1; 75 q1.push(xx);q2.push(yy); 76 sum+=a[xx][yy]; 77 } 78 } 79 s[++lll]=sum; 80 } 81 void print() 82 { 83 int i; 84 if(lll==0){puts("0\n0");return;} 85 sort(s+1,s+1+lll); 86 printf("%d\n",lll); 87 for(i=1;i<=lll;i++) 88 printf("%lld%c",s[i],i==lll?‘\n‘:‘ ‘); 89 } 90 int main() 91 { 92 #ifndef ONLINE_JUDGEW 93 // freopen("1.txt","r",stdin); 94 // freopen("2.txt","w",stdout); 95 #endif 96 int i,j,k; 97 int x,y,z; 98 // init(); 99 // for(scanf("%d",&cass);cass;cass--) 100 for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 101 // while(~scanf("%s",s)) 102 // while(~scanf("%d%d",&n,&m)) 103 { 104 lll=0;mem(u,0); 105 printf("Case #%d:\n",cass); 106 scanf("%d%d%d",&n,&m,&nc); 107 for(i=1;i<=nc;i++) 108 { 109 scanf("%d%d",&x,&y); 110 if(x>n || y>m){i--,nc--;continue;} 111 c[i].x=x,c[i].y=y; 112 } 113 for(i=1;i<N+N;i++)rr[i]=r[i]=1; 114 nn=n,mm=m; 115 sort(c+1,c+1+nc,cmp1); 116 for(i=1;i<=nc;i++) 117 { 118 if(c[i].x-c[i-1].x>2) 119 { 120 nn-=c[i].x-c[i-1].x-2; 121 c[i].x1=c[i-1].x1+2; 122 r[c[i-1].x1+1]*=c[i].x-c[i-1].x-1; 123 } 124 else c[i].x1=c[i-1].x1+c[i].x-c[i-1].x; 125 } 126 if(nn!=c[nc].x1)r[c[nc].x1+1]*=nn-c[nc].x1,nn=c[nc].x1+1; 127 128 sort(c+1,c+1+nc,cmp2); 129 for(i=1;i<=nc;i++) 130 { 131 if(c[i].y-c[i-1].y>2) 132 { 133 mm-=c[i].y-c[i-1].y-2; 134 c[i].y1=c[i-1].y1+2; 135 rr[c[i-1].y1+1]*=c[i].y-c[i-1].y-1; 136 } 137 else c[i].y1=c[i-1].y1+c[i].y-c[i-1].y; 138 } 139 if(mm!=c[nc].y1)rr[c[nc].y1+1]*=mm-c[nc].y1,mm=c[nc].y1+1; 140 141 for(i=1;i<=nc;i++)u[c[i].x1][c[i].y1]=1; 142 for(i=1;i<=nn;i++) 143 for(j=1;j<=mm;j++) 144 a[i][j]=r[i]*rr[j]; 145 for(i=1;i<=nn;i++) 146 { 147 for(j=1;j<=mm;j++) 148 { 149 if(u[i][j])continue; 150 spfa(i,j); 151 } 152 } 153 print(); 154 } 155 return 0; 156 } 157 /* 158 // 159 160 // 161 */
HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。