RQNOJ 34紧急救援(简单BFS)

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 1005;

char map[N][N], vis[N][N];
struct node {
	int x;
	int y;
	int c;
}Q[N*N], s, e;
int front, rear;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

int BFS(int n) {
	node first, next;
	front = rear = 0;
	Q[front++] = s;
	memset(vis, true, sizeof(vis));
	vis[s.x][s.y] = false;
	while (rear < front) {
		first = Q[rear++];
		if (first.x==e.x && first.y==e.y) return first.c;
		for (int i=0; i<4; ++i) {
			next.x = first.x + dir[i][0];
			next.y = first.y + dir[i][1];
			next.c = first.c + 1; 
			if (next.x>=0&&next.x<n&&next.y>=0&&next.y<n&&vis[next.x][next.y]&&map[next.x][next.y]=='0') {
				Q[front++] = next;
				vis[next.x][next.y] = false;
			}
		}
	}
}

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		for (int i=0; i<n; ++i) scanf("%s", map[i]);
		scanf ("%d%d%d%d", &s.x, &s.y, &e.x, &e.y);
		--s.x, --s.y, --e.x, --e.y;
		s.c = 0;
		int dis = BFS(n);
		printf ("%d\n", dis);
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/try86/archive/2012/05/13/2498068.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值