USACO Overfencing,BFS,FloodFill

这一题用floodfill吧,思路还是比较简单的——先沿着四周边界找两个出口,找到出口后就使用floodfill来标记dis数组
标记函数可以使用BFS,也可以使用递归recursive的函数
有人说使用递归会栈溢出,但是只要注意剪枝,是可以成功通过的,如下面第二个mark函数

我的代码:

/* 
ID: wangxin12 
PROG: maze1
LANG: C++ 
*/ 
#include <iostream>
#include <queue>
#include <fstream>
#include <string>
using namespace std;


int W, H; // W <= 38, H <= 100
char map[ 2 * 100 + 1 + 2][ 2 * 38 + 1 + 2];  //第0行0列不使用
int dis[100 + 1][38 + 1];
bool visited[100 + 1][38 + 1];

struct point {
	int height;
	int wide;
	int level;
	bool modify(int h, int w, int l) {
		if(h < 1 || h > H  
			|| w < 1 || w > W) return false;
		
		height = h;
		wide = w;
		level = l;
		return true;
	}
};
queue<struct point> q;

void mark(int h, int w, int level);
void initVisited() {
	for(int m = 0; m <= H + 1; m++) {
		for( int n = 0; n <= W + 1; n++) {
			visited[m][n] = false;
		}
	}
}

int main() {
	ifstream fin("maze1.in");
	ofstream fout("maze1.out");
	
	int i, j;
	char c;
	fin>>W>>H;
	
	//read data
	for(i = 1; i <= 2 * H + 1; i++) {
		for(j = 1; j <= 2 * W + 1; j++) {
			fin.get(c);
			if(c == 10)   //10是LF,换行符;32是space,空格
				fin.get(c);
			map[i][j] = c;
		}
	}

	for(i = 0; i <= H; i++) {
		for(j = 0; j <= W; j++) {
			dis[i][j] = 99999;
		}
	}

	//body
	//find the two exit on the four borders, and then BFS
	for(i = 1, j = 1; j <= W; j++) {
		if(map[1][2 * j] == ' ') {
			mark(1, j, 1);
		}
	}

	for(i = 2 * H + 1, j = 1; j <= W; j++) {
		if(map[2 * H + 1][2 * j] == ' ') {
			mark(H , j, 1);
		}
	}

	for(i = 1, j = 1; i <= H; i++) {
		if(map[2 * i][1] == ' ') {
			mark(i, 1, 1);
		}
	}

	for(i = 1, j = 2 * W + 1; i <= H; i++) {
		if(map[2 * i][2 * W + 1] == ' ') {
			mark(i, W , 1);
		}
	}

	int maxDis = 0;
	for(i = 1; i<= H; i++) {
		for(j = 1; j <= W; j++) {
			if(dis[i][j] > maxDis && map[2*i][2*j] == ' ')
				maxDis = dis[i][j];
		}
	}

	fout<<maxDis<<endl;

	//output
	fin.close();
	fout.close();
	return 0;
}

//bfs

void mark(int h, int w, int level) {
	initVisited();
	if(map[2 * h][2 * w] != ' ' || h < 1 || h > H
		|| w < 1 || w > W) return;

	struct point p;
	p.height = h;
	p.wide = w;
	p.level = level;

	q.push(p);
	struct point pt;
	
	while(!q.empty()) {
		pt = q.front();
		p = q.front();
		q.pop();
		if(visited[pt.height][pt.wide]) 
			continue;
		visited[pt.height][pt.wide] = true;

		if(dis[pt.height][pt.wide] <= pt.level)
			continue;	
		dis[pt.height][pt.wide] = pt.level;
		
		if(map[2 * p.height + 1][2 * p.wide] == ' ') {
			if(pt.modify(p.height + 1, p.wide, p.level + 1))  //south
				q.push(pt);
		}
		if(map[2 * p.height - 1][2 * p.wide] == ' ') {
			if(pt.modify(p.height - 1, p.wide, p.level + 1))
				q.push(pt);   //north
		}
		if(map[2 * p.height][2 * p.wide + 1] == ' ') {
			if(pt.modify(p.height, p.wide + 1, p.level + 1))
				q.push(pt);    //east
		}
		if(map[2 * p.height][2 * p.wide - 1] == ' ') {
			if(pt.modify(p.height, p.wide - 1, p.level + 1))
				q.push(pt);   //west
		}
	}
}


//recursive version 不需要visited数组和queue队列 
/*
void mark(int h, int w, int level) {
	if(map[2 * h][2 * w] != ' ' || h < 1 || h > 2 * H + 1 
		|| w < 1 || w > 2 * W + 1) return;

	if(dis[h][w] <= level) return;
	//如果level小于dis,更新
	dis[h][w] = level;

	if(map[2 * h + 1][2 * w] == ' ') 
		mark(h+1, w, level + 1);  //south
	if(map[2 * h - 1][2 * w] == ' ') 
		mark(h-1, w, level + 1);  //north
	if(map[2 * h][2 * w + 1] == ' ') 
		mark(h, w + 1, level + 1);  //east
	if(map[2 * h][2 * w - 1] == ' ') 
		mark(h, w - 1, level + 1);  //west
} 
*/



…………………………………………(题目描述)…………………………………………

Overfencing
Kolstad and Schrijvers

Farmer John went crazy and created a huge maze of fences out in a field. Happily, he left out two fence segments on the edges, and thus created two “exits” for the maze. Even more happily, the maze he created by this overfencing experience is a `perfect’ maze: you can find a way out of the maze from any point inside it.

Given W (1 <= W <= 38), the width of the maze; H (1 <= H <= 100), the height of the maze; 2*H+1 lines with width 2*W+1 characters that represent the maze in a format like that shown later – then calculate the number of steps required to exit the maze from the `worst’ point in the maze (the point that is `farther’ from either exit even when walking optimally to the closest exit). Of course, cows walk only parallel or perpendicular to the x-y axes; they do not walk on a diagonal. Each move to a new square counts as a single unit of distance (including the move “out” of the maze.

Here’s what one particular W=5, H=3 maze looks like:

Fenceposts appear only in odd numbered rows and and odd numbered columns (as in the example). The format should be obvious and self explanatory. Each maze has exactly two blank walls on the outside for exiting.

PROGRAM NAME: maze1

INPUT FORMAT

Line 1: W and H, space separated
Lines 2 through 2*H+2: 2*W+1 characters that represent the maze

SAMPLE INPUT (file maze1.in)

OUTPUT FORMAT

A single integer on a single output line. The integer specifies the minimal number of steps that guarantee a cow can exit the maze from any possible point inside the maze.

SAMPLE OUTPUT (file maze1.out)

9

The lower left-hand corner is *nine* steps from the closest exit.


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值