For the processing of the Unicom block

Source: Internet
Author: User

01 Maze Topic Description

There is a labyrinth of nxn that consists only of numbers 0 and 1. If you are on a grid of 0, then you can move to one of the next 4 grid 1, also if you are on a grid of 1, then you can move to a 4 grid adjacent to a certain grid 0.

Your task is: For a given maze, ask how many squares (including itself) you can move from one grid to the beginning.

Input output Format input format:

The 1th behavior of the input is two positive integer n,m.

The following n lines, n characters per line, can only be 0 or 1 characters, with no spaces between characters.

The next m line, each line of 2 positive integers separated by a space, corresponds to the i,j in the maze of row J of a lattice, asking how many squares can be moved from the beginning of the lattice.

Output format:

The output includes m lines, and the corresponding answer is output for each query.

Input and Output Sample input example:

2 2
01
10
1 1
2 2

Output Example 1:

4
4

Description

All squares can reach each other.

For 20% of data, $n ≤10$;

For 40% of data, $n ≤50$;

For 50% of data, $m ≤5$;

For 60% of data, $n ≤100$, $m ≤100$;

For 100% of data, $n ≤1000$, $m ≤100000$.

For this to search questions, how to search is not the focus, the focus is how to deal with a single unicom block.

For the full score data, every time asked to traverse one side is impossible, this life is impossible.

We can dye or use and check the set of maintenance

It is a revelation to us that when dealing with a unicom block of the same nature, the whole block is processed

#include <iostream> #include <queue>using namespace Std;int dp[1010][1010],map[1010][1010];bool exist[ 1010][1010];int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};int n,m,z;queue<int>ox;queue<int>oy;int Find (int    X,int y) {if (Exist[x][y]) return dp[x][y];    Exist[x][y]=true;    int lx,ly;    Ox.push (x); Oy.push (y);    z++;        for (int i=0;i<4;i++) {lx=x+dx[i];ly=y+dy[i]; if (lx>0&&lx<=n&&ly>0&&ly<=n&&!exist[lx][ly]&&map[lx][ly]!=    Map[x][y]) find (lx,ly); } return dp[x][y];}    int main () {Cin.sync_with_stdio (false);    cin>>n>>m;    char input;            for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {cin>>input;            map[i][j]=input-' 0 ';        Dp[i][j]=1;    } int A, b;        for (int i=1;i<=m;i++) {cin>>a>>b;        Find (A, b);        Z=max (Z,dp[a][b]);        cout<<z<<endl; while (!Ox.empty ()) {Dp[ox.front ()][oy.front ()]=z;        Ox.pop (); Oy.pop ();    } z=0; } return 0;}

Handling of the Unicom block

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.