linux中fork函数详解
http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1541
When rain, nocLyt discovered a magical phenomenon that the rainwater always flow to the low-lying place. |
我以后都在题解里放一小段题目,让人更容易搜到,有没有很有想法!(咦这样好像放全部的题目更容易被搜到啊)(不过那样好像比较乱啊)
[1541] Rainwater 时间限制: 3000 ms 内存限制: 262144 K 问题描述 When rain, nocLyt discovered a magical phenomenon that the rainwater always flow to the low-lying place. Now, there is a N * N grid matrix (four Unicom) and above the first row was constantly raining. I have to tell you some rules: 1. Each grid is solid (represented as black) initially, the rain cannot flow into the solid grid. 2. You can "Open" a grid, if you "Open" that grid, that grid will becomes hollow (represented as white). 3. Rainwater can flow from top to bottom and from left to right (also right to left), but the precondition is that they are both hollow grid. (grid fill with rainwater represented as blue) You can get more information from below picture. Figure: from left to right are executed 50 times, 100 times, 150 times, 204 times "Open" operation. We have three operation: 1. O i j: "Open" the grid (i, j). 2. F i j: You should tell me whether the grid(i, j) filled with rainwater. If yes, you should output 1, otherwise output 0. 4. C: You should tell me whether the rainwater has flow to the last row. If yes, you should output 1, otherwise output 0. Note: The grid matrix from top to bottom number 1 to N, from left to right number 1 to N. 输入 There are multiple test cases, each test case contains two positive integers N (1 <= N <= 5000) and M (1 <= M <= 1000000). Following M lines, each line contain one of the three operation: 1. O i j (1 <= i, j <= N) 2. F i j (1 <= i, j <= N) 3. C 输出 Output the answer. 样例输入 3 7 O 1 1 F 2 1 O 2 1 F 2 1 C O 3 1 C 样例输出 0 1 0 1 提示 无来源 nocLyt @SDAU
我把题目隐藏成一行,这样就不乱,而且好像也能被搜到。不过这样格式有点逗,要看题目还是点链接去看好了。
好下面来说这题Rainwater:
看图比较容易懂意思,就是你的三种操作是开某个孔,查某个孔有没有进水,查最下面一行有没有水。水会从最上面一行出现,然后随便乱流,看最后一个图可以知道它会往上流。
看完题目,这不就是超水的深搜吗!
把地图从1开始,第0行先灌满水。每次开一块的时候,如果四个方向没水,那这一块肯定没水。如果有水,那就把这格填上水,并且深搜一波,把能到的地方都水了。这样每格最多水一次,复杂度超低。然后它询问某个孔的时候就直接答,询问最下面这行的话也直接答,搜的时候记得记最下面这行有没有水,不要在查的时候才查一整行,这个可是要问100W个问题的,地图只有5000*5000,最下面那一行有5000个格子啊,你敢每次搜一下吗!
总之就是搜就过啦,深搜王,我咧哇纳鲁!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define ll long long 6 const int MAXN=5555; 7 const int gx[4]={0,0,-1,1}; 8 const int gy[4]={-1,1,0,0}; 9 int a[MAXN][MAXN]; 10 int n,m,flagc; 11 12 void dfs(int x,int y) 13 { 14 int i; 15 a[x][y]=2; 16 if(x==n) flagc=1; 17 for(i=0;i<4;i++) 18 if(a[x+gx[i]][y+gy[i]]==1) dfs(x+gx[i],y+gy[i]); 19 } 20 21 void open(int x,int y) 22 { 23 int i,j; 24 if(a[x][y]!=0) return; 25 a[x][y]=1; 26 for(i=0;i<4;i++) 27 if(a[x+gx[i]][y+gy[i]]==2){a[x][y]=2;break;} 28 if(a[x][y]==1) return; 29 dfs(x,y); 30 } 31 32 int main() 33 { 34 char c; 35 int x,y,i; 36 while(scanf("%d%d",&n,&m)!=EOF) 37 { 38 memset(a,0,sizeof(a)); 39 for(i=1;i<=n;i++) 40 a[0][i]=2; 41 flagc=0; 42 for(i=0;i<m;i++) 43 { 44 //cout<<i<<"!"<<endl; 45 do{scanf("%c",&c);}while(c==‘\n‘ || c==‘ ‘); 46 if(c!=‘C‘) 47 { 48 scanf("%d%d",&x,&y); 49 if(c==‘O‘) open(x,y); 50 else if (c==‘F‘) 51 { 52 printf("%d\n",a[x][y]==2); 53 } 54 } 55 else 56 { 57 printf("%d\n",flagc); 58 } 59 } 60 } 61 return 0; 62 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。