标签:相同 数组 lan str %s i++ stl tle net
考察:搜索
思路:
预处理有子弹和城堡的坐标,这部分除了暴力没有更好的办法.因为还有一个时间限制,所以需要三维数组.剩下就是BFS.
剪枝: 1.相同时间的拜访点不必再访问.
2.当离终点的曼哈顿距离>能量 不必再走下去.
注意:城堡处一定要预处理再判断子弹.
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 using namespace std; 6 const int N = 110,INF = 0x3f3f3f3f,M = 1010; 7 typedef pair<int,int> PII; 8 struct Castle{ 9 int dir,T,v,x,y; 10 }ca[N]; 11 struct Node{ 12 int x,y,t; 13 }; 14 int m,n,k,d; 15 bool vis[N][N][M],bullet[N][N][M]; 16 char op[2]; 17 string dirs = "NSWE"; 18 int xx[5] ={-1,1,0,0,0},yy[5] = {0,0,-1,1,0}; 19 void init() 20 { 21 for(int i=1;i<=k;i++) 22 { 23 int dx = ca[i].x,dy = ca[i].y,cnt = 0; 24 while(1) 25 { 26 int sx = dx,sy = dy; 27 dx = dx+xx[ca[i].dir]*ca[i].v,dy = dy+yy[ca[i].dir]*ca[i].v; 28 cnt++; 29 if(dx<0||dy<0||dx>m||dy>n||bullet[dx][dy][0]) break; 30 bool ok =1; 31 while(sx!=dx||sy!=dy) 32 { 33 sx = sx+xx[ca[i].dir],sy = sy+yy[ca[i].dir]; 34 if(bullet[sx][sy][0]) {ok = 0;break;} 35 } 36 if(!ok) break; 37 // printf("%d %d %d被子弹%d\n",dx,dy,cnt,i); 38 int s = cnt; 39 while(s<=d) 40 { 41 bullet[dx][dy][s] = 1; 42 s+=ca[i].T; 43 } 44 } 45 } 46 } 47 int bfs() 48 { 49 queue<Node> q; 50 Node ns; ns.x = 0,ns.y= 0; 51 ns.t = 0; 52 q.push(ns); 53 vis[0][0][0] =1; 54 while(q.size()) 55 { 56 Node it = q.front(); 57 q.pop(); 58 int x= it.x,y = it.y,ti = it.t; 59 if(x==m&&y==n) return ti; 60 if(ti>=d) continue; 61 for(int i=0;i<5;i++) 62 { 63 int dx = x+xx[i],dy = y+yy[i]; 64 if(ti+1<d&&dx>=0&&dx<=m&&dy>=0&&dy<=n&&!vis[dx][dy][ti+1]&&!bullet[dx][dy][ti+1]) 65 { 66 if(bullet[dx][dy][0]) continue; 67 if(ti+m-dx+n-dy>d) continue; 68 Node news = {dx,dy,ti+1}; 69 vis[dx][dy][ti+1] = 1; 70 q.push(news); 71 } 72 } 73 } 74 return -1; 75 } 76 int main() 77 { 78 while(scanf("%d%d%d%d",&m,&n,&k,&d)!=EOF) 79 { 80 memset(vis,0,sizeof vis); 81 memset(bullet,0,sizeof bullet); 82 for(int i=1;i<=k;i++) 83 { 84 scanf("%s%d%d%d%d",op,&ca[i].T,&ca[i].v,&ca[i].x,&ca[i].y); 85 int idx = dirs.find(op[0]); 86 ca[i].dir = idx; 87 bullet[ca[i].x][ca[i].y][0] = 1; 88 } 89 init(); 90 int sc = bfs(); 91 if(sc!=-1) printf("%d\n",sc); 92 else puts("Bad luck!"); 93 } 94 return 0; 95 }
标签:相同 数组 lan str %s i++ stl tle net
原文地址:https://www.cnblogs.com/newblg/p/14687802.html