3 solutions
-
0
#include<bits/stdc++.h> using namespace std; const int N=1e3; int m,n,sum=0,cnt=0,dp[N],min1=1000005; bool check[N][N]; char a[N][N]; int dx[4]={0,-1,0,1}; int dy[4]={-1,0,1,0}; void dfs(int nx,int ny) { if(a[nx][ny]'*'){ dp[sum]=cnt; sum++; } for(int i=0;i<4;i++){ int x=dx[i]+nx; int y=dy[i]+ny; if(a[x][y]!='#'&&x>=1&&x<=m&&y>=1&&y<=n&&!check[x][y]){ check[nx][ny]=true; cnt++; dfs(x,y); check[nx][ny]=false; cnt--; } } } int main() { cin>>m>>n; int i,j,tx,ty; for(i=1;i<=m;i++){ for(j=1;j<=n;j++){ cin>>a[i][j]; if(a[i][j]'@'){ tx=i; ty=j; } } } dfs(tx,ty); if(dp[0]==0){ cout<<"-1";//只需判断计数为0时是否有路程,即是否到过一次目标点即可 } else{ for(i=0;i<sum;i++){ min1=min(min1,dp[i]); } cout<<min1; } return 0; }
Information
- ID
- 1236
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 158
- Accepted
- 50
- Uploaded By