poj 2688 (bfs+dfs)

http://poj.org/problem?id=2688

很基础的搜索吧。跑了200MS。。。做完这题发现了一个很囧的东西。。这题貌似就是TSP问题....AC了后想去搜下看有没有好的剪枝,结果发现别人说这是经典的TSP问题。可问题是,,,,,,两个月前我特意去想弄懂什么是TSP问题,却看不懂啊,,,,现在却AC了,,而且思路神马的都是自己想的,,大囧啊,以后搞不懂的东西得换个思路来想了,,想着纠结了这么久的TSP原来就是一个bfs+dfs啊。先bfs找出所有的脏点与robot的最短步数。然后dfs找最短的旅行路线。ps:开始的时候竟然以为是最小生成树。。再囧。。不过收获蛮大的。对搜索熟悉了些吧。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 
  7 using namespace std;
  8 
  9 #define MAXN 22
 10 #define inf 100000000
 11 int w,h;
 12 char map[MAXN][MAXN];
 13 int dist[MAXN][MAXN];
 14 int cnt;//机器人与脏地的个数
 15 int tag[MAXN][MAXN];//标记
 16 struct point
 17 {
 18     int x,y;
 19     int step;
 20 }pos[MAXN*MAXN];
 21 
 22 point robot;
 23 int dir[4][2]={0,-1,0,1,-1,0,1,0};
 24 bool isok(point p)
 25 {
 26     if(p.x<1 || p.x>h || p.y<1 || p.y>w)
 27         return false;
 28     return true;
 29 }
 30 bool vist[MAXN][MAXN];
 31 void bfs(point p,int po)
 32 {
 33     vist[p.x][p.y]=1;
 34     queue<point>q;
 35     while(!q.empty())
 36         q.pop();
 37     q.push(p);
 38     while(!q.empty())
 39     {
 40         point cur=q.front();
 41         q.pop();
 42         if(map[cur.x][cur.y]=='o' || map[cur.x][cur.y]=='*')
 43             dist[po][tag[cur.x][cur.y]]=cur.step;
 44         point next;
 45         next.step=cur.step+1;
 46         for(int i=0;i<4;i++)
 47         {
 48             next.x=cur.x+dir[i][0];
 49             next.y=cur.y+dir[i][1];
 50             if(!isok(next) || vist[next.x][next.y] || map[next.x][next.y]=='x')
 51                 continue;
 52             q.push(next);
 53             vist[next.x][next.y]=1;
 54         }
 55     }
 56 }
 57 
 58 int ans=inf;
 59 bool vis[MAXN];
 60 void dfs(int x,int step,int s)
 61 {
 62     if(step==cnt)
 63     {
 64         if(s<ans)
 65             ans=s;
 66         return ;
 67     }
 68     if(s>ans)
 69         return ;
 70     for(int j=1;j<=cnt;j++)
 71     {
 72         if(vis[j])
 73             continue;
 74         vis[j]=1;
 75         dfs(j,step+1,s+dist[x][j]);
 76         vis[j]=0;
 77     }
 78 }
 79 
 80 int main()
 81 {
 82     while(~scanf("%d%d",&w,&h))
 83     {
 84         if(w+h==0)
 85             break;
 86         getchar();
 87         cnt=0;
 88         memset(pos,0,sizeof(pos));
 89         memset(tag,0,sizeof(tag));
 90         for(int i=1;i<=h;i++)
 91         {
 92             gets(map[i]+1);
 93             for(int j=1;j<=w;j++)
 94                 if(map[i][j]=='o')
 95                 {
 96                     pos[++cnt].x=i;
 97                     pos[cnt].y=j;
 98                     robot.x=i;
 99                     robot.y=j;
100                     tag[i][j]=cnt;
101                 }
102                 else if(map[i][j]=='*')
103                 {
104                     pos[++cnt].x=i;
105                     pos[cnt].y=j;
106                     tag[i][j]=cnt;
107                 }
108         }
109         for(int i=1;i<=cnt;i++)
110             for(int j=1;j<=cnt;j++)
111                 if(i!=j)
112                     dist[i][j]=inf;
113                 else
114                     dist[i][j]=0;
115         for(int i=1;i<=cnt;i++)
116         {    
117             memset(vist,0,sizeof(vist));
118             pos[i].step=0;
119             bfs(pos[i],i);
120         }
121         bool flag=1;
122         for(int i=1;i<=cnt && flag;i++)
123             for(int j=1;j<=cnt && flag;j++)
124                 if(dist[i][j]==inf)
125                     flag=0;
126         /*j----------------------
127         for(int i=1;i<=cnt;i++)
128         {
129             for(int j=1;j<=cnt;j++)
130                 cout<<dist[i][j]<<" ";
131             cout<<endl;
132         }*/
133         //-----------------------
134         if(flag==0)
135         {
136             puts("-1");
137             continue;
138         }
139         memset(vis,0,sizeof(vis));
140         vis[tag[robot.x][robot.y]]=1;
141         ans=inf;
142         dfs(tag[robot.x][robot.y],1,0);
143         printf("%d\n",ans);
144 
145     }
146     return 0;
147 }
原文地址:https://www.cnblogs.com/Missa/p/2710414.html