P1141 01 maze
Title Description
There is only one digital 0 and 1 composed of n- × n-maze grid. If you are in a grid on a 0, then you can move to an adjacent four cells in a cell 1 , the same if you are in a grid 1, then you can move to an adjacent 4 a lattice grid of 0 on the .
Your task is: for a given maze inquiry can begin to move from one cell to the number of grid (including its own).
Input Format
The first 1 Behavior two positive integers n- , m.
Here n lines of n characters, characters can only be 0 or 1, with no spaces between characters.
Subsequently m rows, each row 2 , separated by a space positive integers i, j , corresponding to the first maze i row j a column of the grid, this query initiate movable from cell to cell number.
Output Format
m rows for each query output corresponding answers.
Sample input and output
2 2 01 10 1 1 2 2
4 4
Description / Tips
All the grid up to each other.
For 2 0 % of the data, n- ≤ . 1 0;
For . 4 0 % of the data, n- ≤ . 5 0;
For . 5 0 % of the data, m ≤ . 5;
For . 6 0 % of the data, n- ≤ . 1 0 0 , m ≤ . 1 0 0;
For . 1 0 0 % of the data, n- ≤ . 1 0 0 0 , m ≤ . 1 0 0 0 0 0.
----------------------------------------------------------------------------------------------------------------------------------------------
To thought about this question, we need to pay attention to the following points:
1): for each pass required memory processing, reducing the number of traverse, T is not easy;
2): Do not open unnecessary memset, when a large array, memset command is very time-consuming;
3): when the program runs on a Rockwell valley following the input is \ r \ n two characters, and the program when the unit is tested only following the input \ n a character, it is necessary (after) the original plus a getchar getchar () throw away the rest of the \ n, to prevent the impact on the input map.
1 #include<stdio.h> 2 #include<string.h> 3 int n,m,num,h[3][1000100],u[5]={0,-1,0,1,0},w[5]={0,0,1,0,-1},tap[1010][1010],cu[1000100]; 4 bool map[1010][1010]; 5 int bfs(int,int); 6 int main(){ 7 memset(tap,0,sizeof(tap)); 8 scanf("%d %d",&n,&m); 9 getchar(); 10 getchar(); 11 for(int i=1;i<=n;i++){ 12 for(int j=1;j<=n;j++){ 13 char o; 14 scanf("%c",&o); 15 if(o=='1') map[i][j]=1; 16 else map[i][j]=0; 17 } 18 getchar(); 19 getchar(); 20 } 21 num=0; 22 for(int i=1;i<=m;i++){ 23 int x,y; 24 scanf("%d %d",&x,&y); 25 if(!tap[x][y]){ 26 whether ++ ; 27 Cu [num] = BFS (x, y); 28 printf ( " % d \ n " , since [whether]); 29 } 30 else printf ( " % d \ n " , since [tap [x] [y]]); 31 } 32 return 0 ; 33 } 34 int BFS ( int sx, int sy) { 35 int head = 0 , tail = 1 ; 36 h [ 1 ] [tail] = sx; h [2][tail]=sy; 37 tap[sx][sy]=num; 38 do{ 39 head++; 40 for(int i=1;i<=4;i++){ 41 int x,y; 42 x=h[1][head]+u[i]; 43 y=h[2][head]+w[i]; 44 if(x>0&&y>0&&x<=n&&y<=n&&(!tap[x][y])) 45 if((map[h[1][head]][h[2][head]]&&!map[x][y])||(!map[h[1][head]][h[2][head]]&&map[x][y])){ 46 tail++; 47 h[1][tail]=x; 48 h[2][tail]=y; 49 tap[x][y]=num; 50 } 51 } 52 }while(head<tail); 53 return tail; 54 }