南京信息工程大学第二届程序设计大赛团队赛:I-夏日旅行

题目描述

夏天的时候,ThinkSpirit 实验室去海边玩了。蒙起眼睛的 Crush 举起了木棒,他要在大家的帮助下打西瓜。

沙滩可以被描述成一个 N × M 的网格,其中某些格子为不可移动的障碍物。Crush 总是在格子上,始终在格子上移动,且不会走出边界。西瓜也处于一个格子上,当 Crush 走到西瓜所在格子,他就会举起木棒,重重挥下 —— 啪!

好心的其它成员当然会说出提示,来帮助 Crush ,每个提示都会让 Crush 走向相邻的一个可走的格子。问最少需要多少条提示,才能让 Crush 打到西瓜?

输入描述

第一行两个正整数 N , M ( N , M ⩽ 50 ),表示沙滩的大小。

接下来 N 行,每行 M 个用空格隔开的 0 或 1,表示沙滩的构造。其中,0 表示此格点无障碍,1 表示此格点为不可移动的障碍物。

最后一行输入 4 个整数,分别表示 Crush (起始点)和西瓜(目标点)所在网格的行号与列号。(行号和列号从 1 开始,行号从上往下递增,列号从左往右递增)

输出描述

一个整数,表示需要的最小提示数,如果无论接收多少提示 Crush 都不能抵达西瓜,输出 -1。

样例输入

9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
3 3 4 4 

样例输出

6

题目思路:

这是一道BFS模板题,可以自己定义数据结构

代码1:

#include<bits/stdc++.h>
using namespace std;
struct node 
{ 
	int x,y;
	int step;
} S,T,Node;
int n,m;
int tmp;
const int maxn = 55;
int maze[maxn][maxn];
bool inq[maxn][maxn]={false};
int X[4]={-1,0,0,1};
int Y[4]={0,-1,1,0};

bool test(int x,int y) {
	if(x>n || x<=0 || y>m || y<=0) return false;
	if(maze[x][y]==1) return false;
	if(inq[x][y]==true) return false;
	return true;
}
int BFS() {
	queue<node> q;
	q.push(S);
	while(!q.empty()) {
		node top = q.front();
		q.pop();
		if(top.x == T.x && top.y == T.y) {
			return top.step;
		}
		for(int i=0; i<4; i++) {
			int newX = top.x+X[i];
			int newY = top.y+Y[i];
			if(test(newX,newY)) {
				Node.x = newX;
				Node.y = newY;
				Node.step = top.step +1;
				q.push(Node);
				inq[newX][newY] = true;
			}
		}
	}
	return -1;
}
int main() {
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++) {
			scanf("%d",&tmp);
			if(tmp == 0) {
				maze[i][j] = 0;
			} else {
				maze[i][j] = 1;
			}
		}
	}
	scanf("%d%d",&S.x,&S.y);
	scanf("%d%d",&T.x,&T.y);
	S.step = 0;
	printf("%d\n",BFS());
	return 0;
}

代码2:

#include<bits/stdc++.h>
#include<iostream>
#include<queue>
using namespace std;

typedef pair<int,int> P;
int n,m;
int sx,sy,gx,gy;//起点和终点的坐标
int tmp;
const int INF = 100000000;
const int maxn=55;
char maze[maxn][maxn];
int d[maxn][maxn];
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};

bool test(int x,int y) {
	if(x>n || x<=0 || y>m || y<=0) return false;
	if(maze[x][y]=='1') return false;
	if(d[x][y]!=INF) return false;
	return true;
}

int bfs() {
	queue<P> q;
    for (int i = 1; i <= n; i++)
    {
        /* code */
        for (int j = 1; j <= m; j++)
        {
            /* code */
            d[i][j]=INF;
        }
    }
	q.push(P(sx,sy));
    d[sx][sy]=0;
	while(!q.empty()) {
		P p=q.front();
		q.pop();
		if(p.first == gx && p.second == gy) {
			break;
		}
		for(int i=0; i<4; i++) {
			int nx = p.first+dx[i];
			int ny = p.second+dy[i];
			if(test(nx,ny)) {
				q.push(P(nx,ny));
				d[nx][ny] = d[p.first][p.second]+1;
			}
		}
	}
	return d[gx][gy];
} 
int main() {
	cin>>n>>m;
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++) {
			cin>>maze[i][j];
			}
		}
	}
    cin>>sx>>sy>>gx>>gy;
	cout<<bfs()<<endl;
	return 0;
}
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值