BFS 练习C

C Knights of Ni
Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible.

Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000).

The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery.

In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block.

It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.
Input
Line 1: Two space-separated integers: W and H.

Lines 2…?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map’s description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row.

The integers that describe the map come from this set:
0: Square through which Bessie can travel
1: Impassable square that Bessie cannot traverse
2: Bessie’s starting location
3: Location of the Knights of Ni
4: Location of a shrubbery
Output
Line 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.
Sample Input
8 4
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 4 1 1 1 0
Sample Output
11
心得:要用vis标记走过的路,记得判断下一步的是否走过;输入输出尽量用scanf和printf;能开数组尽量开,不要总是清空重复用同一个数组;re可能是空队列用了pop;记得用完队列要清空;当写有参构造,必须定义无参构造。
这题不能先找出4再由4到2,由4到3,但有很多个4的时候就会超时(还容易越界)。应该从2和3分别出发,同时用二维数组把所有到达位置需要的步数记下,再遍历图,找出从2和从3出发到4的步数最小值即为答案。

#include<cstdio>
#include<queue>

using namespace std;
int n,m,g[1010][1010],vis1[1010][1010],vis2[1010][1010],ans1[1010][1010],ans2[1010][1010];
const int Move[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node {
	int ipos,jpos,step;
	node() {}
	node(int i,int j,int s):ipos(i),jpos(j),step(s) {}
} Start , End ;
queue<node> q;
bool judge1(int x,int y) {
	if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] != 1 && vis1[x][y] == 0)
		return 1;
	return 0;
}
bool judge2(int x,int y) {
	if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] != 1 && vis2[x][y] == 0)
		return 1;
	return 0;
}
void bfs1() {
	int a,b;
	vis1[Start.ipos][Start.jpos] = 1;
	q.push(Start);
	while(!q.empty()) {
		node t;
		t = q.front();
		q.pop();
		for(int k = 0; k < 4; k ++) {
			a = t.ipos + Move[k][0];
			b = t.jpos + Move[k][1];
			if(judge1(a,b)) {
				ans1[a][b] = t.step + 1;
				vis1[a][b] = 1;
				q.push(node(a,b,t.step + 1));
			}
		}
	}
}
	void bfs2() {
		int a,b;
		vis2[End.ipos][End.jpos] = 1;
		q.push(End);
		while(!q.empty()) {
			node t;
			t = q.front();
			q.pop();
			for(int k = 0; k < 4; k ++) {
				a = t.ipos + Move[k][0];
				b = t.jpos + Move[k][1];
				if(judge2(a,b)) {
					ans2[a][b] = t.step + 1;
					vis2[a][b] = 1;
					q.push(node(a,b,t.step + 1));
				}
			}
		}

	}
	int main() {
		while(~scanf("%d%d",&m,&n)) {
			int ans = 1 << 30;
			int k = 0;
			for(int i = 0; i < n; i ++)
				for(int j = 0; j < m; j ++) {
					scanf("%d",&g[i][j]);
					vis1[i][j] = 0;
					vis2[i][j] = 0;
					ans1[i][j] = 1e9;
					ans2[i][j] = 1e9;
					if(g[i][j] == 2) {
						Start = node(i,j,0);
					}
					if(g[i][j] == 3) {
						End = node(i,j,0);
					}
				}
			bfs1();
			bfs2();
			for(int i = 0;i < n;i ++){
				for(int j = 0;j < m;j ++){
					if(g[i][j] == 4){
						int temp = ans1[i][j] + ans2[i][j];
						if(temp < ans )
							ans = temp;
					}
				}
			}
			printf("%d\n",ans);
		}
		return 0;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值