1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 7 #define dec(i,a,b) for(int i(a); i >= (b); --i) 8 #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) 9 10 #define LL long long 11 #define ULL unsigned long long 12 #define MP make_pair 13 #define PB push_back 14 #define FI first 15 #define SE second 16 #define INF 1 << 30 17 18 const int N = 100000 + 10; 19 const int M = 10000 + 10; 20 const int Q = 1000 + 10; 21 const int A = 100 + 1; 22 23 const int dx[] = {0, 1, 0, -1}; 24 const int dy[] = {1, 0, -1, 0}; 25 26 const int cx[] = {2, 0, -2, 0, 1, -1, 1, -1}; 27 const int cy[] = {0, 2, 0, -2, 1, -1, -1, 1}; 28 29 struct node{ 30 int x, y, k, t; 31 } st, en; 32 33 char s[Q]; 34 int n, m, k, t; 35 int a[A][A]; 36 queue <node> q; 37 int f[A][A]; 38 39 40 bool check(int x, int y){ 41 return (x >= 1) && (x <= m) && (y >= 1) && (y <= n); 42 } 43 44 int main(){ 45 #ifndef ONLINE_JUDGE 46 freopen("test.txt", "r", stdin); 47 freopen("test.out", "w", stdout); 48 #endif 49 50 while (~scanf("%d%d%d%d", &m, &n, &k, &t), n + m + k + t){ 51 memset(a, 0, sizeof a); 52 rep(i, 1, m){ 53 scanf("%s", s + 1); 54 rep(j, 1, n){ 55 if (s[j] == ‘#‘){ 56 a[i][j] = 1; 57 } 58 else{ 59 a[i][j] = 0; 60 if (s[j] == ‘@‘) st.x = i, st.y = j; 61 if (s[j] == ‘X‘) en.x = i, en.y = j; 62 } 63 } 64 65 } 66 while (!q.empty()) q.pop(); 67 st.k = k; 68 st.t = t; 69 q.push(st); 70 memset(f, -1, sizeof f); 71 f[st.x][st.y] = k; 72 int ans = 0; 73 while (!q.empty()){ 74 node now = q.front(); q.pop(); 75 if (now.x == en.x && now.y == en.y && now.t >= 0){ 76 ans = 1; 77 break; 78 } 79 REP(i, 4){ 80 node nn; 81 nn.x = now.x + dx[i]; 82 nn.y = now.y + dy[i]; 83 if (check(nn.x, nn.y) && f[nn.x][nn.y] < now.k && !a[nn.x][nn.y]){ 84 nn.k = now.k; 85 nn.t = now.t - 1; 86 if (nn.t >= 0){ 87 q.push(nn); 88 f[nn.x][nn.y] = nn.k; 89 } 90 } 91 } 92 if (now.k > 0){ 93 REP(i, 8){ 94 node nn; 95 nn.x = now.x + cx[i]; 96 nn.y = now.y + cy[i]; 97 if (check(nn.x, nn.y) && f[nn.x][nn.y] < now.k - 1 && !a[nn.x][nn.y]){ 98 nn.k = now.k - 1; 99 nn.t = now.t - 1; 100 if (nn.t >= 0){ 101 q.push(nn); 102 f[nn.x][nn.y] = nn.k; 103 } 104 } 105 } 106 } 107 } 108 if (ans) puts("Yes"); else puts("No"); 109 } 110 return 0; 111 112 }
原文:http://www.cnblogs.com/cxhscst2/p/6379226.html