标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 541 Accepted Submission(s): 141
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 using namespace std; 5 6 const int SIZE = 105; 7 const int UPDATE[][2] = {{1,0},{0,1},{0,0}}; 8 int N,M,K,E; 9 bool FIRE[SIZE][SIZE][1005]; 10 bool VIS[SIZE][SIZE][1005]; 11 bool CASTLE[SIZE][SIZE]; 12 struct Node 13 { 14 int x,y,t,e; 15 bool check(void) 16 { 17 if(x < 0 || x > N || y < 0 || y > M || t > E || VIS[x][y][t] || CASTLE[x][y] || 18 FIRE[x][y][t] || !e || N - x + M - y > e) 19 return false; 20 return true; 21 } 22 }; 23 struct Cas 24 { 25 char ch; 26 int t,v,x,y; 27 }; 28 29 void deal(char ch,int t,int v,int x,int y); 30 void bfs(void); 31 int main(void) 32 { 33 Cas temp[105]; 34 while(scanf("%d%d%d%d",&N,&M,&K,&E) != EOF) 35 { 36 fill(&FIRE[0][0][0],&FIRE[SIZE - 1][SIZE - 1][1004],false); 37 fill(&VIS[0][0][0],&VIS[SIZE - 1][SIZE - 1][1004],false); 38 fill(&CASTLE[0][0],&CASTLE[SIZE - 1][SIZE - 1],false); 39 40 for(int i = 0;i < K;i ++) 41 { 42 scanf(" %c%d%d%d%d",&temp[i].ch,&temp[i].t,&temp[i].v,&temp[i].x,&temp[i].y); 43 CASTLE[temp[i].x][temp[i].y] = true; 44 } 45 if(CASTLE[N][M]) 46 { 47 puts("Bad luck!"); 48 continue; 49 } 50 for(int i = 0;i < K;i ++) 51 deal(temp[i].ch,temp[i].t,temp[i].v,temp[i].x,temp[i].y); 52 bfs(); 53 } 54 55 return 0; 56 } 57 58 void deal(char ch,int t,int v,int x,int y) 59 { 60 if(ch == ‘W‘) 61 { 62 int stop = 0; 63 for(int j = y - 1;j >= 0;j --) 64 if(CASTLE[x][j]) 65 { 66 stop = j; 67 break; 68 } 69 for(int j = y - v,ini = 1;j >= stop;j -= v,ini ++) 70 for(int k = ini;k <= E;k += t) 71 FIRE[x][j][k] = true; 72 73 } 74 else if(ch == ‘E‘) 75 { 76 int stop = M; 77 for(int j = y + 1;j <= M;j ++) 78 if(CASTLE[x][j]) 79 { 80 stop = j; 81 break; 82 } 83 84 for(int j = y + v,ini = 1;j <= stop;j += v,ini ++) 85 for(int k = ini;k <= E;k += t) 86 FIRE[x][j][k] = true; 87 } 88 else if(ch == ‘N‘) 89 { 90 int stop = 0; 91 for(int j = x - 1;j >= 0;j --) 92 if(CASTLE[j][y]) 93 { 94 stop = j; 95 break; 96 } 97 for(int j = x - v,ini = 1;j >= stop;j -= v,ini ++) 98 for(int k = ini;k <= E;k += t) 99 FIRE[j][y][k] = true; 100 } 101 else if(ch == ‘S‘) 102 { 103 int stop = N; 104 for(int j = x + 1;j <= N;j ++) 105 if(CASTLE[j][y]) 106 { 107 stop = j; 108 break; 109 } 110 for(int j = x + v,ini = 1;j <= stop;j += v,ini ++) 111 for(int k = ini;k <= E;k += t) 112 FIRE[j][y][k] = true; 113 } 114 } 115 116 void bfs(void) 117 { 118 Node first; 119 first.x = first.y = first.t = 0; 120 first.e = E; 121 queue<Node> que; 122 que.push(first); 123 VIS[0][0][0] = true; 124 125 while(!que.empty()) 126 { 127 Node cur = que.front(); 128 que.pop(); 129 130 for(int i = 0;i < 3;i ++) 131 { 132 Node next = cur; 133 next.x += UPDATE[i][0]; 134 next.y += UPDATE[i][1]; 135 next.t ++; 136 next.e --; 137 if(!next.check()) 138 continue; 139 if(next.x == N && next.y == M) 140 { 141 printf("%d\n",next.t); 142 return ; 143 } 144 VIS[next.x][next.y][next.t] = true; 145 que.push(next); 146 } 147 } 148 puts("Bad luck!"); 149 }
标签:
原文地址:http://www.cnblogs.com/xz816111/p/4394104.html