【洛谷】P1746 离开中山路*(bfs搜索)

穿越隧道

有些怪,好像转成整型就会RE。
使用他的字符型,就ac了

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int,int> pii;
const int N = 2e3 + 10;
char g[N][N];
int dis[N][N];
pii q[N*N]; 
//bool st[N][N];
int n,m;
int x1,y1,x2,y2;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int bfs(int sx, int sy,int x2,int y2){
	memset(dis,-1,sizeof(dis));
	dis[sx][sy] = 0;
	int hh = 0, tt = 0;
	q[0] = {sx,sy};
	g[sx][sy] = '1';
	while(hh <= tt){
		pii t = q[hh++];
		for(int i = 0; i < 4; i++){
			int a = t.x + dx[i], b = t.y + dy[i];
			if(a >= 1 && a <= n && b >= 1 && b <= n && g[a][b] == '0'){
				dis[a][b] = dis[t.x][t.y] + 1;
				q[++tt] = {a,b};
				g[a][b] = 1;
			}
		}
	}
	return dis[x2][y2];
}
int main(){
//	freopen("P1746_1.in","r",stdin);
//	freopen("out.out","w",stdout);
	cin >> n;
//	getchar();
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++ ){
			cin >> g[i][j];
			/*不知道为啥,洛谷使用char ch=getchar() 就会RE.本地能够运行出样例结果 
			把RE的数据下载下来,结果是对的,还是RE
			采用字符的方式没啥问题,将字符转为整型就有问题。 
			*/ 
//			char ch = getchar();
//			g[i][j] = (ch - '0');
		}
//		getchar();
	}
//	for(int i = 1; i <= n; i++){
//		for(int j = 1; j<= n; j++){
//			cout << g[i][j] <<" ";
//		}
//		puts("");
//	}
	cin >> x1 >> y1 >> x2 >> y2;
	cout << bfs(x1,y1,x2,y2);
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值