标签:main for pass positive getch cat printf inf using
链接:https://ac.nowcoder.com/acm/contest/338/B
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.
链接:https://ac.nowcoder.com/acm/contest/338/B
来源:牛客网
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.
First grid in the upper left corner is(1,1)
AC代码:
#pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; inline int read() {int x=0,f=1;char c=getchar();while(c!=‘-‘&&(c<‘0‘||c>‘9‘))c=getchar();if(c==‘-‘)f=-1,c=getchar();while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar();return f*x;} typedef long long ll; const int maxn = 1e4+10; struct node{ int x,y; int st; }; int dir[4][2]={-1,0,1,0,0,-1,0,1}; int vis[maxn][maxn]; int n,m,n1,m1; char a[1000][1000]; int bfs(int u,int v){ node tmp,nex; tmp.x=u; tmp.y=v; tmp.st=0; queue<node>q; q.push(tmp); while(!q.empty()){ tmp=q.front(); q.pop(); for(int i=0;i<4;i++){ nex.x=tmp.x+dir[i][0]; nex.y=tmp.y+dir[i][1]; nex.st=tmp.st+1; if(nex.x<0||nex.y<0||nex.x>=n||nex.y>=m||a[nex.x][nex.y]==‘1‘||vis[nex.x][nex.y]==1)continue; vis[nex.x][nex.y]=1; if(nex.x==n1-1&&nex.y==m1-1){ return nex.st; } q.push(nex); } } } int main() { cin>>n>>m>>n1>>m1; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; } } int ans=bfs(0,0); printf("%d",ans); return 0; }
标签:main for pass positive getch cat printf inf using
原文地址:https://www.cnblogs.com/lipu123/p/12177507.html