游荡的奶牛——一个水题

  昨天中午最后十分钟敲的傻逼一样的dfs就不贴了,刚通过的bfs。今天中午又搞了二十几分钟完成的。简单的广搜,刚打完的时候因为把插入新节点的时间写成了t+1 而总是输出0,后来扫视一遍整个代码才发现,改了就对了。

 1 #include<map>
 2 #include<set>
 3 #include<queue>
 4 #include<cmath>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<string>
 8 #include<cstring>
 9 #include<iostream>
10 #include<algorithm>
11 using namespace std;
12 const int ax[]={-1,1,0,0},ay[]={0,0,-1,1};
13 struct node{
14     int x,y,t;
15     node(int n1,int n2,int n3):x(n1),y(n2),t(n3){}
16 };
17 queue<node> q;
18 int n,m,t,sx,sy,tx,ty,res[128][128][16];
19 bool vis[128][128][16];
20 string rc[128];
21 int bfs(){
22     while(!q.empty()){
23         node x=q.front();q.pop();
24         if(x.t>t)break;
25         for(int i=0;i<4;i++){
26             int nx=x.x+ax[i],ny=x.y+ay[i],nt=x.t+1;
27             if(nx>=n||nx<0||ny>=m||ny<0||rc[nx][ny]=='*')continue;
28             res[nx][ny][nt]+=res[x.x][x.y][x.t];
29             if(!vis[nx][ny][nt]){
30                 q.push(node(nx,ny,nt));
31                 vis[nx][ny][nt]=true;
32             }
33         }
34     }
35     return res[tx][ty][t];
36 }
37 int main(){
38     cin>>n>>m>>t;
39     for(int i=0;i<n;i++)cin>>rc[i];
40     cin>>sx>>sy>>tx>>ty;
41     tx--;ty--;
42     sx--;sy--;
43     q.push(node(sx,sy,0));
44     vis[sx][sy][0]=true;
45     res[sx][sy][0]=1;
46     cout<<bfs()<<endl;
47     return 0;
48 }
Method_01

  洛谷上边1ms。

posted @ 2017-05-08 13:25  Darkins  阅读(127)  评论(0)    收藏  举报