HDU 4771 Stealing Harry Potter's Precious
来源:互联网 发布:基因调控网络仿真 编辑:程序博客网 时间:2024/06/09 18:18
旅行商问题,状压dp
用一个二进制数表示是否走过当前的几个位置
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <string>#include <math.h>#include <stdlib.h>using namespace std;int n,m;char g[110][110];int a[110][110];int sx,sy;int k;int dp[110][110][32];int px[10],py[10];struct Node{ int x,y; int s; Node(int _x = 0,int _y = 0,int _s = 0) { x = _x; y = _y; s = _s; }};int move_[][2] = {{0,1},{0,-1},{1,0},{-1,0}};int bfs(){ queue<Node>q; int s = 0; for(int i = 0;i < k;i++) if(sx == px[i] && sy == py[i]) s = s|(1<<i); q.push(Node(sx,sy,s)); memset(dp,-1,sizeof(dp)); dp[sx][sy][s] = 0; while(!q.empty()) { Node tmp = q.front(); q.pop(); if(tmp.s == ((1<<k) - 1)) { return dp[tmp.x][tmp.y][tmp.s]; } for(int i = 0;i < 4;i++) { int nx = tmp.x + move_[i][0]; int ny = tmp.y + move_[i][1]; int s = tmp.s; if(nx < 0 || nx >= n || ny < 0 || ny >= m)continue; if(a[nx][ny] == -2)continue; for(int j = 0;j < k;j++) if(nx == px[j] && ny == py[j]) { s |= (1<<j); } if(dp[nx][ny][s] != -1)continue; dp[nx][ny][s] = dp[tmp.x][tmp.y][tmp.s] + 1; q.push(Node(nx,ny,s)); } } return -1;}int main(){ while(scanf("%d%d",&n,&m) == 2) { if(n == 0 && m == 0)break; for(int i = 0;i < n;i++) scanf("%s",g[i]); memset(a,-1,sizeof(a)); for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) { if(g[i][j] == '@') { sx = i; sy = j; } if(g[i][j] == '#') a[i][j] = -2; } scanf("%d",&k); int x,y; for(int i = 0;i < k;i++) { scanf("%d%d",&x,&y); x --; y--; px[i] = x; py[i] = y; } printf("%d\n",bfs()); } return 0;}
阅读全文
0 0
- hdu 4771 - Stealing Harry Potter's Precious
- HDU:4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- Scrapy爬取图片资源
- redis原理
- caffe随记(六)---tools工具举例
- Mac上用什么清理工具最好?
- Summer Training day4上帝与集合的正确用法 欧拉函数+降幂公式
- HDU 4771 Stealing Harry Potter's Precious
- C与C++的相同点与不同点
- 视图
- java.util.Random
- C++中的cin.get()和cin.getline()
- malloc和new,free和delete
- VR开发-罗德里格公式的研究
- mysql教程,mysql快速入门教程
- Tom数 oj58