标签:
11
10
8
......s...
..........
#ooooooo.o
#.........
#.........
#.........
#.....m...
#.........
10
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; struct node{ int x; int y; int dist; friend bool operator < (node a,node b){ return a.dist > b.dist; } }; priority_queue<node> q; int t,x,y,startx,starty,endx,endy,map[50][50],jud[50][50]; int dx[4] = {-1,0,1,0}; int dy[4] = {0,-1,0,1}; void input(){ cin>>t>>x>>y; char cmd; for(int i = 1;i <= y;i++){ for(int j = 1;j <= x;j++){ cin>>cmd; if(cmd == ‘.‘) map[i][j] = 1; if(cmd == ‘#‘) map[i][j] = 2; if(cmd == ‘o‘) map[i][j] = 3; if(cmd == ‘s‘){ map[i][j] = 1; startx = j; starty = i; } if(cmd == ‘m‘){ map[i][j] = 1; endx = j; endy = i; } } } node tmp; tmp.x = startx; tmp.y = starty; tmp.dist = 0; q.push(tmp); for(int i = 1;i <= 40;i++){ for(int j = 1;j <= 40;j++){ jud[i][j] = 100000000; } } } bool bfs(){ node now,next; int nx,ny; while(!q.empty()){ now = q.top(); q.pop(); for(int i = 0;i < 4;i++){ nx = now.x + dx[i]; ny = now.y + dy[i]; if(nx < 1 || nx > x || ny < 1 || ny > y || map[ny][nx] == 3 ||jud[ny][nx] <= now.dist + map[ny][nx]) continue; next.x = nx; next.y = ny; next.dist = now.dist + map[ny][nx]; if(nx == endx && ny == endy){ if(next.dist >= t) return false; else{ cout<<next.dist<<endl; return true; } } q.push(next); jud[ny][nx] = next.dist; } } } int main(){ input(); if(!bfs()) cout<<55555<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5812555.html