您的位置:首页 > 其它

poj 1024+BFS+Tester Program

2015-09-06 13:59 218 查看
题目大意:给一个rows*column的迷宫,并给出一条路径,和一些迷宫路径之间的墙,让判断这些墙是否能使的从起点到终点的唯一最短路径为给定的路径,并且这些墙中没有多余的墙,其中起点为(0,0),终点不定。

题目解法:暴力+BFS,然后竟然也过了- -|

从起点BFS搜索到终点的最短路径,如果最短路径长度小于给定的路径长度,则不正确;
依次将路径中的路径用墙隔离,如果隔离后仍存在从起点到终点的最短路径,则路径不唯一,不正确;
依次拆除墙,如果拆除后的路径不变,则该墙多余,不正确。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

#define MAXN 110

class Position
{
public:
int x;
int y;

Position(){}
Position(int x, int y):x(x), y(y){}
~Position(){}
};

class GridState
{
public:
int x;
int y;
int step;

GridState(){}
GridState(int x, int y, int step = 0):x(x), y(y), step(step){}
GridState(Position p, int step = 0):x(p.x), y(p.y), step(step){}
~GridState(){}

bool operator==(Position &gs)
{
return (x==gs.x && y==gs.y);
}
};

class Wall
{
public:
int x1, y1;
int x2, y2;

Wall(){}
Wall(int x1, int y1, int x2, int y2):x1(x1), y1(y1), x2(x2), y2(y2){}
~Wall(){}
};

int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
//0->up, 1->down, 2->left, 3->right

bool tmap[MAXN][MAXN][4], flag;

string path;

GridState mend;
vector <Wall > walls;

int rows, columns;

int getDir(char c)
{
switch(c){
case 'U':return 0;
case 'D':return 1;
case 'L':return 2;
case 'R':return 3;
}
return -1;
}

int getDir(int x1, int y1, int x2, int y2)
{
//0->up, 1->down, 2->left, 3->right
if (x1 < x2)return 3;//right
if (x1 > x2)return 2;//left
if (y1 < y2)return 0;//up
if (y1 > y2)return 1;//down
return -1;
}

bool isLegal(GridState tmp)
{
return (tmp.x>=0 && tmp.x<rows && tmp.y>=0 && tmp.y<columns);
}

void getEndState()
{
int x = 0, y = 0;
for (int i = 0; i < (int)path.length(); ++i){
int d = getDir(path[i]);
x += dir[d][0];
y += dir[d][1];
}
mend.x = x;
mend.y = y;
mend.step = path.length();
}

//--------------------Debug--------------------

void printMap()
{
for (int i = columns-1; i >= 0; --i){
for (int j = 0; j < rows; ++j){
printf ("<");
for (int k = 0; k < 4; ++k){
printf (k == 0 ? "%d" : " %d", tmap[j][i][k]);
}
printf ("> ");
}
printf ("\n");
}
}

void PrintChange(int d, GridState gs)
{
if (d == 0){
printf ("UP:\t");
}
else if (d == 1){
printf ("DOWN:\t");
}
else if (d == 2){
printf ("LEFT:\t");
}
else if (d == 3){
printf ("RIGHT:\t");
}
printf ("(%d,%d)->(%d,%d)\n", gs.x-dir[d][0], gs.y-dir[d][1], gs.x, gs.y);
}

//--------------------Debug--------------------

void init()
{
memset (tmap, true, sizeof(tmap));
for (int i = 0; i < rows; ++i){
tmap[i][0][1] = false;
tmap[i][columns-1][0] = false;
}
for (int i = 0; i < columns; ++i){
tmap[0][i][2] = false;
tmap[rows-1][i][3] = false;
}
while (!walls.empty()){
walls.clear();
}
int m;
scanf ("%d", &m);
int x1, y1, x2, y2;
while (m--){
scanf ("%d%d%d%d", &x1, &y1, &x2, &y2);
int d = getDir(x1, y1, x2, y2);
tmap[x1][y1][d] = false;
tmap[x2][y2][d^1] = false;
walls.push_back(Wall(x1, y1, x2, y2));
}
getEndState();
//--------------------Debug--------------------
//printf ("Original:\n");
//printMap();
//printf ("--------------------end--------------------\n");
//--------------------Debug--------------------
}

int bfs(Position s, Position e)
{
bool visited[MAXN][MAXN];
queue <GridState > Qu;
memset (visited, false, sizeof(visited));
visited[s.x][s.y] = true;
Qu.push(GridState(s, 0));
while(!Qu.empty()){
GridState cur = Qu.front();
Qu.pop();
//--------------------Debug--------------------
//printf ("Current Poped:\t(%d,%d)\n", cur.x, cur.y);
//--------------------Debug--------------------
if (cur == e)return cur.step;
for (int i = 0; i < 4; ++i){
GridState tmp(cur.x+dir[i][0], cur.y+dir[i][1], cur.step+1);
//--------------------Debug--------------------
//PrintChange(i, tmp);
//--------------------Debug--------------------
if (isLegal(tmp) && !visited[tmp.x][tmp.y] && tmap[cur.x][cur.y][i]){
visited[tmp.x][tmp.y] = true;
//--------------------Debug--------------------
//printf ("pushed:(%d, %d)\n", tmp.x, tmp.y);
//--------------------Debug--------------------
Qu.push(tmp);
}
}
}
return -1;
}

bool notOnlyOnePath()
{
int x = 0, y = 0;
for (int i = 0; i < (int)path.length(); ++i){
int d = getDir(path[i]);
int nx = x+dir[d][0], ny = y+dir[d][1];
tmap[x][y][d] = false;
tmap[nx][ny][d^1] = false;
//--------------------Debug--------------------
//printMap();
//printf ("DELPATH::\tWall:(%d,%d)->(%d,%d)\n", x, y, nx, ny);
//--------------------Debug--------------------
int pathLength = bfs(Position(0, 0), Position(mend.x, mend.y));
//--------------------Debug--------------------
//printf ("O~D-:>%d\n", pathLength);
//--------------------Debug--------------------
tmap[x][y][d] = true;
tmap[nx][ny][d^1] = true;
x = nx, y = ny;
if (pathLength != -1 && pathLength <= mend.step)return true;
}
return false;
}

bool deleteWall()
{
for (int i = 0; i < (int)walls.size(); ++i){
Position first(walls[i].x1, walls[i].y1);
Position second(walls[i].x2, walls[i].y2);
int d = getDir(first.x, first.y, second.x, second.y);
tmap[first.x][first.y][d] = true;
tmap[second.x][second.y][d^1] = true;
//--------------------Debug--------------------
//printMap();
//printf ("DELWALL::\tROAD:(%d,%d)->(%d,%d)\n", first.x, first.y, second.x, second.y);
//--------------------Debug--------------------
int pathLength = bfs(Position(0, 0), Position(mend.x, mend.y));
//--------------------Debug--------------------
//printf ("O~D:\t%d\n", pathLength);
//--------------------Debug--------------------
if (pathLength >= mend.step && !notOnlyOnePath()){
tmap[first.x][first.y][d] = false;
tmap[second.x][second.y][d^1] = false;
return true;
}
tmap[first.x][first.y][d] = false;
tmap[second.x][second.y][d^1] = false;
}
return false;
}

void solve()
{
flag = true;
int pathLength = bfs(Position(0, 0), Position(mend.x, mend.y));
//--------------------Debug--------------------
//printf ("ORI::\tO~D-:>%d\n", pathLength);
//--------------------Debug--------------------
if (pathLength != mend.step){
flag =false;
}
if (flag && notOnlyOnePath()){
flag = false;
}
if (flag && deleteWall()){
flag = false;
}
if (flag){
puts("CORRECT");
}
else {
puts("INCORRECT");
}
}

int main()
{
//--------------------Debug--------------------
//freopen("out.dat", "w", stdout);
//freopen("in.dat", "r", stdin);
//--------------------Debug--------------------
int t, iCase = 1;
scanf ("%d", &t);
while (t--){
scanf ("%d%d", &rows, &columns);
cin >> path;
init();
//--------------------Debug--------------------
//printf ("Case:%d\n", iCase++);
//--------------------Debug--------------------
solve();
}
//--------------------Debug--------------------
//fclose(stdin);
//fclose(stdout);
//--------------------Debug--------------------
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: