#include <stdio.h>
#include <string.h>
#include <cstring>
#include <vector>
#define W 100
#define H 100
#define WH (W*H)
typedef struct point
{
int x;
int y;
} Point;
typedef struct pair{
Point a;
Point b;
}Pair;
using namespace std;
int w,h;
char path[WH];
int len;
int m;
int graphic[W][H];
int gra[W][H];
int go[W][H];
int desx,desy;
vector<Pair> pairs;
void searchDestination(vector<Point> &v, const int step)
{
vector<Point> nv;
for (vector<Point>::iterator it = v.begin(); it!= v.end(); ++it)
{
const Point& point = *it;
int sx = point.x;
int sy = point.y;
if (!(go[sx][sy] & 1))
{
if (graphic[sx][sy-1]==0)
{
Point pp;
pp.x = sx;
pp.y = sy -1;
graphic[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
if (!(go[sx][sy] & 2))
{
if (graphic[sx][sy+1]==0)
{
Point pp;
pp.x = sx;
pp.y = sy +1;
graphic[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
if (!(go[sx][sy] & 4))
{
if (graphic[sx-1][sy]==0)
{
Point pp;
pp.x = sx-1;
pp.y = sy;
graphic[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
if (!(go[sx][sy] & 8))
{
if (graphic[sx+1][sy]==0)
{
Point pp;
pp.x = sx+1;
pp.y = sy;
graphic[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
}
if (!nv.empty())
searchDestination(nv,step+1);
}
void searchStart(vector<Point> &v, const int step)
{
vector<Point> nv;
for (vector<Point>::iterator it = v.begin(); it!= v.end(); ++it)
{
const Point& point = *it;
int sx = point.x;
int sy = point.y;
if (!(go[sx][sy] & 1))
{
if (gra[sx][sy-1]==0)
{
Point pp;
pp.x = sx;
pp.y = sy -1;
gra[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
if (!(go[sx][sy] & 2))
{
if (gra[sx][sy+1]==0)
{
Point pp;
pp.x = sx;
pp.y = sy +1;
gra[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
if (!(go[sx][sy] & 4))
{
if (gra[sx-1][sy]==0)
{
Point pp;
pp.x = sx-1;
pp.y = sy;
gra[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
if (!(go[sx][sy] & 8))
{
if (gra[sx+1][sy]==0)
{
Point pp;
pp.x = sx+1;
pp.y = sy;
gra[pp.x][pp.y]= step;
nv.push_back(pp);
}
}
}
if (!nv.empty())
searchStart(nv,step+1);
}
bool onePath(int sx,int sy,int step)
{
if (sx==0 && sy==0)
return true;
int nx,ny;
int count =0;
if (!(go[sx][sy] & 1))
{
if (graphic[sx][sy-1]==step)
{
nx = sx;
ny = sy -1;
count++;
}
}
if (!(go[sx][sy] & 2))
{
if (graphic[sx][sy+1]==step)
{
nx = sx;
ny = sy +1;
count++;
}
}
if (!(go[sx][sy] & 4))
{
if (graphic[sx-1][sy]==step)
{
nx = sx-1;
ny = sy;
count++;
}
}
if (!(go[sx][sy] & 8))
{
if (graphic[sx+1][sy]==step)
{
nx = sx+1;
ny = sy;
count++;
}
}
if (count !=1)
return false;
return onePath(nx,ny, step-1);
}
bool checkUnique()
{
vector<Point> v;
Point p;
p.x =0;
p.y =0;
v.push_back(p);
graphic[0][0] = 1;
searchDestination(v,2);
if (graphic[desx][desy]== len+1 && onePath(desx,desy,len))
{
return true;
}
return false;
}
bool checkNecessary(){
vector<Point> v;
Point p;
p.x =desx;
p.y =desy;
v.push_back(p);
gra[desx][desy] = 1;
searchStart(v,2);
for (vector<Pair>::iterator it = pairs.begin();it!= pairs.end();++it){
const Pair& pr= *it;
int s1 =gra[pr.a.x][pr.a.y];
int s2 =graphic[pr.b.x][pr.b.y];
int s3=gra[pr.b.x][pr.b.y];
int s4 =graphic[pr.a.x][pr.a.y];
if ((s1==0 || s2==0 || s1 + s2-1> len) && (s3==0 || s4==0 || s3 + s4-1> len))
{
return false;
}
}
return true;
}
int main()
{
// freopen("data.txt","r",stdin);
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d %d",&w, &h);
scanf("%s", path);
len = strlen(path);
desx=0;
desy=0;
for (int i=0; i<len; i++)
{
switch (path[i])
{
case 'U':
desy++;
break;
case 'D':
desy--;
break;
case 'R':
desx++;
break;
case 'L':
desx--;
break;
}
}
memset(go,0,sizeof(go));
memset(graphic,0,sizeof(graphic));
memset(gra,0,sizeof(graphic));
for (int i=0; i<w; i++)
{
go[i][0] |=1; //can't down
go[i][h-1] |= 2; //can't up
}
for (int i=0; i<h; i++)
{
go[0][i] |=4; //can't left
go[w-1][i] |= 8; //can't right
}
scanf("%d", &m);
Pair pr;
pairs.clear();
int x1,x2,y1,y2;
for (int i=0; i<m; i++)
{
scanf("%d %d %d %d",&x1, &y1,&x2,&y2);
pr.a.x = x1;
pr.b.x = x2;
pr.a.y = y1;
pr.b.y = y2;
pairs.push_back(pr);
if (x1 == x2)
{
if (y1 > y2)
{
go[x1][y1] |=1; //can't down
go[x2][y2] |=2; //can't up
}
else
{
go[x1][y1] |=2; //can't up
go[x2][y2] |=1; //can't down
}
}
else
{
if (x1 > x2)
{
go[x1][y1] |=4; //can't left
go[x2][y2] |=8; //can't right
}
else
{
go[x1][y1] |=8; //can't right
go[x2][y2] |=4; //can't left
}
}
}
if (checkUnique() && checkNecessary())
{
puts("CORRECT");
}
else
{
puts("INCORRECT");
}
}
return 0;
}
poj1024
最新推荐文章于 2022-10-24 06:00:00 发布