poj2688 Cleaning Robot 简单BFS+DFS
2014-09-24 18:11
295 查看
先bfs处理出各个垃圾之间的距离,然后直接dfs。一次AC~~~
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue> #include <vector> #include <cmath> using namespace std; #define pb(a) push_back(a) struct node { int x,y,n; }; int dx[]={0,1,-1,0}; int dy[]={1,0,0,-1}; vector<node>dirt; node rob; int dis[500][500]; int m,n,lab; char tab[30][30]; int label[30][30]; int vis[30][30],ans; bool judge(int x,int y) { if(x>0&&x<=n&&y>0&&y<=m) { return true; } return false; } bool bfs(node st) { queue<node>q; memset(vis,0,sizeof(vis)); node tmp,cur; int now,xx,yy,x,y,tot,tt; vis[st.x][st.y]=1; q.push(st); while(!q.empty()) { tmp=q.front(); q.pop(); x=tmp.x; y=tmp.y; for(int i=0;i<4;i++) { xx=x+dx[i]; yy=y+dy[i]; if(!judge(xx,yy)) continue; if(vis[xx][yy]||tab[xx][yy]=='x') continue; vis[xx][yy]=vis[x][y]+1; cur.x=xx; cur.y=yy; q.push(cur); } } } int use[500]; void dfs(int now,int sum,int num) { if(sum>ans) { return; } if(num==lab-1) { ans=min(ans,sum); return ; } for(int i=2;i<=lab;i++) { if(use[i]) continue; use[i]=1; dfs(i,sum+dis[now][i],num+1); use[i]=0; } } void solve() { bfs(rob); ans=0x3f3f3f3f; for(int i=0;i<dirt.size();i++) { dis[dirt[i].n][1]=dis[1][dirt[i].n]=vis[dirt[i].x][dirt[i].y]-1; if(dis[1][dirt[i].n]<=0) { printf("-1\n"); return; } } int now; for(int j=0;j<dirt.size();j++) { now=dirt[j].n; bfs(dirt[j]); for(int i=0;i<dirt.size();i++) { dis[dirt[i].n][now]=dis[now][dirt[i].n]=vis[dirt[i].x][dirt[i].y]-1; } } dfs(1,0,0); printf("%d\n",ans); } int main() { while(scanf("%d%d",&m,&n)) { if(n+m==0) { break; } lab=1; node tmp; dirt.clear(); memset(label,0,sizeof(label)); memset(dis,0,sizeof(dis)); memset(use,0,sizeof(use)); for(int i=1;i<=n;i++) { scanf("%s",tab[i]+1); for(int j=1;j<=m;j++) { if(tab[i][j]=='o') { rob.x=i; rob.y=j; rob.n=1; label[i][j]=1; } if(tab[i][j]=='*') { lab++; tmp.x=i; tmp.y=j; tmp.n=lab; label[i][j]=lab; dirt.pb(tmp); } } } solve(); } return 0; } /* 7 5 ....... .o...*. ....... .*...*. ....... 15 13 .......x....... ...o...x....*.. .......x....... .......x....... .......x....... ............... xxxxx.....xxxxx ............... .......x....... .......x....... .......x....... ..*....x....*.. .......x....... 10 10 .......... ..o....... .......... .......... .......... .....xxxxx .....x.... .....x.*.. .....x.... .....x.... 0 0 8 49 -1 */
相关文章推荐
- POJ 2688 Cleaning Robot (BFS+DFS)
- POJ-2688:Cleaning Robot(bfs预处理+dfs)
- POJ 1979 简单dfs||bfs
- poj 2688 Cleaning Robot bfs+dfs
- poj1562 Oil Deposits 简单搜索,dfs和bfs都行,我用的bfs
- poj 2688 Cleaning Robot BFS+DFS(TSP问题)
- POJ 1979 简单dfs||bfs
- poj 2688 (bfs+dfs)
- PKU2688 Cleaning Robot(BFS + DFS)
- POJ 2243 简单搜索 (DFS BFS A*)
- poj 3984 迷宫问题【BFS && DFS】【简单】
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- poj 1979Red and Black(BFS DFS)
- poj 1020 Anniversary Cake 简单dfs
- poj 1856Sea Battle(BFS ,DFS亦可)
- poj 2251Dungeon Master(BFS, DFS亦可)
- POJ 3051 Satellite Photographs 简单DFS
- POJ 3083 Children of the Candy Corn DFS及BFS搜索
- POJ 1111 Image Perimeters 简单DFS
- poj 2688 Cleaning Robot