深搜加广搜
来源:互联网 发布:人工智能测试工程师 编辑:程序博客网 时间:2024/05/14 11:09
B. Stealing Harry Potter's Precious
Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d Java class name: MainSubmit Status PID: 33993
Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:
Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
Input
There are several test cases.
In each test cases:
The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
The input ends with N = 0 and M = 0
In each test cases:
The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
The input ends with N = 0 and M = 0
Output
For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
Sample Input
2 3##@#.#12 24 4#@##....####....22 12 40 0
Sample Output
-15
之前写这个题看人家状态压缩加广搜,感觉不会状压,后来才知道就是深搜加广搜。
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>#include<math.h>using namespace std;typedef long long LL;const int maxn=1005;const int INF=0x3f3f3f3f;char s[120][120];int vis[120][120];int dx[4]= {0,0,-1,1},dy[4]= {-1,1,0,0};int f[120][120];int n,m;struct node{ int x,y,step;};queue<node>que;int judge(int xx,int yy){ if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy]!='#'&&vis[xx][yy]==0) return 1; return 0;}int bfs(node aa,node bb){ while(!que.empty()) que.pop(); memset(vis,0,sizeof(vis)); node qq; aa.step=0; que.push(aa); vis[aa.x][aa.y]=1; while(!que.empty()) { qq=que.front(); que.pop(); if(qq.x==bb.x&&qq.y==bb.y) { return qq.step; } for(int i=0; i<4; i++) { int nx=qq.x+dx[i]; int ny=qq.y+dy[i]; if(judge(nx,ny)) { node dd; dd.x=nx; dd.y=ny; dd.step=qq.step+1; // printf("step %d %d %d\n",dd.x,dd.y,dd.step); que.push(dd); vis[nx][ny]=1; } } } return -1;}int perm[15],used[15],minn=INF;node ss,a[10];void permutation(int pos,int nn){ if(pos==nn) { int ans=0,flag=0; for(int i=1; i<nn; i++) { if(bfs(a[perm[i-1]],a[perm[i]])==-1) { flag=1; break; } ans+=bfs(a[perm[i-1]],a[perm[i]]); // printf("%d ",perm[i]); } //cout<<bfs(ss,a[perm[0]])<<endl; if(bfs(ss,a[perm[0]])==-1) flag=1; else ans+=bfs(ss,a[perm[0]]); //printf("%d***",ans); if(flag) minn=INF; else minn=min(minn,ans); //printf("\n"); return; } for(int i=1; i<=nn; i++) { if(!used[i]) { perm[pos]=i; used[i]=true; permutation(pos+1,nn); used[i]=false; } } return;}int main(){ //permutation(0,4); while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; for(int i=0; i<n; i++) { scanf("%s",s[i]); for(int j=0; j<m; j++) { if(s[i][j]=='@') { ss.x=i; ss.y=j; ss.step=0; } } } int q; // printf("f1---%d %d\n",f1,f2); scanf("%d",&q); int flag=0; memset(f,0,sizeof(f)); for(int i=1; i<=q; i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].x--; a[i].y--; } minn=INF; memset(used,0,sizeof(used)); permutation(0,q);// if(flag) printf("-1\n");// else printf("%d\n",res); if(minn==INF) printf("-1\n"); else printf("%d\n",minn); }}
宝宝爱搜集一切大神的代码 这个深搜就是一个数的全排列的深搜啊哈算法第一章的深搜下面上啊哈算法代码
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>#include<math.h>//n的全排列的代码int a[10],book[10],n;void dfs(int step){ int i; if(step==n+1) { for(i=1;i<=n;i++) { printf("%d ",a[i]); } printf("\n"); return; } for(i=1;i<=n;i++) { if(book[i]==0) { a[step]=i; book[i]=1; dfs(step+1); book[i]=0; } } return;}int main(){ scanf("%d",&n); dfs(1); getchar(); getchar(); return 0;}
0 0
- 深搜加广搜
- poj 3083 Children of the Candy Corn 深搜加广搜
- IE和火狐的css兼容性问题
- StackoverflowAboutPython(1)—— Python中关键字yield有什么作用?
- Linux下查看tomcat占用端口
- C++ cin.clear()的使用:防止出现死循环
- 订单系统开发(仿淘宝和美团网) 之 项目总结(一)
- 深搜加广搜
- java成神之路
- 第二章JavaScript对象
- 设备树学习之(二)点灯
- dubbo组成原理-zookeeper注册原理
- C# 结构体与类的区别
- linux查看文件20到30行的三种方法
- Binary Tree Inorder Traversal
- MySQL Replication 的基本知识及简单配置(1)