Rokua OJ P1141 01 Maze Problem Solving Report
by Medalplus
"title 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 description"
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 description"
The output includes m lines, and the corresponding answer is output for each query.
Analysis
This question compares the pit father
The problem is to ask for the number of Unicom blocks where a point resides .
Then I think of the Tarjan and the collection and the
The result looked under the range n<=1000,m<=100000, then gave up ...
Think about it, it's a BFS.
Then find that the time complexity is O (n2m)
And then the tle, we have to consider optimizing the
We find that the number of squares that can go on the path of a point is the same !
So that means we can preprocess the entire graph.
It reminds us of Flood_fill.
Then the author uses the DFS approach
Then the stack, found that the full picture of the 1000*1000 (all 1 or 0) will reach 106 layers, so it will explode
This time may consider the BFS way Flood_fill
Then on the tle, I found the main slow time is to take the STL's queue
Special time consuming, and then write a queue, and then add the card constant read into
It's AC ....
A hint of sadness
"Code"
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 #defineRep (I,l,r) for (i=l;i<=r;i++)7 #defineRead scanf8 #definePrint printf9 Ten Const intmaxn=1001; One Const intmaxm=10000001; A - structxoy{ - intx, y; the }; - - CharGRAPH[MAXN][MAXN]; - intRECORD[MAXM],ID[MAXN][MAXN]; + xOy Q[MAXM]; - intFront,tail; + intN,m;//size of the board; question numbers A intDatax,datay; at - voidBFsintXintYintindex) { -Front=tail=1; -Tail=2; -q[1]=(structxOy) {x, y}; - xOy head; in while(front!=tail) { -Head=Q[front]; to++Front; + if(Id[head.x][head.y])Continue; -id[head.x][head.y]=index; therecord[index]++; * if(head.x-1>=1&& graph[head.x-1][head.y]!=Graph[head.x][head.y]) $Q[tail++]= (structXOy) {head.x-1, head.y};Panax Notoginseng if(head.y-1>=1&& graph[head.x][head.y-1]!=Graph[head.x][head.y]) -Q[tail++]= (structXOy) {head.x,head.y-1}; the if(head.x+1<=n && graph[head.x+1][head.y]!=Graph[head.x][head.y]) +Q[tail++]= (structXOy) {head.x+1, head.y}; A if(head.y+1<=n && graph[head.x][head.y+1]!=Graph[head.x][head.y]) theQ[tail++]= (structXOy) {head.x,head.y+1}; + } - } $ $ intMain () { -Read"%d%d",&n,&m); - inti,j,index=1; theRep (I,1, N) -Rep (J,1, N)WuyiCin>>Graph[i][j]; theRep (I,1, N) -Rep (J,1, N) Wu { - BFS (i,j,index); About if(Record[index]) index++; $ } -Rep (I,1, M) { -Read"%d%d",&datax,&Datay); -Print"%d\n", Record[id[datax][datay]]); A } + return 0; the}
Rokua OJ P1141 01 Maze Problem Solving Report