USACO maze1 BFS

好久不写bfs了,中间失误了好长时间,在扩展新节点的时候一旦扩展成功就应该标记节点为已访问,不然有些情况下会无限扩展队列。题目的输入处理略坑爹,可以先当字符串读进来,然后用一个数组标记每个节点四周墙的情况,最后从两个出口搜索一遍,用流水填充,最后取两遍的较小值,然后取整个地图的最大值作为结果

/*
ID:kevin_s1
PROG:maze1
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath>

using namespace std;

#define INF 9999999
#define MAXH 110
#define MAXW 50
#define MAXHH 250
#define MAXWW 100

//gobal variable====
int H, W;
int HH, WW;
string maze[MAXHH];
int G[MAXH][MAXW];
int Gtmp[MAXH][MAXW];
int wall[MAXH][MAXW][4];

struct entry{
	int x, y;
};

vector<entry> entrys;
int result;
int visited[MAXH][MAXW];

int direct[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
queue<entry> que;
//==================

//function==========
void print(){
	/*
	for(int i = 0; i < HH; i++){
		for(int j = 0; j < WW; j++){
			cout<<maze[i][j];
		}
		cout<<endl;
	}
	*/
	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++){
			cout<<Gtmp[i][j]<<"|"<<G[i][j]<<" ";
		}
		cout<<endl;
	}
}

void BFS(entry start){
	que.push(start);
	G[start.x][start.y] = 1;
	visited[start.x][start.y] = 1;
	while(!que.empty()){
		entry top = que.front();
		que.pop();
		for(int i = 0; i < 4; i++){
			if(wall[top.x][top.y][i] == 1){
				entry nw;
				nw.x = top.x + direct[i][0];
				nw.y = top.y + direct[i][1];
				if(nw.x < 1 || nw.x > H || nw.y < 1 || nw.y > W)
					continue;
				if(visited[nw.x][nw.y] == 0){
					G[nw.x][nw.y] = G[top.x][top.y] + 1;
					que.push(nw);
					visited[nw.x][nw.y] = 1;
				}
			}
		}
	}
	return;
}

//==================

int main(){
	freopen("maze1.in","r",stdin);
	freopen("maze1.out","w",stdout);
	cin>>W>>H;
	HH = 2 * H + 1;
	WW = 2 * W + 1;
	getchar();
	for(int i = 0; i < HH; i++){
		getline(cin, maze[i]);
	}
	memset(wall, 0, sizeof(wall));
	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++){
			if(maze[2*i][2*j-1] == ' ')
				wall[i][j][0] = 1;
			if(maze[2*i-2][2*j-1] == ' ')
				wall[i][j][1] = 1;
			if(maze[2*i-1][2*j-2] == ' ')
				wall[i][j][2] = 1;
			if(maze[2*i-1][2*j] == ' ')
				wall[i][j][3] = 1;
		}
	}

	entry tmp;
	for(int i = 1; i <= H; i++){
		if(wall[i][1][2] == 1){
			tmp.x = i, tmp.y = 1;
			entrys.push_back(tmp);
		}
		if(wall[i][W][3] == 1){
			tmp.x = i, tmp.y = W;
			entrys.push_back(tmp);
		}
	}

	for(int j = 1; j <= W; j++){
		if(wall[1][j][1] == 1){
			tmp.x = 1, tmp.y = j;
			entrys.push_back(tmp);
		}
		if(wall[H][j][0] == 1){
			tmp.x = H, tmp.y = j;
			entrys.push_back(tmp);
		}
	}

	result = 0;
	memset(visited, 0, sizeof(visited));
	memset(G, INF, sizeof(G));
	BFS(entrys[0]);
	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++)
			Gtmp[i][j] = G[i][j];
	}
	memset(visited, 0, sizeof(visited));
	memset(G, INF, sizeof(G));
	BFS(entrys[1]);

	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++){
			G[i][j] = min(G[i][j], Gtmp[i][j]);
			if(G[i][j] > result && G[i][j] != INF)
				result = G[i][j];
		}
	}

	cout<<result<<endl;
	return 0;
}

USACO maze1 BFS,布布扣,bubuko.com

时间: 2024-08-08 13:58:15

USACO maze1 BFS的相关文章

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l

USACO Mother&#39;s Milk(bfs)

题目请点我 题解: 水杯倒水的问题很经典,套路也是一样的,bfs找出所有状态.这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的很方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #

USACO 3.2 msquare 裸BFS

又是个裸BFS... 和西安网赛那道1006一样的,只不过加上了要记录方案.顺便复习map 记录方案直接在bfs队列的结点里加一个vector<int> opt,把从开头一直到当前结点的操作序列记下来 1 /* 2 PROB:msquare 3 LANG:C++ 4 */ 5 6 #include <iostream> 7 #include <vector> 8 #include <algorithm> 9 #include <map> 10 #

USACO 2015 Meeting time BFS+最优剪枝

题目 题目描述 Bessie and her sister Elsie want to travel from the barn to their favorite field, such that they leave at exactly the same time from the barn, and also arrive at exactly the same time at their favorite field. The farm is a collection of N fie

USACO 2.4

USACO 2.4.1 题解: 模拟. 用一个6维数组储存农夫与奶牛当前状态是否出现过,若出现过则表明出现循环,直接输出0,f[农夫x][农夫y][农夫方向][奶牛x][奶牛y][奶牛方向]. 最后注意转弯要算一步. 代码: /* ID:m1599491 PROB:ttwo LANG:C++ */ #include<cmath> #include<cstdio> #include<cstring> #include<iostream> using names

poj 3669 Meteor Shower(bfs)

Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroy

POJ 题目Catch That Cow(BFS)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 52537   Accepted: 16471 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

HDU 2717 Catch That Cow(bfs)

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10575    Accepted Submission(s): 3303 Problem Description Farmer John has been informed of the location of a fugitive cow and want