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. |
我以后都在题解里放一小段题目,让人更容易搜到,有没有很有想法!(咦这样好像放全部的题目更容易被搜到啊)(不过那样好像比较乱啊)
[] Rainwater
时间限制: ms 内存限制: 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: . Each grid is solid (represented as black) initially, the rain cannot flow into the solid grid. . You can "Open" a grid, if you "Open" that grid, that grid will becomes hollow (represented as white). . 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 times, times, times, times "Open" operation. We have three operation: . O i j: "Open" the grid (i, j). . F i j: You should tell me whether the grid(i, j) filled with rainwater. If yes, you should output , otherwise output . . C: You should tell me whether the rainwater has flow to the last row. If yes, you should output , otherwise output . Note: The grid matrix from top to bottom number to N, from left to right number to N. 输入
There are multiple test cases, each test case contains two positive integers N ( <= N <= ) and M ( <= M <= ).
Following M lines, each line contain one of the three operation:
. O i j ( <= i, j <= N)
. F i j ( <= i, j <= N)
. C 输出
Output the answer.
样例输入 O
F
O
F
C
O
C
样例输出 提示
无来源
nocLyt @SDAU
题目
我把题目隐藏成一行,这样就不乱,而且好像也能被搜到。不过这样格式有点逗,要看题目还是点链接去看好了。
好下面来说这题Rainwater:
看图比较容易懂意思,就是你的三种操作是开某个孔,查某个孔有没有进水,查最下面一行有没有水。水会从最上面一行出现,然后随便乱流,看最后一个图可以知道它会往上流。
看完题目,这不就是超水的深搜吗!
把地图从1开始,第0行先灌满水。每次开一块的时候,如果四个方向没水,那这一块肯定没水。如果有水,那就把这格填上水,并且深搜一波,把能到的地方都水了。这样每格最多水一次,复杂度超低。然后它询问某个孔的时候就直接答,询问最下面这行的话也直接答,搜的时候记得记最下面这行有没有水,不要在查的时候才查一整行,这个可是要问100W个问题的,地图只有5000*5000,最下面那一行有5000个格子啊,你敢每次搜一下吗!
总之就是搜就过啦,深搜王,我咧哇纳鲁!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int MAXN=;
const int gx[]={,,-,};
const int gy[]={-,,,};
int a[MAXN][MAXN];
int n,m,flagc; void dfs(int x,int y)
{
int i;
a[x][y]=;
if(x==n) flagc=;
for(i=;i<;i++)
if(a[x+gx[i]][y+gy[i]]==) dfs(x+gx[i],y+gy[i]);
} void open(int x,int y)
{
int i,j;
if(a[x][y]!=) return;
a[x][y]=;
for(i=;i<;i++)
if(a[x+gx[i]][y+gy[i]]==){a[x][y]=;break;}
if(a[x][y]==) return;
dfs(x,y);
} int main()
{
char c;
int x,y,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,,sizeof(a));
for(i=;i<=n;i++)
a[][i]=;
flagc=;
for(i=;i<m;i++)
{
//cout<<i<<"!"<<endl;
do{scanf("%c",&c);}while(c=='\n' || c==' ');
if(c!='C')
{
scanf("%d%d",&x,&y);
if(c=='O') open(x,y);
else if (c=='F')
{
printf("%d\n",a[x][y]==);
}
}
else
{
printf("%d\n",flagc);
}
}
}
return ;
}