列表

详情


NC21276. bearBaby loves sleeping

描述

Sleeping is a favorite of little bearBaby, because the wetness of Changsha in winter is too uncomfortable. One morning, little bearBaby accidentally overslept. The result of being late is very serious. You are the smartest artificial intelligence. Now little bearBaby  asks you to help him figure out the minimum time it takes to reach the teaching building.
The school map is a grid of n*m, each cell is either an open space or a building (cannot pass), and the bedroom of little bearBaby is at (1,1)—— the starting point coordinates.The teaching building is at (x, y)——the target point coordinates, he  can only go up, down, left or right, it takes 1 minute for each step. The input data ensures that the teaching building is reachable.


输入描述

The first line has two positive integers n, m , separated by spaces(1 <= n, m <= 100), n for the row, m for the column
Next there are two positive integers x, y, separated by spaces(1 <= x <= n, 1 <= y <= m) indicating the coordinates of the teaching building
Next is a map of n rows and m columns, 0 indicate a open space and 1 indicate a obstacles.

输出描述

For each test case, output a single line containing an integer giving the minimum time little bearBaby takes to reach the teaching building, in minutes.

示例1

输入:

5 4
4 3
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1

输出:

7

说明:

For the input example, you could go like this:
(1,1)-->(1,2)-->(2,2)-->(2,3)-->(2,4)-->(3,4)-->(4,4)-->(4,3),so the minimum time is 7.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3(3.5.2) 解法, 执行用时: 190ms, 内存消耗: 4732K, 提交时间: 2019-01-07 16:01:49

import queue
dr = [[0, 1], [1, 0], [-1, 0], [0, -1]]
def cal(x):
	x = x[1:len(x)-1]
	pos = x.index(',')
	x = x[0:pos] + x[pos+1:len(x)]
	pos = x.index(',')
	x = x[0:pos] + x[pos+1:len(x)]
	res = list(map(int, x.split(' ')))
	return res
n, m = map(int, input().split(' '))
x, y = map(int, input().split(' '))
x -= 1;y -= 1
s = []
for i in range(n):
	s.append(list(map(int, input().split(' '))))
Q = queue.Queue()
Q.put(str((0, 0, 0)))
while not Q.empty():
	st = cal(Q.get())
	tx = st[0];ty = st[1];tcnt = st[2]
	#print(tx, ty, tcnt)
	if(tx == x and ty == y):
		print(tcnt)
		exit()
	for i in range(4):
		nx = tx + dr[i][0]
		ny = ty + dr[i][1]
		if nx >= 0 and nx < n and ny >= 0 and ny < m and s[nx][ny] == 0:
			s[nx][ny] = 1
			Q.put(str((nx, ny, tcnt+1)))

C(clang 3.9) 解法, 执行用时: 3ms, 内存消耗: 480K, 提交时间: 2019-01-06 16:14:51

#include<stdio.h>
int sun=0,sul,z,l,n,m,min=999999,e[105][105],p[10000][2],r[4]={1,0,-1,0},t[4]={0,1,0,-1};
main()
{
	int i,f;
	int v,jk;
	p[0][0]=p[0][1]=1;
	scanf("%d %d",&n,&m);
	scanf("%d %d",&z,&l);
	for(i=1;i<=n;i++){
		for(f=1;f<=m;f++){
			scanf("%d",&e[i][f]);
		}
	}
	f=0;
	for(jk=0;;jk++){
		sul=sun;
	for(;f<=sun;f++){
		if(p[f][0]==z&&p[f][1]==l) {
			printf("%d",jk);
			return 0;
		}e[p[f][0]][p[f][1]]=1;	
	for(i=0;i<4;i++){
		if(e[p[f][0]+r[i]][p[f][1]+t[i]]==0&&p[f][0]+r[i]>0&&p[f][0]+r[i]<=n&&p[f][1]+t[i]>0&&p[f][1]+t[i]<=m){1;
			p[sul+1][0]=p[f][0]+r[i];
			p[sul+1][1]=p[f][1]+t[i];
			e[p[f][0]+r[i]][p[f][1]+t[i]]=1;
			sul++;
		}
	}
	}
	sun=sul;
}
} 

C++14(g++5.4) 解法, 执行用时: 184ms, 内存消耗: 1000K, 提交时间: 2019-06-01 15:35:33

#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0x3f3f3f3f;
int mp[102][102],ok[102][102];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void dfs(int x,int y,int step){
	ok[x][y]=step;
	for(int i=0;i<4;i++){
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(mp[tx][ty]||step+1>=ok[tx][ty])continue;
		dfs(tx,ty,step+1);
	}
}
int main(){
	memset(ok,0x3f3f3f3f,sizeof ok);
	cin>>n>>m;
	int x,y;
	cin>>x>>y;
	for(int i=0;i<=n+1;i++){
		mp[i][0]=mp[i][m+1]=1;
		for(int j=1;j<=m;j++){
			if(i==0||i==n+1)mp[i][j]=1;
			else cin>>mp[i][j];
		}
	}
	dfs(1,1,0);
	cout<<ok[x][y]<<endl;
}

C++11(clang++ 3.9) 解法, 执行用时: 520ms, 内存消耗: 1116K, 提交时间: 2020-02-18 21:41:19

#include<iostream>
#include<string.h>
using namespace std;
int m,n,a[101][101],h[101][101],fnx,fny,minm=205;
void dfs(int b,int c,int de)
{
	if(b>m||b<1||c>n||c<1||a[b][c]==1) return;
	if(h[b][c]!=0&&h[b][c]<=de) return;
	h[b][c]=de;
	if(b==fnx&&c==fny)
	{
		minm=min(minm,de);
		return;
	}
	dfs(b+1,c,de+1);
	dfs(b-1,c,de+1);
	dfs(b,c+1,de+1);
	dfs(b,c-1,de+1);
}
int main()
{
	memset(h,0,sizeof(h));
	cin>>m>>n>>fnx>>fny;
	for(int i=1;i<=m;i++)
	for(int j=1;j<=n;j++)
	cin>>a[i][j];
	dfs(1,1,0);
	cout<<minm;
	return 0;
}

上一题