POJ-1475Pushing Boxes(貌似测试数据有问题)

本文探讨了一道复杂的图论搜索题目,并采用双BFS算法进行求解。通过对比测试数据,发现了部分测试案例答案存在错误。文章分享了具体的实现代码及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天刚好做到《数据结构编程实验》这本书中的图论部分,恰好遇到这个比较比较难的搜索题,本来最开始放弃的,但是后来想想觉得挺有意思的,就做了。我用的双重BFS的方法进行做,但交上去怎么都WA,最开始确实是有问题,修改后依然WA,后来通过比对书中光盘中所提供的测试数据,发现似乎并不是我代码的问题,某些数据给的答案是错的。我把代码贴出来吧,希望大家都看看到底错哪了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#define MAX 30
using namespace std;
struct node
{
    int px,py,prex,prey,pos,len,isp;
    char pway[1000];
}b[MAX][MAX];
int r,c,sx,sy,ex,ey,bx,by;
char map[MAX][MAX],strway[10]={"snewSNEW"};
int movex[4]={1,-1,0,0},movey[4]={0,0,1,-1};
bool use[MAX][MAX];
bool CanGo(int bx,int by,int sx,int sy,int ex,int ey)
{
    int used[MAX][MAX],px[MAX][MAX],py[MAX][MAX],pos[MAX][MAX];
    memset(used,0,sizeof(used));
    memset(px,0,sizeof(px));
    memset(py,0,sizeof(py));
    queue<int> x,y;
    x.push(sx);
    y.push(sy);
    used[sx][sy]=1;
    int flag=0;
    while(!x.empty())
    {
	int itx=x.front();
	x.pop();
	int ity=y.front();
	y.pop();
	for(int i=0;i<4;i++)
	{
	    int tx=itx+movex[i];
	    int ty=ity+movey[i];
	    if(used[tx][ty]||map[tx][ty]=='#')
		continue;
	    px[tx][ty]=itx;
	    py[tx][ty]=ity;
	    pos[tx][ty]=i;
	    used[tx][ty]=1;
	    x.push(tx);
	    y.push(ty);
	    if(tx==ex&&ty==ey)
	    {
		flag=1;
		break;
	    }
	}
	if(flag)
	{
	    while(!x.empty())
	    {
		x.pop();y.pop();
	    }
	    int xx=ex,yy=ey,cou=0;
	    char ita[1000];
	    while(!(xx==sx&&yy==sy))
	    {
		ita[cou++]=strway[pos[xx][yy]];
		int ita=px[xx][yy],itb=py[xx][yy];
		xx=ita;yy=itb;
	    }
	    for(int i=0;i<cou;i++)
		b[bx][by].pway[i]=ita[i];
	    b[bx][by].pway[cou]='\0';
	    b[bx][by].len+=cou;
	    return true;
	}
    }
    return false;
}
int main()
{
    int cas=1;
    while(scanf("%d%d",&r,&c)!=EOF)
    {
	if(r==0&&c==0)
	    break;
	memset(use,0,sizeof(use));
	memset(b,0,sizeof(b));
	for(int i=0;i<=c+1;i++)
	    map[0][i]=map[r+1][i]='#';
	for(int i=0;i<=r+1;i++)
	    map[i][0]=map[i][c+1]='#';
	for(int i=1;i<=r;i++)
	{
	    getchar();
	    for(int j=1;j<=c;j++)
	    {
		scanf("%c",&map[i][j]);
		if(map[i][j]=='S')
		{
		    sx=i;
		    sy=j;
		}
		else if(map[i][j]=='T')
		{
		    ex=i;
		    ey=j;
		}
		else if(map[i][j]=='B')
		{
		    bx=i;
		    by=j;
		}
	    }
	}
	queue<int> x,y;
	x.push(bx);
	y.push(by);
	use[bx][by]=1;
	b[ex][ey].len=1<<28;
	b[ex][ey].pos=-1;
	b[bx][by].px=sx;
	b[bx][by].py=sy;
	b[bx][by].len=0;
	while(!x.empty())
	{
	    int itx=x.front();
	    x.pop();
	    int ity=y.front();
	    y.pop();
	    for(int i=0;i<4;i++)
	    {
		int tx=itx+movex[i];
		int ty=ity+movey[i];
		if(use[tx][ty]||map[tx][ty]=='#')
		    continue;
		if(b[itx][ity].px+movex[i]==tx-movex[i]&&b[itx][ity].py+movey[i]==ty-movey[i])
		{
		    if(tx==ex&&ty==ey&&b[ex][ey].len<=b[itx][ity].len+1)
			continue;
		    b[tx][ty].px=tx-movex[i];
		    b[tx][ty].py=ty-movey[i];
		    b[tx][ty].pos=i;
		    b[tx][ty].prex=itx;
		    b[tx][ty].prey=ity;
		    b[tx][ty].len+=b[itx][ity].len+1;
		    if(map[tx][ty]!='T')
		    {
			use[tx][ty]=1;
			x.push(tx);
			y.push(ty);
		    }
		}
		else 
		{
		    map[itx][ity]='#';
		    if(map[itx-movex[i]][ity-movey[i]]!='#'&&CanGo(tx,ty,b[itx][ity].px,b[itx][ity].py,itx-movex[i],ity-movey[i]))
		    {	
			if(tx==ex&&ty==ey&&b[ex][ey].len<=b[itx][ity].len+1)
			    continue;
			b[tx][ty].isp=1;
			b[tx][ty].px=tx-movex[i];
			b[tx][ty].py=ty-movey[i];
			b[tx][ty].pos=i;
			b[tx][ty].prex=itx;
			b[tx][ty].prey=ity;
			b[tx][ty].len+=b[itx][ity].len+1;
			if(map[tx][ty]!='T')
			{
			    use[tx][ty]=1;
			    x.push(tx);
			    y.push(ty);
			}
		    }
		    map[itx][ity]='.';
		}

	    }
	}
	int itx=ex,ity=ey;
	char ans[1000];
	int cou=0;
	printf("Maze #%d\n",cas++);
	if(b[itx][ity].pos!=-1)
	{
	    while(!(itx==bx&&ity==by))
	    {
		if(!b[itx][ity].isp)
		    ans[cou++]=strway[4+b[itx][ity].pos];
		else
		{
		    ans[cou++]=strway[4+b[itx][ity].pos];		    
    		    for(int i=0;i<strlen(b[itx][ity].pway);i++)
			ans[cou++]=b[itx][ity].pway[i];

		}
		int ita=b[itx][ity].prex;
		int itb=b[itx][ity].prey;
		itx=ita;ity=itb;
	    }
	    ans[cou]='\0';
	    for(int i=strlen(ans)-1;i>=0;i--)
		printf("%c",ans[i]);
	    printf("\n");
	}
	else
	    printf("Impossible.\n");
	printf("\n");	
    }
    return 0;
}
书中提供的测试数据:
输入:

1 7
SB....T
1 7
SB..#.T
7 11
###########
#T##......#
#.#.#..####
#....B....#
#.######..#
#.....S...#
###########
8 4
....
.##.
.#..
.#..
.#.B
.##S
....
###T
5 11
#T##......#
#.#.#..####
#....B....#
#.######..#
#.....S...#
4 5
##T..
.SB..
.#...
...##
7 8
........
.######.
.....T#.
#.#####.
#...BS..
#..#####
########
10 5
...##
.#.##
.#.##
.#.##
.#.##
.#.##
.#...
SB...
##...
##T..
1 3
STB
1 3
SBT
1 3
TBS
3 1
T
B
S
3 3
S..
.B.
T.#
12 14
S.#...........
.B.#..........
....#.........
#....#........
.#....#.......
..#....#......
...#....#.....
....#....#....
.....#....#...
......#....#..
.......#....#.
........#...T#
11 19
....#####..........
....#...#..........
....#...#..........
..###..B##.........
..#......#.........
###.#.##.#...######
#...#.##.#####T...#
#.................#
####..###.#S##....#
...#......#########
...########........
20 20
S...................
.##################.
..................#.
.#.#..............#.
.#.###########.#..#.
.#.#...........#..#.
.#.#..########.#..#.
.#.#.........#.#..#.
.#.########..#.#..#.
.#.#.....B...#.#....
.#.#.######..#.#..#.
.#.#.........#.#..#.
.#.#..########.#..#.
.#.#............#.#.
.#.#............#.#.
.#.#.############.#.
..................#.
.#................#.
.##################.
...................T
20 20
....................
.##################.
..................#.
#################.#.
................#.#.
.##############.#.#.
.#............#.#.#.
.#.##########.#.#.#.
.#.#........#.#.#.#.
.#.#.####...#.#.#.#.
.#.#.#..SB..#.#.#.#.
.#.#.#.##T..#.#.#.#.
.#.#.#.######.#.#.#.
.#.#.#........#.#.#.
.#.#.##########.#.#.
.#.#............#.#.
.#.##############.#.
.#................#.
.##################.
....................
20 20
....................
.##################.
..................#.
#################.#.
................#.#.
.##############.#.#.
.#............#.#.#.
.#.##########.#.#.#.
.#.#........#.#.#.#.
.#.#.####...#.#.#.#.
.#.#.#.SB...#.#.#.#.
.#.#.#..#.###.#.#.#.
.#.#.#.....##.#.#.#.
.#.#.#.T.#....#.#.#.
.#.#.##########.#.#.
.#.#............#.#.
.#.##############.#.
.#................#.
.##################.
....................
20 20
#################..#
SB.................#
################.#.#
.................#.#
..############.#.#.#
#..............#.#.#
#.#.########.#.#.#.#
#.#..........#.#.#.#
#.#.#.####.#.#.#.#.#
#.#.#......#.#.#.#.#
#.#.#.#.##.#.#.#.#.#
#.#.#.#.##T#.#.#.#.#
#.#.#.#........#.#.#
#.#.#.#.######.#.#.#
#.#.#............#.#
#.#.#.##########.#.#
#.#................#
#.#.##############..
#...................
#..#################
20 20
..#.................
.#SB.#...........##.
.#.#.#.#.#####.###..
.#.....#.....#...#..
.#.###.#...#.....#.#
.#...#####.#####.#..
.....#.....#.#...##.
.###.#.#...#.#......
.#...#.#.###.#.####.
.#.....#.....#...#..
.#.###.#...#.....#.#
.#...#####.#####.#..
.....#.....#.#...##.
.###.#.#...#.#......
.#...#.#.###.#.###.#
.#.....#.....#...#..
.#.###.#...#.....##.
.#...#####.#####.#..
...........#.......#
.#########...####..T
20 20
S...................
.T..................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
..................B.
....................
20 20
T...................
.S..................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
...................B
20 20
####################
#SB#################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
###################T
20 1
S
.
B
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
T
20 3
S..
##.
...
.##
...
##.
...
.##
...
##.
...
.##
...
##.
...
.##
...
##.
...
.BT
1 20
TB.................S
0 0

输出:

Maze #1
EEEEE


Maze #2
Impossible.


Maze #3
eennwwWWWWeeeeeesswwwwwwwnNN


Maze #4
swwwnnnnnneeesssSSS


Maze #5
eennwwWWWWeeeeeesswwwwwwwnNN


Maze #6
wsseenN


Maze #7
WWWswNNseeeeeennnnwwwwwwwssEEEE


Maze #8
EwnnnnnnneessssssSS


Maze #9
Impossible.


Maze #10
E


Maze #11
W


Maze #12
N


Maze #13
eesWnwS


Maze #14
eSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEneSwsEE


Maze #15
nwwwnnnwwnneSwwsssseeennnWWnwSSSnnwwssseEEEEEEEEEEseNenW


Maze #16
sseesssssssssssssseennnnnnneeeeEEwwwwwwsseeeeeeenennwSSesWWWWWWWeeeeeennwwwwwwwsSSSSSneeeeeeeeenennnnnnnnnnwwwwwwwwwwnwwsssssssssssssseEEEEEEEEEEEEEseNNNNNNNwnEEwnnnnnnwwwwwwwwwwwwwnwwwwnneeeeeeeeeeeeeeeeeeessssssssSSSSSSSSSS


Maze #17
wwssseeeeeeennnnnnnwwwwwwwwwwwssssssssssseeeeeeeeeeeeeeennnnnnnnnnnnnnnwwwwwwwwwwwwwwwwwnneeeeeeeeeeeeeeeeeeessssssssssssssssssswwwwwwwwwwwwwwwwwwwnnnnnnnnnnnnnnneeeeeeeeeeeeeeessssssssssswwwwwwwwwwwnnnnnnneeeeesS


Maze #18
EwsseeeseeennnnnnnwwwwwwwwwwwssssssssssseeeeeeeeeeeeeeennnnnnnnnnnnnnnwwwwwwwwwwwwwwwwwnneeeeeeeeeeeeeeeeeeessssssssssssssssssswwwwwwwwwwwwwwwwwwwnnnnnnnnnnnnnnneeeeeeeeeeeeeeessssssssssswwwwwwwwwwwnnnnnnneeeeesSSnnnwwwwwssssssseeeeeeeeeeennnnnnnnnnnwwwwwwwwwwwwwwwssssssssssssssseeeeeeeeeeeeeeeeeeennnnnnnnnnnnnnnnnnnwwwwwwwwwwwwwwwwwwwsseeeeeeeeeeeeeeeeessssssssssssssswwwwwwwwwwwwwwwnnnnnnnnnnneeeeeeeeeeessssssswwwnWWennwwsS


Maze #19
EEEEEEEEEEEEEEEEEneSSSSSSSSSSSSSSSSSesWWWWWWWWWWWWWWWWWswNNNNNNNNNNNNNNNwnEEEEEEEEEEEEEEEwssssssssssseesseennnnnnnnnnnnnnnwwsSSSSSSSSSSSSSnnnnnnnnnnnnnneessssssssssssssswWWWWWWWWWWWWWennnnnnnnnwwnnwwssssssssssssseenNNNNNNNNNNNsssssssssssswwnnnnnnnnnnnnneEEEEEEEEEEEwwwwwwwwwwwwnneeeeeeeeeeeeesSSSSSSSSSnnnnnnnnnneessssssssssswWWWWWWWWWeeeeeeeeeesswwwwwwwwwwwnNNNNNNNsssssssswwnnnnnnnnneEEEEEEEwwwwwwwwnneeeeeeeeesSSSSSnnnnnneessssssswWWWWWeeeeeesswwwwwwwnNNNsssswwnnnnneEEEwwwwnneeeeesSS


Maze #20
EneSSnneessswWWennwwsSSSesesswwsssswwnnnnnneEEneSSSnnnwwnneeeenneesseessswwwwssswWWnwSSSnnenennwwwwsssssseEEneSSSwwssseeseeeeeennwwnnnnwwssswWWnwSSSnnenennwwwwsssssseEEEEEEEEwwwwwnwwnneeeennneessseeeeseeeesswwwwswwNNNssseeneeeennwwwwnwWWswNNNssseesswwwwwwwnwnneeeennneEEseNNNsswwssseeeeseennneennnwwwwnwWWswNNNsseessswwwwssswwnnnnwwnneeeennneEEseNNNwwnnneeeeeessswwnwWWswNNseeeeseennnwwwwnwwwsEEEEEEneSSSnnwwwwwwsseeeeseEEneSSSnnwwnnneeneeesswsssesswwWWnwSSSnneennnwwwwnwwssswwssseeeeseEEneSSSwwssseessseenennwnnwWWnwSSSesesswwwwswwnnnneeseEEneSSnwwwwnwwsssseeneeeEEwnnwwnnneeeessesswSwsE


Maze #21
essesesseeseseseeeesseeessssessessseeNNNNNNNNNNNNNNNNNenWWWWWWWWWWWWWWWWW


Maze #22
Impossible.


Maze #23
Impossible.


Maze #24
sSSSSSSSSSSSSSSSSS


Maze #25
eesswwsseesswwsseesswwsseesswwsseesswwsE


Maze #26
wwwwwwwwwwwwwwwwwW


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值