【搜索】 HDU 3152 Obstacle Course
2014-10-07 19:46
246 查看
优先队列点按数值最小出队
#include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <cmath> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> #include <time.h>; #define cler(arr, val) memset(arr, val, sizeof(arr)) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define IN freopen ("in.txt" , "r" , stdin); #define OUT freopen ("out.txt" , "w" , stdout); typedef long long LL; const int MAXN = 100010; const int MAXM = 444; //const int INF = 0x3f3f3f3f; const int mod = 1000000007; const int INF= 9999999; int mp[126][126]; bool vis[126][126]; int n ; int xx[4]= {1,0,0,-1}; int yy[4]= {0,1,-1,0}; struct node { int x,y,step; bool operator < (const node& a) const { return step>a.step; } }; bool in(int x,int y) { if(x<=n&&y<=n&&x>=1&&y>=1) return true; return false; } priority_queue<node> q; int bfs(int x,int y) { while(!q.empty()) q.pop(); vis[x][y] =true; node front; front.x=x; front.y=y; front.step=mp[x][y]; q.push(front); while(!q.empty()) { front=q.top(); q.pop(); for(int i=0;i<4;i++) { int dx=front.x+xx[i],dy=front.y+yy[i]; if(in(dx,dy)&&!vis[dx][dy]) { vis[dx][dy]=true; node rear; rear.x=dx,rear.y=dy,rear.step=front.step+mp[dx][dy]; if(dx==n&&dy==n) return rear.step; q.push(rear); } } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif int cas=1; while(~scanf("%d", &n) && n) { memset(vis,false,sizeof(vis)); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) scanf("%d",&mp[i][j]); printf("Problem %d: %d\n",cas++,bfs(1,1)); } return 0; }
相关文章推荐
- 【搜索】 HDU 3152 Obstacle Course
- hdu 3152 Obstacle Course
- hdu 3152 Obstacle Course
- HDU 3152 Obstacle Course(优先队列)
- HDU 1551(搜索题,二分)
- HDU 2199(搜索题,二分)
- HDU 2241(搜索题,三分)
- HDU 2289(搜索题,二分、几何)
- HDU 1241(搜索题,DFS)
- hdu1864 搜索版本
- HDU 1016(搜索题,DFS)
- HDU 2446(搜索题,二分)
- HDU 1399(搜索题,暴力- -!)
- hdu 1242 Rescue BFS搜索
- hdu 1015 Safecracker 暴力搜索
- HDU 1251(搜索题,典型的字典树问题)
- HDU 1969(搜索题,二分)
- HDU 1312(搜索题,DFS)
- HDU 1905(搜索题,逐次平方法)
- hdu 1312 Red and Black DFS搜索 or BFS搜索