Fans and snakes

Source: Internet
Author: User

Fans and snakes

Description

Karles and his friends went to the maze to play. I didn't expect to encounter a huge flood once every 10000000 years. Fortunately, Karles is a pleasure.

The Thinker found that the terrain and floods in the Maze are of the following nature:

① The Maze can be seen as a rectangle matrix of N * M. The coordinates in the upper left corner are (), and the coordinates in the lower right corner are (n, m ),

Each grid (I, j) has an h (I, j) height ).

② The flood starts from (sx, sy). If a grid is flooded, it is surrounded by grids that are lower (or the same) than it.

It will also be drowned.

Now Karles wants to help calculate the number of grids that will not be drowned, and Karles wants to ask if the grid (x, y) is

If it is drowned, "Yes" is output; otherwise, "No" is output ".

Input

The first line contains two integers, n and m.

The number of m in each row in the n rows below. The number of j in the I row indicates the grid height h (I, j ).

The following line contains two integers, sx and sy, indicating the lattice that was initially flooded.

The following row contains an integer q, indicating the number of queries.

Each row of the last q row contains two integers x and y, indicating the query lattice.

Output

The first line of the output is the number of grids that will never be drowned.

The following q rows output "Yes" or "No" (excluding quotation marks) when the grid is drowned)

Example

Maze. in maze. out

3 3

1 2 3

2 3 4

3 4 5

2 2

2

1 2

2 3

5

Yes

No

Hint

For 10% of the data, (sx, sy) is the highest point in the maze.

For 30% of the data, 1 <= N, M <= 5, q = 1.

For 60% of data, 1 <= N, M <= 100, q <= 100.

For 100% of data, 1 <= N, M <= 2000, q <= 1000.

Train of Thought: a very simple board dfs problem, as long as you control the boundary, direction, height, one-step search,

The last query requires a separate vis array.

Range! Range! Range!

I didn't see the code for all memory bursts:

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 using namespace std; 8 int xx[5]={-1,+1,0,0}; 9 int yy[5]={0,0,-1,+1};10 int a[10001][10001];11 int vis[10001][10001];12 int tot;13 int n,m;14 void dfs(int x,int y)15 {16     vis[x][y]=1;17     for(int i=0;i<4;i++)18     {19         int h=a[x][y];20         if(a[x+xx[i]][y+yy[i]]<=h&&vis[x+xx[i]][y+yy[i]]==0&&x+xx[i]>=1&&x+xx[i]<=n&&y+yy[i]>=1&&y+yy[i]<=m)21         {22             tot--;23             vis[x+xx[i]][y+yy[i]]=1;24             dfs(x+xx[i],y+yy[i]);25         }26     }27 }28 int main()29 {30     freopen("maze.in","r",stdin);31     freopen("maze.out","w",stdout);32     scanf("%d%d",&n,&m);33     tot=n*m;34     for(int i=1;i<=n;i++)35     {36         for(int j=1;j<=m;j++)37         {38             scanf("%d",&a[i][j]);39         }40     }41     int sx,sy;42     scanf("%d%d",&sx,&sy);43     dfs(sx,sy);44     int q;45     scanf("%d",&q);46     printf("%d\n",tot-1);47     for(int i=1;i<=q;i++)48     {49         int qx,qy;50         scanf("%d%d",&qx,&qy);51         if(vis[qx][qy]==1)52         {53             printf("Yes\n");54         }55         else56         {57             printf("No\n");58         }59     }60     fclose(stdin);61     fclose(stdout);62     return 0;63 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.