[Cpp]
/*
WA
Main ideas:
First, create a level chart for DFS.
Then, determine whether the path is unique and whether the walls are repeated.
*/
# Include <iostream>
// # Define TEST
Using namespace std;
Const int nmax= 105;
Const int mMax = 10100;
Int dis_s [nMax] [nMax], dis_e [nMax] [nMax];
Char s [mMax];
Struct Node
{
Int x1, y1;
Int x2, y2;
Node (){}
Node (int x1, int y1, int x2, int y2): x1 (x1), y1 (y1), x2 (x2), y2 (y2 ){}
} Node [2 * mMax];
Int ind;
Int visit [nMax] [nMax];
Int dir [4] [2] = {0, 1, 0,-1, 1, 0,-1, 0 };
Int W, H;
Int n;
Int step;
Struct Queue
{
Int x, y;
Queue (){}
Queue (int x, int y): x (x), y (y ){}
} Queue [mMax];
Bool isExist (int a, int B, int x, int y) // checks whether a wall exists between (a, B) (x, y)
{
For (int I = 0; I <ind; ++ I)
{
If (node [I]. x1 = a & node [I]. y1 = B & node [I]. x2 = x & node [I]. y2 = y)
Return 1;
}
Return 0;
}
Void bfs (int a, int B, int dis [] [nMax]) // create a hierarchy chart. The original error is: BFS, which requires queue
{
Int front, rear;
Front = rear = 0;
Queue [front ++] = Queue (a, B );
While (rear <front)
{
Int x = queue [rear]. x;
Int y = queue [rear]. y;
Rear ++;
For (int I = 0; I <4; ++ I)
{
Int _ x = x + dir [I] [0];
Int _ y = y + dir [I] [1];
If (_ x> = 0 & _ x <W & _ y> = 0 & _ y <H)
{
If (! IsExist (x, y, _ x, _ y) & dis [_ x] [_ y] =-1)
{
Dis [_ x] [_ y] = dis [x] [y] + 1;
Queue [front ++] = Queue (_ x, _ y );
}
}
}
}
}
Bool isUnique () // determines whether the path is unique
{
For (int I = 0; I <W; ++ I)
{
For (int j = 0; j <H; ++ j)
{
If (! Visit [I] [j])
{
If (dis_s [I] [j] + dis_e [I] [j] <= step) // The sum of the distance from the node to the start point and the end point in the idle position is less than or equal to step.
Return 0;
}
}
}
Return 1;
}
Bool isRepeat ()
{
For (int I = 0; I <n; ++ I)
{
If (dis_s [node [I]. x1] [node [I]. y1] + dis_e [node [I]. x2] [node [I]. y2]> = step & dis_e [node [I]. x1] [node [I]. y1] + dis_s [node [I]. x2] [node [I]. y2]> = step)
// The sum of the distance from both sides of the wall to the start point and the end point is greater than or equal to step.
Return 1;
}
Return 0;
}
Int main ()
{
Freopen ("e: // data.txt", "r", stdin );
// Freopen ("e: // dataout.txt", "w", stdout );
Int T;
Scanf ("% d", & T );
While (T --)
{
Int flag = 0;
Scanf ("% d", & H, & W );
Scanf ("% s", s );
Scanf ("% d", & n );
Int x1, y1, x2, y2;
Ind = 0;
For (int I = 0; I <n; ++ I)
{
Scanf ("% d", & y1, & x1, & y2, & x2 );
Node [ind ++] = Node (x1, y1, x2, y2 );
Node [ind ++] = Node (x2, y2, x1, y1 );
}
Int a, B;
A = B = 0;
Step = strlen (s );
Memset (visit, 0, sizeof (visit ));
Visit [0] [0] = 1;
For (int I = 0; I <step; ++ I)
{
Int x1, y1;
X1 =;
Y1 = B;
If (s [I] = 'l ')
B --;
Else if (s [I] = 'R ')
B ++;
Else if (s [I] = 'U ')
A ++;
Else if (s [I] = 'D ')
A --;
If (isExist (x1, y1, a, B) | a <0 | a> = W | B <0 | B> = H)
{
Flag = 1;
Break;
}
Visit [a] [B] = 1;
}
If (flag)
{
Printf ("INCORRECT \ n ");
Continue;
}
Memset (dis_e,-1, sizeof (dis_e ));
Dis_e [a] [B] = 0;
Bfs (a, B, dis_e );
Memset (dis_s,-1, sizeof (dis_s ));
Dis_s [0] [0] = 0;
Bfs (0, 0, dis_s );
# Ifdef TEST
For (int I = 0; I <W; ++ I)
{
For (int j = 0; j <H; ++ j)
{
Printf ("% d \ t", dis_s [I] [j]);
}
Printf ("\ n ");
}
Printf ("\ n ");
For (int I = 0; I <W; ++ I)
{
For (int j = 0; j <H; ++ j)
{
Printf ("% d \ t", dis_e [I] [j]);
}
Printf ("\ n ");
}
# Endif
Bool test1, test2;
Test1 = isUnique ();
Test2 = isRepeat ();
If (test1 &&! Test2)
Printf ("CORRECT \ n ");
Else
Printf ("INCORRECT \ n ");
}
Return 0;
}
Author: lhshaoren