【题解】游荡的奶牛-C++
题目 输入输出格式 输出格式: 输入输出样例 草地被划分成4 行5 列,奶牛在6 秒内从第1 行第3 列走到了第1 行第5 列。 奶牛在6 秒内从(1,3)走到(1,5)的方法只有一种(绕过她面前的树) 思路 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,t,cnt; 4 char a[101][101]; 5 struct node 6 { 7 int x,y,t; 8 node(){}; 9 node(int xx,int yy,int tt) 10 { 11 x=xx,y=yy,t=tt; 12 } 13 }st,ed; 14 int dir[4][2]={{1,0},{-1,{0,1},-1}},f[55][55][22]; 15 void bfs(node s) 16 { 17 queue<node> q; 18 q.push(s); 19 while(!q.empty()) 20 { 21 node now=q.front(); 22 q.pop(); 23 if(now.t>ed.t) 24 return; 25 for(int i=0;i<4;i++) 26 { 27 int tx=now.x+dir[i][0],ty=now.y+dir[i][1]; 28 if(a[tx][ty]==‘.‘) 29 { 30 node v=node(tx,ty,now.t+1); 31 if(abs(v.x-ed.x)+abs(v.y-ed.y)>t-v.t)continue; 32 f[tx][ty][now.t+1]+=f[now.x][now.y][now.t]; 33 if(f[tx][ty][now.t+1]-f[now.x][now.y][now.t]>0)continue; 34 q.push(v); 35 } 36 } 37 } 38 return; 39 } 40 int main() 41 { 42 cin>>n>>m>>t; 43 for(int i=1;i<=n;i++) 44 { 45 for(int j=1;j<=m;j++) 46 { 47 cin>>a[i][j]; 48 } 49 } 50 int a,b,c,d; 51 cin>>a>>b>>c>>d; 52 f[a][b][0]=1; 53 st=node(a,0); 54 ed=node(c,d,t); 55 bfs(st); 56 cout<<f[c][d][t]<<endl; 57 return 0; 58 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |