POJ 2049 Finding Nemo

搜索练习,在数据预处理部分稍微麻烦一下(即初始化迷宫地图部分),AC时还要注意一下Nemo起始位置可能在迷宫外边,这个时候应该直接返回0。

BFS和DFS都在代码里面了!

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <queue>

using namespace std;

const int MAXLEN = 255;
const int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};

int M, N;
int maze[MAXLEN][MAXLEN];
int door[MAXLEN][MAXLEN][4];// 0 up, 1 down, 3 left, 4 right
int minSteps;

void Dfs(int x, int y, int steps)
{
    if( steps >= minSteps )
	return ;
    if(x >= MAXLEN || y >= MAXLEN || maze[x][y] == -1)
    {
	minSteps = min(minSteps, steps);
	return;
    }
    if( maze[x][y] == 1 ) return;

    maze[x][y] = 1;
    for(int i = 0; i < 4; ++i)
    {
	if( door[x][y][i] )
	{
	    Dfs(x + dx[i], y + dy[i], steps + 1);
	}
    }
    maze[x][y] = 0;
}

void Bfs(int x, int y)
{
    if( x >= MAXLEN || y >= MAXLEN )
    {
	cout << 0 << endl;
	return ;
    }
    queue<pair<pair<int, int>, int> > q;
    q.push( make_pair( make_pair(x, y), 0 ) );
    !maze[x][y]?maze[x][y]=1:NULL;
    while( !q.empty() )
    {
	x = q.front().first.first;
	y = q.front().first.second;
	int level = q.front().second;
	if( maze[x][y] == -1 )
	{
	    cout << level << endl;
	    break;
	}
	q.pop();
	for(int i = 0; i < 4; ++i)
	{
	    if( door[x][y][i] && maze[x+dx[i]][y+dy[i]] != 1)
	    {
		!maze[x+dx[i]][y+dy[i]]?maze[x+dx[i]][y+dy[i]]=1:NULL;
		q.push( make_pair( make_pair(x+dx[i], y+dy[i]), level+1) );
	    }
	}
    }

    if( q.empty() )
	cout << -1 << endl;
}
void InitMaze()
{
    // 0 means (i,j) is part of maze, -1 means no
    for(int i = 0; i < MAXLEN; ++i)
	for(int j = 0; j < MAXLEN; ++j)
	{
	    if( maze[i][j] == 4 )
	    {
		maze[i][j] = 0;
	    }
	    else maze[i][j] = -1;
	}
}
int main()
{
    while( (cin >> M >> N) && (~M || ~N) )
    {
	memset(maze, 0, sizeof(maze));
	memset(door, 0, sizeof(door));

	for(int i = 0; i < M; ++i)
	{
	    int x, y;
	    cin >> x >> y;
	    int t, d;
	    cin >> t >> d;
	    for(int j = 0; j < d; ++j)
	    {
		if( t )
		{
		    maze[x-1][y+j]++;
		    maze[x][y+j]++;
		}
		else 
		{
		    maze[x+j][y-1]++;
		    maze[x+j][y]++;
		}
	    }
	}

	InitMaze();

	for(int i = 0; i < N; ++i)
	{
	    int x, y, t;
	    cin >> x >> y >> t;
	    if( t )
	    {
		door[x-1][y][RIGHT] = 1;
		door[x][y][LEFT] = 1;
	    }
	    else 
	    {
		door[x][y-1][UP] = 1;
		door[x][y][DOWN] = 1;
	    }
	}

	double xBeg, yBeg;
	cin >> xBeg >> yBeg;
	Bfs((int)xBeg, (int)yBeg );
	//minSteps = 0xffff;
	//Dfs((int)xBeg, (int)yBeg, 0);// note that xBeg or yBeg may exceed 200
	/*
	if(minSteps == 0xffff)
	    cout << -1 << endl;
	else 
	    cout << minSteps << endl;
	    */
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值