Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 8456 | Accepted: 1975 |
Description
Input
Output
Sample Input
8 9 1 1 1 3 2 1 1 3 3 1 1 3 4 1 1 3 1 1 0 3 1 2 0 3 1 3 0 3 1 4 0 3 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 1 1 2 0 3 3 0 4 3 1 1.5 1.5 4 0 1 1 0 1 1 1 1 1 2 1 1 1 1 2 0 1 1.5 1.7 -1 -1
Sample Output
5 -1
题意:先给出n个墙,以x,y为左下角的点d == 1,与
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 const int MAX = 250; 9 const int INF = 0x3f3f3f3f; 10 int maxx,maxy,sx,sy; 11 int h[MAX][MAX],l[MAX][MAX],dis[MAX][MAX]; 12 double f1,f2; 13 struct node 14 { 15 int x,y; 16 int door; 17 bool operator<(const node &a) const 18 { 19 return a.door < door; 20 } 21 }; 22 23 void bfs() 24 { 25 priority_queue<node> q; 26 while(q.empty() == 0) 27 q.pop(); 28 node cur; 29 cur.x = 0; 30 cur.y = 0; 31 cur.door = 0; 32 for(int i = 0; i <= maxx; i++) 33 { 34 for(int j = 0; j <= maxy; j++) 35 { 36 dis[i][j] = INF; 37 } 38 } 39 dis[0][0] = 0; 40 q.push(cur); 41 while(q.empty() == 0) 42 { 43 cur = q.top(); 44 q.pop(); 45 int x = cur.x; 46 int y = cur.y; 47 if(x == sx && y == sy) 48 return; 49 if(y + 1 <= maxy && dis[x][y + 1] > dis[x][y] + h[x][y + 1]) 50 { 51 dis[x][y + 1] = dis[x][y] + h[x][y + 1]; 52 cur.x = x; 53 cur.y = y + 1; 54 cur.door = dis[x][y + 1]; 55 q.push(cur); 56 } 57 if(y -1 >= 0 && dis[x][y - 1] > dis[x][y] + h[x][y]) 58 { 59 dis[x][y - 1] = dis[x][y] + h[x][y]; 60 cur.x = x; 61 cur.y = y - 1; 62 cur.door = dis[x][y - 1]; 63 q.push(cur); 64 } 65 if(x + 1 <= maxx && dis[x + 1][y] > dis[x][y] + l[x + 1][y]) 66 { 67 dis[x + 1][y] = dis[x][y] + l[x + 1][y]; 68 cur.x = x + 1; 69 cur.y = y; 70 cur.door = dis[x + 1][y]; 71 q.push(cur); 72 } 73 if(x - 1 >= 0 && dis[x - 1][y] > dis[x][y] + l[x][y]) 74 { 75 dis[x - 1][y] = dis[x][y] + l[x][y]; 76 cur.x = x - 1; 77 cur.y = y; 78 cur.door = dis[x - 1][y]; 79 q.push(cur); 80 } 81 } 82 dis[sx][sy] = -1; 83 } 84 int main() 85 { 86 int n,m,x,y,d,t; 87 while(scanf("%d%d",&n,&m) != EOF) 88 { 89 if(m == -1 && n == -1) 90 break; 91 maxx = maxy = -1; 92 memset(h,0,sizeof(h)); 93 memset(l,0,sizeof(l)); 94 for(int i = 0; i < n; i++) 95 { 96 scanf("%d%d%d%d",&x,&y,&d,&t); 97 if(d == 0) 98 { 99 for(int j = 0; j < t; j++) 100 { 101 h[x + j][y] = INF; 102 } 103 maxx = max(maxx,x + t); 104 maxy = max(maxy,y); 105 } 106 else 107 { 108 for(int j = 0; j < t; j++) 109 l[x][y + j] = INF; 110 maxx = max(maxx,x); 111 maxy = max(maxy,y + t); 112 } 113 } 114 for(int i = 0; i < m; i++) 115 { 116 scanf("%d%d%d", &x, &y,&d); 117 if(d == 0) 118 { 119 h[x][y] = 1; 120 } 121 else 122 l[x][y] = 1; 123 } 124 scanf("%lf%lf",&f1,&f2); 125 if(f1 > maxx || f2 > maxy) 126 { 127 printf("0\n"); 128 continue; 129 } 130 sx = (int) f1; 131 sy = (int) f2; 132 bfs(); 133 printf("%d\n",dis[sx][sy]); 134 } 135 return 0; 136 }
原文:http://www.cnblogs.com/zhaopAC/p/5008835.html