HDU
来源:互联网 发布:疯狂的美工助手破解版 编辑:程序博客网 时间:2024/05/01 16:48
题目地址:HDU-1044
HDU ACM 1044 Collect More Jewels BFS+DFS
用BFS转化为DFS常见情况求解
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;int map[60][60];int jewel[60];int w,h,l,m;int value,sum;int X[]= {-1,0,0,1};int Y[]= {0,1,-1,0};int step[60][60];int dis[60][60];int dv[60];struct node { int x,y;};int judge(int x,int y) { if(x<0||x>=h||y<0||y>=w||map[x][y]=='*') return 0; return 1;}void bfs(int x,int y,int s) { int v[60][60]; memset(v,0,sizeof(v)); memset(step,0,sizeof(step)); queue<node> q; node t,t1; t.x=x; t.y=y; q.push(t); v[x][y]=1; step[x][y]=0; while(!q.empty()) { t1=q.front(); q.pop(); for(int i=0; i<4; i++) { t.x=t1.x+X[i]; t.y=t1.y+Y[i]; if(!judge(t.x,t.y)) continue ; if(v[t.x][t.y]) continue ; step[t.x][t.y]=step[t1.x][t1.y]+1; v[t.x][t.y]=1; if(map[t.x][t.y]=='@') dis[s][0]=step[t.x][t.y]; else if(map[t.x][t.y]=='<') dis[s][m+1]=step[t.x][t.y]; else if(map[t.x][t.y]>='A' && map[t.x][t.y]<='J') dis[s][map[t.x][t.y]-'A'+1]=step[t.x][t.y]; q.push(t); } }}void dfs(int s,int v,int t) { if(t>l) return ; if(value==sum) return ; if(s>m) { if(v>value) value=v; return ; } for(int i=0; i<=m+1; i++) { if(!dis[s][i]||dv[i]) continue ; dv[i]=1; dfs(i,v+jewel[i],t+dis[s][i]); dv[i]=0; }}int main() { int N; scanf("%d",&N); for(int p=1; p<=N; p++) { sum=0; scanf("%d%d%d%d",&w,&h,&l,&m); for(int i=1; i<=m; i++) { scanf("%d",&jewel[i]); sum+=jewel[i]; } getchar(); for(int i=0; i<h; i++) { for(int j=0; j<w; j++) scanf("%c",&map[i][j]); getchar(); } memset(dis,0,sizeof(dis)); jewel[0]=0; jewel[m+1]=0; for(int i=0; i<h; i++) { for(int j=0; j<w; j++) if(map[i][j]=='@') bfs(i,j,0); else if(map[i][j]=='<') bfs(i,j,m+1); else if(map[i][j]>='A' && map[i][j]<='Z') bfs(i,j,map[i][j]-'A'+1); } memset(dv,0,sizeof(dv)); value=-1; dv[0]=1; dfs(0,0,0); printf("Case %d:\n",p); if(value<0) printf("Impossible\n"); else printf("The best score is %d.\n",value); if(p!=N) printf("\n"); } return 0;}
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- scala入门
- 关于STM32 中的外部中断EXIT的说明和如何启用
- bzoj 3926: [Zjoi2015]诸神眷顾的幻想乡 后缀自动机
- 【BZOJ 4445】[Scoi2015]小凸想跑步 半平面交
- debug的使用--必备
- HDU
- C++与运算(&)错误笔记(优先级)
- iOS信号量机制
- java实现最大堆及代码测试
- Android使用OkHttp实例,以及OkHttp方法封装
- 图像形态学操作—腐蚀膨胀深入
- appium的一个内存泄露bug
- HTML的表单元素和input元素
- IO