/*
*
* (1)求各点到源点的最小步数(BFS)
* (2)求各点到终点的最小步数(BFS)
* (3)如果点不是给定路径上的点,那么:该点到源点的最小步数+该点到终点的最小步数<给定路径的步数,否则给定路径不是唯一最短的
* (4)如果两相邻点a、b之间存在墙,那么:a到源点的最小步数+1+b到终点的最小步数<=给定路径的步数
* 或者 a到终点的最小步数+1+b到源点的最小步数<=给定路径的步数,否则墙多余
* (5)如果存在点不可达,说明存在墙将该点封闭起来,可以证明墙至少有一块多余
*/
#include < iostream >
#include < string >
#include < queue >
using namespace std;
struct Grid
{
bool inpath; // 是否是路径方格
bool uwal; // 是否有上墙
bool rwal; // 是否有右墙
int scnt; // 到源点步数
int dcnt; // 到终点步数
};
int main( int argc, char ** argv)
{
bool ok;
int w, h, cnt, steps; // 1 <= w, h <= 100
string path;
Grid grid[ 100 ][ 100 ];
queue < pair < int , int > > q;
int t, x, y, desx, desy, x2, y2, i;
for (cin >> t; t > 0 ; -- t)
{
// 初始化数据
cin >> w >> h;
for (y = 0 ; y < h; ++ y)
for (x = 0 ; x < w; ++ x)
{
grid[y][x].inpath = false ;
grid[y][x].uwal = false ;
grid[y][x].rwal = false ;
grid[y][x].scnt = - 1 ;
grid[y][x].dcnt = - 1 ;
}
cin >> path;
x = 0 , y = 0 ;
grid[ 0 ][ 0 ].inpath = true ;
steps = path.size();
for (i = 0 ; i < steps; ++ i)
{
switch (path[i])
{
case ' U ' : ++ y; break ;
case ' D ' : -- y; break ;
case ' L ' : -- x; break ;
case ' R ' : ++ x; break ;
}
grid[y][x].inpath = true ;
}
desx = x, desy = y;
cin >> cnt;
for (i = 0 ; i < cnt; ++ i)
{
cin >> x >> y >> x2 >> y2;
if (x == x2)
if (y + 1 == y2) grid[y][x].uwal = true ;
else grid[y2][x].uwal = true ;
else
if (x + 1 == x2) grid[y][x].rwal = true ;
else grid[y][x2].rwal = true ;
}
// 求各点到源点的最小步数(BFS)
q.push(make_pair( 0 , 0 ));
grid[ 0 ][ 0 ].scnt = 0 ;
while ( ! q.empty())
{
y = q.front().first, x = q.front().second;
if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1 ][x].scnt == - 1 )
{
grid[y + 1 ][x].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y + 1 , x));
}
if ( 0 < y && grid[y - 1 ][x].uwal == false && grid[y - 1 ][x].scnt == - 1 )
{
grid[y - 1 ][x].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y - 1 , x));
}
if ( 0 < x && grid[y][x - 1 ].rwal == false && grid[y][x - 1 ].scnt == - 1 )
{
grid[y][x - 1 ].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y, x - 1 ));
}
if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1 ].scnt == - 1 )
{
grid[y][x + 1 ].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y, x + 1 ));
}
q.pop();
}
// 求各点到终点的最小步数(BFS)
q.push(make_pair(desy, desx));
grid[desy][desx].dcnt = 0 ;
while ( ! q.empty())
{
y = q.front().first, x = q.front().second;
if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1 ][x].dcnt == - 1 )
{
grid[y + 1 ][x].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y + 1 , x));
}
if ( 0 < y && grid[y - 1 ][x].uwal == false && grid[y - 1 ][x].dcnt == - 1 )
{
grid[y - 1 ][x].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y - 1 , x));
}
if ( 0 < x && grid[y][x - 1 ].rwal == false && grid[y][x - 1 ].dcnt == - 1 )
{
grid[y][x - 1 ].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y, x - 1 ));
}
if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1 ].dcnt == - 1 )
{
grid[y][x + 1 ].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y, x + 1 ));
}
q.pop();
}
// 判断路径是否唯一最短,以及墙是否多余
ok = true ;
for (y = 0 ; y < h && ok; ++ y)
for (x = 0 ; x < w && ok; ++ x)
{
if (grid[y][x].scnt == - 1 || grid[y][x].dcnt == - 1 )
ok = false ; // 是否有封闭区域
if (y < h - 1 && grid[y][x].uwal
&& grid[y][x].scnt + grid[y + 1 ][x].dcnt + 1 > steps
&& grid[y][x].dcnt + grid[y + 1 ][x].scnt + 1 > steps)
ok = false ; // 是否上墙多余
if (x < w - 1 && grid[y][x].rwal
&& grid[y][x].scnt + grid[y][x + 1 ].dcnt + 1 > steps
&& grid[y][x].dcnt + grid[y][x + 1 ].scnt + 1 > steps)
ok = false ; // 是否右墙多余
if ( ! grid[y][x].inpath && grid[y][x].scnt + grid[y][x].dcnt <= steps)
ok = false ; // 是否存在更短路径或另一最短路径
}
if (ok) cout << " CORRECT " << endl;
else cout << " INCORRECT " << endl;
}
return 0 ;
}
* (1)求各点到源点的最小步数(BFS)
* (2)求各点到终点的最小步数(BFS)
* (3)如果点不是给定路径上的点,那么:该点到源点的最小步数+该点到终点的最小步数<给定路径的步数,否则给定路径不是唯一最短的
* (4)如果两相邻点a、b之间存在墙,那么:a到源点的最小步数+1+b到终点的最小步数<=给定路径的步数
* 或者 a到终点的最小步数+1+b到源点的最小步数<=给定路径的步数,否则墙多余
* (5)如果存在点不可达,说明存在墙将该点封闭起来,可以证明墙至少有一块多余
*/
#include < iostream >
#include < string >
#include < queue >
using namespace std;
struct Grid
{
bool inpath; // 是否是路径方格
bool uwal; // 是否有上墙
bool rwal; // 是否有右墙
int scnt; // 到源点步数
int dcnt; // 到终点步数
};
int main( int argc, char ** argv)
{
bool ok;
int w, h, cnt, steps; // 1 <= w, h <= 100
string path;
Grid grid[ 100 ][ 100 ];
queue < pair < int , int > > q;
int t, x, y, desx, desy, x2, y2, i;
for (cin >> t; t > 0 ; -- t)
{
// 初始化数据
cin >> w >> h;
for (y = 0 ; y < h; ++ y)
for (x = 0 ; x < w; ++ x)
{
grid[y][x].inpath = false ;
grid[y][x].uwal = false ;
grid[y][x].rwal = false ;
grid[y][x].scnt = - 1 ;
grid[y][x].dcnt = - 1 ;
}
cin >> path;
x = 0 , y = 0 ;
grid[ 0 ][ 0 ].inpath = true ;
steps = path.size();
for (i = 0 ; i < steps; ++ i)
{
switch (path[i])
{
case ' U ' : ++ y; break ;
case ' D ' : -- y; break ;
case ' L ' : -- x; break ;
case ' R ' : ++ x; break ;
}
grid[y][x].inpath = true ;
}
desx = x, desy = y;
cin >> cnt;
for (i = 0 ; i < cnt; ++ i)
{
cin >> x >> y >> x2 >> y2;
if (x == x2)
if (y + 1 == y2) grid[y][x].uwal = true ;
else grid[y2][x].uwal = true ;
else
if (x + 1 == x2) grid[y][x].rwal = true ;
else grid[y][x2].rwal = true ;
}
// 求各点到源点的最小步数(BFS)
q.push(make_pair( 0 , 0 ));
grid[ 0 ][ 0 ].scnt = 0 ;
while ( ! q.empty())
{
y = q.front().first, x = q.front().second;
if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1 ][x].scnt == - 1 )
{
grid[y + 1 ][x].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y + 1 , x));
}
if ( 0 < y && grid[y - 1 ][x].uwal == false && grid[y - 1 ][x].scnt == - 1 )
{
grid[y - 1 ][x].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y - 1 , x));
}
if ( 0 < x && grid[y][x - 1 ].rwal == false && grid[y][x - 1 ].scnt == - 1 )
{
grid[y][x - 1 ].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y, x - 1 ));
}
if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1 ].scnt == - 1 )
{
grid[y][x + 1 ].scnt = grid[y][x].scnt + 1 ;
q.push(make_pair(y, x + 1 ));
}
q.pop();
}
// 求各点到终点的最小步数(BFS)
q.push(make_pair(desy, desx));
grid[desy][desx].dcnt = 0 ;
while ( ! q.empty())
{
y = q.front().first, x = q.front().second;
if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1 ][x].dcnt == - 1 )
{
grid[y + 1 ][x].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y + 1 , x));
}
if ( 0 < y && grid[y - 1 ][x].uwal == false && grid[y - 1 ][x].dcnt == - 1 )
{
grid[y - 1 ][x].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y - 1 , x));
}
if ( 0 < x && grid[y][x - 1 ].rwal == false && grid[y][x - 1 ].dcnt == - 1 )
{
grid[y][x - 1 ].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y, x - 1 ));
}
if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1 ].dcnt == - 1 )
{
grid[y][x + 1 ].dcnt = grid[y][x].dcnt + 1 ;
q.push(make_pair(y, x + 1 ));
}
q.pop();
}
// 判断路径是否唯一最短,以及墙是否多余
ok = true ;
for (y = 0 ; y < h && ok; ++ y)
for (x = 0 ; x < w && ok; ++ x)
{
if (grid[y][x].scnt == - 1 || grid[y][x].dcnt == - 1 )
ok = false ; // 是否有封闭区域
if (y < h - 1 && grid[y][x].uwal
&& grid[y][x].scnt + grid[y + 1 ][x].dcnt + 1 > steps
&& grid[y][x].dcnt + grid[y + 1 ][x].scnt + 1 > steps)
ok = false ; // 是否上墙多余
if (x < w - 1 && grid[y][x].rwal
&& grid[y][x].scnt + grid[y][x + 1 ].dcnt + 1 > steps
&& grid[y][x].dcnt + grid[y][x + 1 ].scnt + 1 > steps)
ok = false ; // 是否右墙多余
if ( ! grid[y][x].inpath && grid[y][x].scnt + grid[y][x].dcnt <= steps)
ok = false ; // 是否存在更短路径或另一最短路径
}
if (ok) cout << " CORRECT " << endl;
else cout << " INCORRECT " << endl;
}
return 0 ;
}